From e9abfd9e5053c98a70a4de71126a56417baab8b2 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:02:57 -0700 Subject: [PATCH 01/18] TDW-2277 initial test commit --- scripts/delayed.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 65ee7fa9..90007277 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -646,19 +646,19 @@ async function populateStatusBar(statusBar) { populateStatusBar(document.querySelector('header > .status-bar')); -/* setup cookie preferences */ -function getCookie(cookieName) { - const name = `${cookieName}=`; - const decodedCookie = decodeURIComponent(document.cookie); - const split = decodedCookie.split(';'); - // eslint-disable-next-line no-plusplus - for (let i = 0; i < split.length; i++) { - let c = split[i]; - while (c.charAt(0) === ' ') c = c.substring(1); - if (c.indexOf(name) === 0) return c.substring(name.length, c.length); - } - return null; -} +// /* setup cookie preferences */ +// function getCookie(cookieName) { +// const name = `${cookieName}=`; +// const decodedCookie = decodeURIComponent(document.cookie); +// const split = decodedCookie.split(';'); +// // eslint-disable-next-line no-plusplus +// for (let i = 0; i < split.length; i++) { +// let c = split[i]; +// while (c.charAt(0) === ' ') c = c.substring(1); +// if (c.indexOf(name) === 0) return c.substring(name.length, c.length); +// } +// return null; +// } async function OptanonWrapper() { const geoInfo = window.Optanon.getGeolocationData(); From 5d6924741664107d0e176c6b4f99089eff4cb533 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:08:13 -0700 Subject: [PATCH 02/18] TDW-2277 moved load ad code functions into scripts.js --- scripts/delayed.js | 69 ---------------------------------------------- scripts/scripts.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 69 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 90007277..588cba00 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -646,75 +646,6 @@ async function populateStatusBar(statusBar) { populateStatusBar(document.querySelector('header > .status-bar')); -// /* setup cookie preferences */ -// function getCookie(cookieName) { -// const name = `${cookieName}=`; -// const decodedCookie = decodeURIComponent(document.cookie); -// const split = decodedCookie.split(';'); -// // eslint-disable-next-line no-plusplus -// for (let i = 0; i < split.length; i++) { -// let c = split[i]; -// while (c.charAt(0) === ' ') c = c.substring(1); -// if (c.indexOf(name) === 0) return c.substring(name.length, c.length); -// } -// return null; -// } - -async function OptanonWrapper() { - const geoInfo = window.Optanon.getGeolocationData(); - Object.keys(geoInfo).forEach((key) => { - const cookieName = `PGAT_${key.charAt(0).toUpperCase() + key.slice(1)}`; - const cookie = getCookie(cookieName); - if (!cookie || cookie !== geoInfo[key]) document.cookie = `${cookieName}=${geoInfo[key]}`; - }); - - const prevOptIn = localStorage.getItem('OptIn_PreviousPermissions'); - if (prevOptIn) { - try { - const settings = JSON.parse(prevOptIn); - if (settings.tempImplied) { - localStorage.removeItem('OptIn_PreviousPermissions'); - } - } catch (e) { - // eslint-disable-next-line no-console - console.error('OptIn_PreviousPermissions parse failed'); - } - } - sendAnalyticsPageEvent(); -} - -const otId = placeholders.onetrustId; -if (otId) { - const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); - cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); - cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); - cookieScript.setAttribute('data-nscript', 'beforeInteractive'); - - const gtmId = placeholders.googletagmanagerId; - if (gtmId) { - const GTMScript = document.createElement('script'); - GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': - new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], - j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= - 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); - })(window,document,'script','dataLayer','${gtmId}');`; - document.head.append(GTMScript); - - const GTMFrame = document.createElement('no-script'); - GTMFrame.innerHTML = ``; - document.body.prepend(GTMFrame); - } - - window.OptanonWrapper = OptanonWrapper; - - if (document.querySelector('.marketing')) { - const marketingBlock = document.querySelector('.marketing'); - decorateBlock(marketingBlock); - loadBlock(marketingBlock); - } -} - async function loadLiveChat() { const liveChat = getMetadata('live-chat'); if (liveChat && ['yes', 'on', 'true'].includes(liveChat.toLowerCase())) { diff --git a/scripts/scripts.js b/scripts/scripts.js index 0d58e1de..ecc6e811 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1133,6 +1133,75 @@ function getPageNameAndSections() { }; } +/* setup cookie preferences */ +function getCookie(cookieName) { + const name = `${cookieName}=`; + const decodedCookie = decodeURIComponent(document.cookie); + const split = decodedCookie.split(';'); + // eslint-disable-next-line no-plusplus + for (let i = 0; i < split.length; i++) { + let c = split[i]; + while (c.charAt(0) === ' ') c = c.substring(1); + if (c.indexOf(name) === 0) return c.substring(name.length, c.length); + } + return null; +} + +async function OptanonWrapper() { + const geoInfo = window.Optanon.getGeolocationData(); + Object.keys(geoInfo).forEach((key) => { + const cookieName = `PGAT_${key.charAt(0).toUpperCase() + key.slice(1)}`; + const cookie = getCookie(cookieName); + if (!cookie || cookie !== geoInfo[key]) document.cookie = `${cookieName}=${geoInfo[key]}`; + }); + + const prevOptIn = localStorage.getItem('OptIn_PreviousPermissions'); + if (prevOptIn) { + try { + const settings = JSON.parse(prevOptIn); + if (settings.tempImplied) { + localStorage.removeItem('OptIn_PreviousPermissions'); + } + } catch (e) { + // eslint-disable-next-line no-console + console.error('OptIn_PreviousPermissions parse failed'); + } + } + await sendAnalyticsPageEvent(); +} + +const otId = placeholders.onetrustId; +if (otId) { + const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); + cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); + cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); + cookieScript.setAttribute('data-nscript', 'beforeInteractive'); + + const gtmId = placeholders.googletagmanagerId; + if (gtmId) { + const GTMScript = document.createElement('script'); + GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': + new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], + j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= + 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); + })(window,document,'script','dataLayer','${gtmId}');`; + document.head.append(GTMScript); + + const GTMFrame = document.createElement('no-script'); + GTMFrame.innerHTML = ``; + document.body.prepend(GTMFrame); + } + + window.OptanonWrapper = OptanonWrapper; + + if (document.querySelector('.marketing')) { + const marketingBlock = document.querySelector('.marketing'); + decorateBlock(marketingBlock); + await loadBlock(marketingBlock); + } +} + export async function sendAnalyticsPageEvent() { window.dataLayer = window.dataLayer || []; const dl = window.dataLayer; From 7a2472432f6276ca0172c7b64c029a03ed91ed5e Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:15:18 -0700 Subject: [PATCH 03/18] TDW-2277 minor code clean up --- scripts/delayed.js | 8 ------- scripts/scripts.js | 52 +++++++++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 588cba00..1cc88dda 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -11,14 +11,6 @@ import { sendAnalyticsPageEvent, } from './scripts.js'; -const placeholders = await fetchPlaceholders(); -const isProd = window.location.hostname.endsWith(placeholders.hostname); - -if (!isProd === 'this') { - // temporary override for analytics testing - if (!localStorage.getItem('OptIn_PreviousPermissions')) localStorage.setItem('OptIn_PreviousPermissions', '{"aa":true,"mediaaa":true,"target":true,"ecid":true,"adcloud":true,"aam":true,"campaign":true,"livefyre":false}'); -} - // Core Web Vitals RUM collection sampleRUM('cwv'); diff --git a/scripts/scripts.js b/scripts/scripts.js index ecc6e811..ed14a19b 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -710,6 +710,7 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define + await loadAds(doc); loadDelayed(doc); } @@ -1170,35 +1171,44 @@ async function OptanonWrapper() { await sendAnalyticsPageEvent(); } -const otId = placeholders.onetrustId; -if (otId) { - const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); - cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); - cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); - cookieScript.setAttribute('data-nscript', 'beforeInteractive'); - - const gtmId = placeholders.googletagmanagerId; - if (gtmId) { - const GTMScript = document.createElement('script'); - GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': +async function loadAds(doc) { + const placeholders = await fetchPlaceholders(); + const isProd = window.location.hostname.endsWith(placeholders.hostname); + + if (!isProd === 'this') { + // temporary override for analytics testing + if (!localStorage.getItem('OptIn_PreviousPermissions')) localStorage.setItem('OptIn_PreviousPermissions', '{"aa":true,"mediaaa":true,"target":true,"ecid":true,"adcloud":true,"aam":true,"campaign":true,"livefyre":false}'); + } + const otId = placeholders.onetrustId; + if (otId) { + const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); + cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); + cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); + cookieScript.setAttribute('data-nscript', 'beforeInteractive'); + + const gtmId = placeholders.googletagmanagerId; + if (gtmId) { + const GTMScript = doc.createElement('script'); + GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','${gtmId}');`; - document.head.append(GTMScript); + document.head.append(GTMScript); - const GTMFrame = document.createElement('no-script'); - GTMFrame.innerHTML = ``; - document.body.prepend(GTMFrame); - } + document.body.prepend(GTMFrame); + } - window.OptanonWrapper = OptanonWrapper; + window.OptanonWrapper = OptanonWrapper; - if (document.querySelector('.marketing')) { - const marketingBlock = document.querySelector('.marketing'); - decorateBlock(marketingBlock); - await loadBlock(marketingBlock); + if (doc.querySelector('.marketing')) { + const marketingBlock = doc.querySelector('.marketing'); + decorateBlock(marketingBlock); + await loadBlock(marketingBlock); + } } } From 888af82e93745f24c75755c715009262879000f4 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:19:25 -0700 Subject: [PATCH 04/18] TDW-2277 code cleanup and added fetchplaceholders in delayedjs --- scripts/delayed.js | 5 ++--- scripts/scripts.js | 47 +++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 1cc88dda..6cb855e0 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -3,14 +3,13 @@ import { decorateIcons, fetchPlaceholders, sampleRUM, - decorateBlock, - loadBlock, loadScript, getMetadata, fetchGraphQL, - sendAnalyticsPageEvent, } from './scripts.js'; +const placeholders = await fetchPlaceholders(); + // Core Web Vitals RUM collection sampleRUM('cwv'); diff --git a/scripts/scripts.js b/scripts/scripts.js index ed14a19b..4ada6548 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1148,6 +1148,29 @@ function getCookie(cookieName) { return null; } +export async function sendAnalyticsPageEvent() { + window.dataLayer = window.dataLayer || []; + const dl = window.dataLayer; + const placeholders = await fetchPlaceholders(); + const isUserLoggedIn = window.gigyaAccountInfo && window.gigyaAccountInfo != null + && window.gigyaAccountInfo.errorCode === 0; + + const { pageName, sections } = getPageNameAndSections(); + dl.push({ + event: 'pageload', + pageName, + pageUrl: window.location.href, + siteSection: sections[0] || '', + siteSubSection: sections[1] || '', + siteSubSection2: sections[2] || '', + gigyaID: isUserLoggedIn && window.gigyaAccountInfo.UID ? window.gigyaAccountInfo.UID : '', + userLoggedIn: isUserLoggedIn ? 'Logged In' : 'Logged Out', + tourName: placeholders.tourName.toLowerCase().replaceAll(' ', '_'), + tournamentID: `${placeholders.tourCode.toUpperCase()}${placeholders.currentYear}${placeholders.tournamentId}`, + ipAddress: '127.0.0.1', + deviceType: 'Web', + }); +} async function OptanonWrapper() { const geoInfo = window.Optanon.getGeolocationData(); Object.keys(geoInfo).forEach((key) => { @@ -1212,30 +1235,6 @@ async function loadAds(doc) { } } -export async function sendAnalyticsPageEvent() { - window.dataLayer = window.dataLayer || []; - const dl = window.dataLayer; - const placeholders = await fetchPlaceholders(); - const isUserLoggedIn = window.gigyaAccountInfo && window.gigyaAccountInfo != null - && window.gigyaAccountInfo.errorCode === 0; - - const { pageName, sections } = getPageNameAndSections(); - dl.push({ - event: 'pageload', - pageName, - pageUrl: window.location.href, - siteSection: sections[0] || '', - siteSubSection: sections[1] || '', - siteSubSection2: sections[2] || '', - gigyaID: isUserLoggedIn && window.gigyaAccountInfo.UID ? window.gigyaAccountInfo.UID : '', - userLoggedIn: isUserLoggedIn ? 'Logged In' : 'Logged Out', - tourName: placeholders.tourName.toLowerCase().replaceAll(' ', '_'), - tournamentID: `${placeholders.tourCode.toUpperCase()}${placeholders.currentYear}${placeholders.tournamentId}`, - ipAddress: '127.0.0.1', - deviceType: 'Web', - }); -} - try { const hidden = Symbol('hidden'); const proxy = Symbol('proxy'); From 13070a6b782712a5d140f2a909f290c17252e9d3 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:21:35 -0700 Subject: [PATCH 05/18] TDW-2277 fixed loaddelayed eslint error --- scripts/scripts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 4ada6548..9a3d3076 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -709,8 +709,8 @@ async function loadPage(doc) { await loadEager(doc); // eslint-disable-next-line no-use-before-define await loadLazy(doc); - // eslint-disable-next-line no-use-before-define await loadAds(doc); + // eslint-disable-next-line no-use-before-define loadDelayed(doc); } From 92f1ef9902fd4b6d6fa6639806990967414a6ace Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:23:18 -0700 Subject: [PATCH 06/18] TDW-2277 fixed loadads eslint error --- scripts/scripts.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/scripts.js b/scripts/scripts.js index 9a3d3076..55e99d99 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -709,6 +709,7 @@ async function loadPage(doc) { await loadEager(doc); // eslint-disable-next-line no-use-before-define await loadLazy(doc); + // eslint-disable-next-line no-use-before-define await loadAds(doc); // eslint-disable-next-line no-use-before-define loadDelayed(doc); From bf0cb6e016c3c3b538cceb305896a7ee57cca8b5 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Mon, 8 Apr 2024 18:40:08 -0700 Subject: [PATCH 07/18] TDW-2277 called load ads function in load lazy --- scripts/scripts.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 55e99d99..6eed0509 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -710,8 +710,6 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define - await loadAds(doc); - // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1071,6 +1069,9 @@ async function loadLazy(doc) { addFavIcon(`${window.hlx.codeBasePath}/styles/favicon.ico`); doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); + + // eslint-disable-next-line no-use-before-define + await loadAds(doc); } /** From 9717da8635dc5914798536d1b7893cacdb8b84bd Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 09:01:27 -0700 Subject: [PATCH 08/18] TDW-2277 load in ads 1 second after loadlazy --- scripts/scripts.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 6eed0509..963aef33 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1071,7 +1071,9 @@ async function loadLazy(doc) { doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); // eslint-disable-next-line no-use-before-define - await loadAds(doc); + window.setTimeout(async () => { + await loadAds(doc); + }, 1000); } /** From b179ce77a8f72eb9aadc4e21b685df3507fc2a7b Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 09:03:04 -0700 Subject: [PATCH 09/18] TDW-2277 minor code fix --- scripts/scripts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 963aef33..00eceab9 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1070,8 +1070,8 @@ async function loadLazy(doc) { doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); - // eslint-disable-next-line no-use-before-define window.setTimeout(async () => { + // eslint-disable-next-line no-use-before-define await loadAds(doc); }, 1000); } From d225216e78be4c4398dc2c1cfd04643c9347e144 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 10:13:34 -0700 Subject: [PATCH 10/18] TDW-2277 lint fix --- scripts/scripts.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 00eceab9..1af9d56d 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1072,7 +1072,7 @@ async function loadLazy(doc) { window.setTimeout(async () => { // eslint-disable-next-line no-use-before-define - await loadAds(doc); + await loadAds(); }, 1000); } @@ -1198,7 +1198,7 @@ async function OptanonWrapper() { await sendAnalyticsPageEvent(); } -async function loadAds(doc) { +async function loadAds() { const placeholders = await fetchPlaceholders(); const isProd = window.location.hostname.endsWith(placeholders.hostname); @@ -1215,7 +1215,7 @@ async function loadAds(doc) { const gtmId = placeholders.googletagmanagerId; if (gtmId) { - const GTMScript = doc.createElement('script'); + const GTMScript = document.createElement('script'); GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= @@ -1223,7 +1223,7 @@ async function loadAds(doc) { })(window,document,'script','dataLayer','${gtmId}');`; document.head.append(GTMScript); - const GTMFrame = doc.createElement('no-script'); + const GTMFrame = document.createElement('no-script'); GTMFrame.innerHTML = ``; document.body.prepend(GTMFrame); From 663adb225bba5f09afe8c4126e03bddb7f2fe378 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 10:19:32 -0700 Subject: [PATCH 11/18] TDW-2277 call loadAds function in loadPage --- scripts/scripts.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 1af9d56d..5071c509 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -710,6 +710,8 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define + await loadAds(); + // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1070,10 +1072,6 @@ async function loadLazy(doc) { doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); - window.setTimeout(async () => { - // eslint-disable-next-line no-use-before-define - await loadAds(); - }, 1000); } /** @@ -1231,8 +1229,8 @@ async function loadAds() { window.OptanonWrapper = OptanonWrapper; - if (doc.querySelector('.marketing')) { - const marketingBlock = doc.querySelector('.marketing'); + if (document.querySelector('.marketing')) { + const marketingBlock = document.querySelector('.marketing'); decorateBlock(marketingBlock); await loadBlock(marketingBlock); } From 718460817bf4416b196b8f68a3079501d43f7c9b Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 10:21:22 -0700 Subject: [PATCH 12/18] TDW-2277 lint fix --- scripts/scripts.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 5071c509..3c41c4d1 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -1071,7 +1071,6 @@ async function loadLazy(doc) { addFavIcon(`${window.hlx.codeBasePath}/styles/favicon.ico`); doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); - } /** From d2a760bebe9199438108ab35044a649b9b050857 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 10:26:09 -0700 Subject: [PATCH 13/18] TDW-2277 moved placeholder call to outside the function --- scripts/scripts.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 3c41c4d1..26baa0c6 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -701,6 +701,9 @@ async function waitForLCP() { }); } +// eslint-disable-next-line no-use-before-define +const placeholders = await fetchPlaceholders(); + /** * Decorates the page. */ @@ -710,7 +713,7 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define - await loadAds(); + await loadAds(placeholders); // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1195,8 +1198,7 @@ async function OptanonWrapper() { await sendAnalyticsPageEvent(); } -async function loadAds() { - const placeholders = await fetchPlaceholders(); +async function loadAds(placeholders) { const isProd = window.location.hostname.endsWith(placeholders.hostname); if (!isProd === 'this') { From 4cb9ab713075c88d91e50903e86af97e17b25392 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 11:16:29 -0700 Subject: [PATCH 14/18] TDW-2277 configured loadads to not be an async function --- scripts/scripts.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/scripts.js b/scripts/scripts.js index 26baa0c6..e58657ef 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -713,7 +713,7 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define - await loadAds(placeholders); + loadAds(placeholders); // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1198,7 +1198,7 @@ async function OptanonWrapper() { await sendAnalyticsPageEvent(); } -async function loadAds(placeholders) { +function loadAds(placeholders) { const isProd = window.location.hostname.endsWith(placeholders.hostname); if (!isProd === 'this') { @@ -1233,7 +1233,7 @@ async function loadAds(placeholders) { if (document.querySelector('.marketing')) { const marketingBlock = document.querySelector('.marketing'); decorateBlock(marketingBlock); - await loadBlock(marketingBlock); + loadBlock(marketingBlock); } } } From eb02e280440c594cef3dd30f44972d46a289f1a5 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 13:12:15 -0700 Subject: [PATCH 15/18] TDW-2277 refactored load ads function --- scripts/delayed.js | 86 ++++++++++++++++++++++++++++++++++++++++++++-- scripts/scripts.js | 66 ++--------------------------------- 2 files changed, 86 insertions(+), 66 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 6cb855e0..0fd4c47c 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -3,12 +3,21 @@ import { decorateIcons, fetchPlaceholders, sampleRUM, + decorateBlock, + loadBlock, loadScript, getMetadata, fetchGraphQL, + sendAnalyticsPageEvent, } from './scripts.js'; const placeholders = await fetchPlaceholders(); +const isProd = window.location.hostname.endsWith(placeholders.hostname); + +if (!isProd === 'this') { + // temporary override for analytics testing + if (!localStorage.getItem('OptIn_PreviousPermissions')) localStorage.setItem('OptIn_PreviousPermissions', '{"aa":true,"mediaaa":true,"target":true,"ecid":true,"adcloud":true,"aam":true,"campaign":true,"livefyre":false}'); +} // Core Web Vitals RUM collection sampleRUM('cwv'); @@ -560,8 +569,8 @@ export function initGigya() { const favoriteButtons = document.querySelectorAll('.leaderboard-favorite-button'); if (favoriteButtons) favoriteButtons.forEach((btn) => btn.replaceWith(btn.cloneNode(true))); loadScript( - 'https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', - setupGigya, + 'https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', + setupGigya, ); } @@ -637,6 +646,77 @@ async function populateStatusBar(statusBar) { populateStatusBar(document.querySelector('header > .status-bar')); +/* setup cookie preferences */ +function getCookie(cookieName) { + const name = `${cookieName}=`; + const decodedCookie = decodeURIComponent(document.cookie); + const split = decodedCookie.split(';'); + // eslint-disable-next-line no-plusplus + for (let i = 0; i < split.length; i++) { + let c = split[i]; + while (c.charAt(0) === ' ') c = c.substring(1); + if (c.indexOf(name) === 0) return c.substring(name.length, c.length); + } + return null; +} + +async function OptanonWrapper() { + const geoInfo = window.Optanon.getGeolocationData(); + Object.keys(geoInfo).forEach((key) => { + const cookieName = `PGAT_${key.charAt(0).toUpperCase() + key.slice(1)}`; + const cookie = getCookie(cookieName); + if (!cookie || cookie !== geoInfo[key]) document.cookie = `${cookieName}=${geoInfo[key]}`; + }); + + const prevOptIn = localStorage.getItem('OptIn_PreviousPermissions'); + if (prevOptIn) { + try { + const settings = JSON.parse(prevOptIn); + if (settings.tempImplied) { + localStorage.removeItem('OptIn_PreviousPermissions'); + } + } catch (e) { + // eslint-disable-next-line no-console + console.error('OptIn_PreviousPermissions parse failed'); + } + } + sendAnalyticsPageEvent(); +} + +export const loadAds = (function() { + const otId = placeholders.onetrustId; + if (otId) { + const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); + cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); + cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); + cookieScript.setAttribute('data-nscript', 'beforeInteractive'); + + const gtmId = placeholders.googletagmanagerId; + if (gtmId) { + const GTMScript = document.createElement('script'); + GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': + new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], + j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= + 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); + })(window,document,'script','dataLayer','${gtmId}');`; + document.head.append(GTMScript); + + const GTMFrame = document.createElement('no-script'); + GTMFrame.innerHTML = ``; + document.body.prepend(GTMFrame); + } + + window.OptanonWrapper = OptanonWrapper; + + if (document.querySelector('.marketing')) { + const marketingBlock = document.querySelector('.marketing'); + decorateBlock(marketingBlock); + loadBlock(marketingBlock); + } + } +}) + async function loadLiveChat() { const liveChat = getMetadata('live-chat'); if (liveChat && ['yes', 'on', 'true'].includes(liveChat.toLowerCase())) { @@ -676,4 +756,4 @@ if (hasFevo) { const hasWeFevo = document.querySelector('a.we-fevo-btn'); if (hasWeFevo) { injectWeFevoScript(); -} +} \ No newline at end of file diff --git a/scripts/scripts.js b/scripts/scripts.js index e58657ef..dd6f011e 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -10,6 +10,8 @@ * governing permissions and limitations under the License. */ +import {loadAds} from "./delayed.js"; + /** * log RUM if part of the sample. * @param {string} checkpoint identifies the checkpoint in funnel @@ -713,7 +715,7 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define - loadAds(placeholders); + loadAds(); // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1175,68 +1177,6 @@ export async function sendAnalyticsPageEvent() { deviceType: 'Web', }); } -async function OptanonWrapper() { - const geoInfo = window.Optanon.getGeolocationData(); - Object.keys(geoInfo).forEach((key) => { - const cookieName = `PGAT_${key.charAt(0).toUpperCase() + key.slice(1)}`; - const cookie = getCookie(cookieName); - if (!cookie || cookie !== geoInfo[key]) document.cookie = `${cookieName}=${geoInfo[key]}`; - }); - - const prevOptIn = localStorage.getItem('OptIn_PreviousPermissions'); - if (prevOptIn) { - try { - const settings = JSON.parse(prevOptIn); - if (settings.tempImplied) { - localStorage.removeItem('OptIn_PreviousPermissions'); - } - } catch (e) { - // eslint-disable-next-line no-console - console.error('OptIn_PreviousPermissions parse failed'); - } - } - await sendAnalyticsPageEvent(); -} - -function loadAds(placeholders) { - const isProd = window.location.hostname.endsWith(placeholders.hostname); - - if (!isProd === 'this') { - // temporary override for analytics testing - if (!localStorage.getItem('OptIn_PreviousPermissions')) localStorage.setItem('OptIn_PreviousPermissions', '{"aa":true,"mediaaa":true,"target":true,"ecid":true,"adcloud":true,"aam":true,"campaign":true,"livefyre":false}'); - } - const otId = placeholders.onetrustId; - if (otId) { - const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); - cookieScript.setAttribute('data-domain-script', `${otId}${isProd ? '' : '-test'}`); - cookieScript.setAttribute('data-dlayer-name', 'dataLayer'); - cookieScript.setAttribute('data-nscript', 'beforeInteractive'); - - const gtmId = placeholders.googletagmanagerId; - if (gtmId) { - const GTMScript = document.createElement('script'); - GTMScript.innerHTML = `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': - new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], - j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= - 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); - })(window,document,'script','dataLayer','${gtmId}');`; - document.head.append(GTMScript); - - const GTMFrame = document.createElement('no-script'); - GTMFrame.innerHTML = ``; - document.body.prepend(GTMFrame); - } - - window.OptanonWrapper = OptanonWrapper; - - if (document.querySelector('.marketing')) { - const marketingBlock = document.querySelector('.marketing'); - decorateBlock(marketingBlock); - loadBlock(marketingBlock); - } - } -} try { const hidden = Symbol('hidden'); From a57a6778c6487e80440f27e29469edb63a985936 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 13:21:43 -0700 Subject: [PATCH 16/18] TDW-2277 lint/duplicate code fix --- scripts/delayed.js | 9 ++++----- scripts/scripts.js | 17 +---------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 0fd4c47c..34236d8b 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -568,9 +568,7 @@ export function initGigya() { if (userButton) userButton.replaceWith(userButton.cloneNode(true)); const favoriteButtons = document.querySelectorAll('.leaderboard-favorite-button'); if (favoriteButtons) favoriteButtons.forEach((btn) => btn.replaceWith(btn.cloneNode(true))); - loadScript( - 'https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', - setupGigya, + loadScript('https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', setupGigya, ); } @@ -660,7 +658,8 @@ function getCookie(cookieName) { return null; } -async function OptanonWrapper() { +const OptanonWrapper = (async function() { + console.log('test'); const geoInfo = window.Optanon.getGeolocationData(); Object.keys(geoInfo).forEach((key) => { const cookieName = `PGAT_${key.charAt(0).toUpperCase() + key.slice(1)}`; @@ -681,7 +680,7 @@ async function OptanonWrapper() { } } sendAnalyticsPageEvent(); -} +}) export const loadAds = (function() { const otId = placeholders.onetrustId; diff --git a/scripts/scripts.js b/scripts/scripts.js index dd6f011e..832fa1be 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -715,8 +715,6 @@ async function loadPage(doc) { // eslint-disable-next-line no-use-before-define await loadLazy(doc); // eslint-disable-next-line no-use-before-define - loadAds(); - // eslint-disable-next-line no-use-before-define loadDelayed(doc); } @@ -1076,6 +1074,7 @@ async function loadLazy(doc) { addFavIcon(`${window.hlx.codeBasePath}/styles/favicon.ico`); doc.querySelectorAll('div:not([class]):not([id]):empty').forEach((empty) => empty.remove()); + loadAds(); } /** @@ -1140,20 +1139,6 @@ function getPageNameAndSections() { }; } -/* setup cookie preferences */ -function getCookie(cookieName) { - const name = `${cookieName}=`; - const decodedCookie = decodeURIComponent(document.cookie); - const split = decodedCookie.split(';'); - // eslint-disable-next-line no-plusplus - for (let i = 0; i < split.length; i++) { - let c = split[i]; - while (c.charAt(0) === ' ') c = c.substring(1); - if (c.indexOf(name) === 0) return c.substring(name.length, c.length); - } - return null; -} - export async function sendAnalyticsPageEvent() { window.dataLayer = window.dataLayer || []; const dl = window.dataLayer; From ab972246b26d902c5588163babddcae26063b3b8 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 13:29:50 -0700 Subject: [PATCH 17/18] TDW-2277 lint fix --- scripts/delayed.js | 13 ++++++------- scripts/scripts.js | 6 ++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 34236d8b..5f878d71 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -568,8 +568,7 @@ export function initGigya() { if (userButton) userButton.replaceWith(userButton.cloneNode(true)); const favoriteButtons = document.querySelectorAll('.leaderboard-favorite-button'); if (favoriteButtons) favoriteButtons.forEach((btn) => btn.replaceWith(btn.cloneNode(true))); - loadScript('https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', setupGigya, - ); + loadScript('https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', setupGigya,); } initGigya(); @@ -658,7 +657,7 @@ function getCookie(cookieName) { return null; } -const OptanonWrapper = (async function() { +const OptanonWrapper = (async function () { console.log('test'); const geoInfo = window.Optanon.getGeolocationData(); Object.keys(geoInfo).forEach((key) => { @@ -680,9 +679,9 @@ const OptanonWrapper = (async function() { } } sendAnalyticsPageEvent(); -}) +}); -export const loadAds = (function() { +export const loadAds = (function () { const otId = placeholders.onetrustId; if (otId) { const cookieScript = loadScript('https://cdn.cookielaw.org/scripttemplates/otSDKStub.js'); @@ -714,7 +713,7 @@ export const loadAds = (function() { loadBlock(marketingBlock); } } -}) +}); async function loadLiveChat() { const liveChat = getMetadata('live-chat'); @@ -755,4 +754,4 @@ if (hasFevo) { const hasWeFevo = document.querySelector('a.we-fevo-btn'); if (hasWeFevo) { injectWeFevoScript(); -} \ No newline at end of file +} diff --git a/scripts/scripts.js b/scripts/scripts.js index 832fa1be..64d6aff5 100644 --- a/scripts/scripts.js +++ b/scripts/scripts.js @@ -10,7 +10,8 @@ * governing permissions and limitations under the License. */ -import {loadAds} from "./delayed.js"; +// eslint-disable-next-line import/no-cycle +import { loadAds } from './delayed.js'; /** * log RUM if part of the sample. @@ -703,9 +704,6 @@ async function waitForLCP() { }); } -// eslint-disable-next-line no-use-before-define -const placeholders = await fetchPlaceholders(); - /** * Decorates the page. */ From b188791de273b684760b484e7bff8d8a10d28200 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 13:32:03 -0700 Subject: [PATCH 18/18] TDW-2277 lint fix --- scripts/delayed.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/delayed.js b/scripts/delayed.js index 5f878d71..39e56c9a 100644 --- a/scripts/delayed.js +++ b/scripts/delayed.js @@ -568,7 +568,7 @@ export function initGigya() { if (userButton) userButton.replaceWith(userButton.cloneNode(true)); const favoriteButtons = document.querySelectorAll('.leaderboard-favorite-button'); if (favoriteButtons) favoriteButtons.forEach((btn) => btn.replaceWith(btn.cloneNode(true))); - loadScript('https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', setupGigya,); + loadScript('https://cdns.gigya.com/JS/socialize.js?apikey=3__4H034SWkmoUfkZ_ikv8tqNIaTA0UIwoX5rsEk96Ebk5vkojWtKRZixx60tZZdob', setupGigya); } initGigya();