Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#220 update the cookie layer options in main tag #269

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -366,6 +405,7 @@ async function loadPage() {
await loadEager(document);
await loadLazy(document);
loadDelayed();
updateCookieOptions();
}

loadPage();
Expand Down