From cf72b0762aa9b3d1949fc3c9b9a46879fc394b63 Mon Sep 17 00:00:00 2001 From: Francisco Chicharro Sanz Date: Thu, 23 May 2024 17:35:03 +0200 Subject: [PATCH] feat(checkpoint): add consent checkpoint to 1.x --- src/index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/index.js b/src/index.js index b260d8e..0ee9922 100644 --- a/src/index.js +++ b/src/index.js @@ -195,3 +195,38 @@ new PerformanceObserver((list) => { .forEach(([key, value]) => { sampleRUM('utm', { source: key, target: value }); }); + +// 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)) { + const cmpCookie = document.cookie.split(';') + .map((c) => c.trim()) + .find((cookie) => cookie.startsWith('OptanonAlertBoxClosed=')); + + if (cmpCookie) { + sampleRUM('consent', { source: 'onetrust', target: 'hidden' }); + } + + 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 }, + ); + } + } +}