diff --git a/scripts/consent-check.js b/scripts/consent-check.js new file mode 100644 index 0000000..2cb8edf --- /dev/null +++ b/scripts/consent-check.js @@ -0,0 +1,89 @@ +import { loadScript } from './aem.js'; + +const OT_DOMAIN_SCRIPT = '0bce5055-3ba9-41df-8539-fb806717230d'; +const OT_TEST_COOKIE_DOMAINS = [ + '.cat.com', + '.caterpillar.com', + '.fgwilson.com', + '.mak-catmarine.com', + '.perkins.com', + '.progressrail.com', + '.solarturbines.com', + '.spmoilandgas.com', + '.tangentenergy.com', + '.pecktech.com', + '.anchorcoupling.com', + '.anchorfluidsystems.com', + '.catrentalstore.com', + '.catdealer.com', +]; + +let consentedLoaded = false; + +/** + * Clears OneTrust cookies on test domains so the banner can be re-tested. + */ +function clearTestConsentCookies() { + ['OptanonConsent', 'OptanonAlertBoxClosed'].forEach((name) => { + OT_TEST_COOKIE_DOMAINS.forEach((domain) => { + document.cookie = `${name}=;domain=${domain};path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT`; + }); + }); +} + +/** + * Returns true once the user has closed the OneTrust banner. + * @returns {boolean} + */ +function hasConsent() { + return document.cookie.split(';').some((cookie) => cookie.trim().startsWith('OptanonAlertBoxClosed=')); +} + +/** + * Loads consented scripts once consent is available. + */ +function loadConsented() { + if (consentedLoaded) return; + consentedLoaded = true; + import('./consented.js'); +} + +/** + * Handles OneTrust consent updates. + * @param {string[]} groups Active OneTrust group IDs + */ +function onConsentUpdate(groups) { + window.dispatchEvent(new CustomEvent('consent.onetrust', { detail: groups })); + + if (hasConsent()) { + loadConsented(); + } +} + +/** + * Loads OneTrust and waits for consent before loading consented scripts. + */ +async function initOneTrust() { + if (OT_DOMAIN_SCRIPT.includes('-test')) { + clearTestConsentCookies(); + } + + window.OptanonWrapper = () => { + const groups = (window.OnetrustActiveGroups || '') + .split(',') + .filter(Boolean); + onConsentUpdate(groups); + }; + + window.addEventListener('OneTrustGroupsUpdated', (event) => { + onConsentUpdate(event.detail || []); + }); + + await loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js', { + id: 'otDomainScript', + charset: 'UTF-8', + 'data-domain-script': OT_DOMAIN_SCRIPT, + }); +} + +initOneTrust(); diff --git a/scripts/consented.js b/scripts/consented.js new file mode 100644 index 0000000..1bd713f --- /dev/null +++ b/scripts/consented.js @@ -0,0 +1,8 @@ +/* eslint-disable */ +// Google Tag Manager +(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': +new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], +j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= +'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); +})(window,document,'script','dataLayer','GTM-5RP9FXB'); +// End Google Tag Manager