From 492a8e4ebc23598f68e87e666f313ed22db834fe Mon Sep 17 00:00:00 2001 From: Jonatan Lledo Date: Fri, 27 Jan 2023 17:15:15 +0100 Subject: [PATCH] #220 update the cookie layer options in main tag --- scripts/scripts.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/scripts/scripts.js b/scripts/scripts.js index 79a67bbc..dd4e35cd 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -297,6 +297,45 @@ function setPageLanguage() { document.documentElement.lang = languagePath; } +/** + * Updates _main_ tag width the user cookies preference based on the stored in the + * 3rd party cookie layer variable ___OptanonActiveGroups___ + * and listen ___OneTrustGroupsUpdated___ custom event + */ +function updateCookieOptions() { + const cookieOptions = { + C0001: 'Critical', + C0002: 'Performance', + C0003: 'Functional', + C0004: 'Targeting', + none: 'none-accepted', + all: 'all-accepted', + }; + + const NONE_ACCEPTED = 1; + const ALL_ACCEPTED = 4; + + document.addEventListener('OneTrustGroupsUpdated', () => { + const main = document.querySelector('main'); + // eslint-disable-next-line no-undef + const cookies = OptanonActiveGroups.split(',').filter((cookie) => cookie.trim() !== ''); + const { length } = cookies; + if (length <= NONE_ACCEPTED) { + main.dataset.wpPageCookie = cookieOptions.none; + } else if (length === ALL_ACCEPTED) { + main.dataset.wpPageCookie = cookieOptions.all; + } else { + const acceptedCookies = NONE_ACCEPTED > 0 + ? cookies.slice(NONE_ACCEPTED) + : cookies; + acceptedCookies.forEach((cookie, i) => { + acceptedCookies[i] = cookieOptions[cookie]; + }); + main.dataset.wpPageCookie = acceptedCookies; + } + }); +} + /** * loads everything needed to get to LCP. */ @@ -366,6 +405,7 @@ async function loadPage() { await loadEager(document); await loadLazy(document); loadDelayed(); + updateCookieOptions(); } loadPage();