From eb02e280440c594cef3dd30f44972d46a289f1a5 Mon Sep 17 00:00:00 2001 From: bosco-ensemble Date: Tue, 9 Apr 2024 13:12:15 -0700 Subject: [PATCH] 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');