/* eslint-disable */
// TODO: try to use typescript instead of vanilla js. It's bit problematic due to how next.js handles the public folder
/**
* This function checks if the user has accepted the performance cookie. This is cookie is used, among other things, to decide whether we
* track user's behaviour on the page, e.g. if he clicked on a link, scroll down...
* This cookie is managed by OneTrust and we have 3 scenarios:
* - The general cookie ${COOKIE_ONETRUST} doesn't exist -> true
* - The cookie doesn't contain the perfomance cookie or its value is false -> false (this happens when you delete the cookie after accepting it)
* - The performance cookie exists -> we return its value
*
* @param {*} cookies document cookies as a string
*/
function arePerformanceAndMarketingCookiesAccepted(cookies, firstCall) {
const COOKIE_ONETRUST = 'OptanonConsent';
const KEY_GROUPS = 'groups';
const PERFORMANCE_COOKIE_KEY = 'C0002';
const MARKETING_COOKIE_KEY = 'C0004';
// eslint-disable-next-line no-undef
const cookieOneTrust = cookies
.split(';')
.map((str) => str.trim())
.find((ck) => ck.startsWith(`${COOKIE_ONETRUST}=`));
if (!cookieOneTrust && firstCall) {
return false;
}
if (!cookieOneTrust) {
// there is no oneTrust cookie
return true;
}
const groups = cookieOneTrust
.substring(`${COOKIE_ONETRUST}=`.length)
.split('&')
.find((str) => str.startsWith(`${KEY_GROUPS}=`));
if (!groups) {
// the cookie exist but not the group, we wait for the user to decide
return false;
}
const groupsValue = groups.substring(`${KEY_GROUPS}=`.length);
const splittedCookie = decodeURIComponent(groupsValue).split(',');
const performanceCookie = splittedCookie.find((str) =>
str.includes(PERFORMANCE_COOKIE_KEY)
);
const marketingCookie = splittedCookie.find((str) =>
str.includes(MARKETING_COOKIE_KEY)
);
return {
performanceCookieAccepted:
(performanceCookie && performanceCookie.split(':')[1] === '1') || false,
marketingCookieAccepted:
(marketingCookie && marketingCookie.split(':')[1] === '1') || false,
};
}
/**
* It's executed on each page load, or whenever the user saves changes to the privacy settings in the Preference Centre.
* It checks if the function getEventListeners is available, it depends on the FF removeOneTrustListeners, if present
* it removes all the listeners from the HTML element which are related to the OneTrust cookie banner
*/
function OptanonWrapper() {
if (
typeof window.document.documentElement.getEventListeners !== 'undefined'
) {
const clickListeners =
window.document.documentElement.getEventListeners('click');
for (const el of [...clickListeners]) {
console.log({ el });
window.document.documentElement.removeEventListener('click', el.listener);
}
}
}
/**
* It's executed on each page load, or whenever the user saves changes to the privacy settings in the Preference Centre.
*/
function LegacyOptanonWrapper(firstCall = false) {
const { performanceCookieAccepted, marketingCookieAccepted } =
arePerformanceAndMarketingCookiesAccepted(document.cookie, firstCall);
window.ocdev = window.ocdev || {};
window.ocdev.performanceCookieAccepted = performanceCookieAccepted;
window.ocdev.marketingCookieAccepted = marketingCookieAccepted;
const performanceCookieUpdatedEvent = new Event('cookiesUpdated');
window.dispatchEvent(performanceCookieUpdatedEvent);
}
// We call it manually once (instead of from onetrust) in case onetrust is deferred.
// OptanonWrapper(true);