Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions scripts/consent-check.js
Original file line number Diff line number Diff line change
@@ -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();
8 changes: 8 additions & 0 deletions scripts/consented.js
Original file line number Diff line number Diff line change
@@ -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