Skip to content

Commit

Permalink
feat(checkpoint): add consent checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chicharr committed May 23, 2024
1 parent 4c52a6b commit 6bf3740
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ function optedIn(checkpoint, data) {
}
// Gets configured collection from the config service for the current domain
function getCollectionConfig() {
// eslint-disable-next-line max-len
if ([770, 1136].includes(Array.from(window.origin).map((a) => a.charCodeAt(0)).reduce((a, b) => a + b, 1) % 1371)) {
return DEFAULT_TRACKING_EVENTS.concat('consent');
}
return DEFAULT_TRACKING_EVENTS;
}

Expand Down Expand Up @@ -285,6 +289,41 @@ function addFormTracking(parent) {
});
}

function addCookieConsentTracking() {
const cmpCookie = document.cookie.split(';')
.map((c) => c.trim())
.find((cookie) => cookie.startsWith('OptanonAlertBoxClosed='));

if (cmpCookie) {
sampleRUM('consent', { source: 'onetrust', target: 'hidden' });
return;
}

let consentMutationObserver;
const trackShowConsent = () => {
if (document.querySelector('body > div#onetrust-consent-sdk')) {
sampleRUM('consent', { source: 'onetrust', target: 'show' });
if (consentMutationObserver) {
consentMutationObserver.disconnect();
}
return true;
}
return false;
};

if (!trackShowConsent()) {
// eslint-disable-next-line max-len
consentMutationObserver = window.MutationObserver ? new MutationObserver(trackShowConsent) : null;
if (consentMutationObserver) {
consentMutationObserver.observe(
document.body,
// eslint-disable-next-line object-curly-newline
{ attributes: false, childList: true, subtree: false },
);
}
}
}

const addObserver = (ck, fn, block) => getCollectionConfig().includes(ck) && fn(block);
function mutationsCallback(mutations) {
mutations.filter((m) => m.type === 'attributes' && m.attributeName === 'data-block-status')
Expand All @@ -310,6 +349,7 @@ function addTrackingFromConfig() {
utm: () => addUTMParametersTracking(),
viewblock: () => addViewBlockTracking(window.document.body),
viewmedia: () => addViewMediaTracking(window.document.body),
consent: () => addCookieConsentTracking(),
};

getCollectionConfig().filter((ck) => trackingFunctions[ck])
Expand Down

0 comments on commit 6bf3740

Please sign in to comment.