Skip to content

Commit

Permalink
TDW-2277 refactored load ads function
Browse files Browse the repository at this point in the history
  • Loading branch information
bosco-ensemble committed Apr 9, 2024
1 parent 4cb9ab7 commit eb02e28
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 66 deletions.
86 changes: 83 additions & 3 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',

Check failure on line 572 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
setupGigya,

Check failure on line 573 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 4 spaces but found 6
);
}

Expand Down Expand Up @@ -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() {

Check warning on line 686 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Unexpected unnamed function

Check failure on line 686 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Missing space before function parentheses
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 = `<iframe src="https://www.googletagmanager.com/ns.html?id=${gtmId}"
height="0" width="0" style="display:none;visibility:hidden"></iframe>`;
document.body.prepend(GTMFrame);
}

window.OptanonWrapper = OptanonWrapper;

if (document.querySelector('.marketing')) {
const marketingBlock = document.querySelector('.marketing');
decorateBlock(marketingBlock);
loadBlock(marketingBlock);
}
}
})

Check failure on line 718 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

async function loadLiveChat() {
const liveChat = getMetadata('live-chat');
if (liveChat && ['yes', 'on', 'true'].includes(liveChat.toLowerCase())) {
Expand Down Expand Up @@ -676,4 +756,4 @@ if (hasFevo) {
const hasWeFevo = document.querySelector('a.we-fevo-btn');
if (hasWeFevo) {
injectWeFevoScript();
}
}

Check failure on line 759 in scripts/delayed.js

View workflow job for this annotation

GitHub Actions / build

Newline required at end of file but not found
66 changes: 3 additions & 63 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import {loadAds} from "./delayed.js";

Check failure on line 13 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Dependency cycle detected

Check failure on line 13 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

A space is required after '{'

Check failure on line 13 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

A space is required before '}'

Check failure on line 13 in scripts/scripts.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

/**
* log RUM if part of the sample.
* @param {string} checkpoint identifies the checkpoint in funnel
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = `<iframe src="https://www.googletagmanager.com/ns.html?id=${gtmId}"
height="0" width="0" style="display:none;visibility:hidden"></iframe>`;
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');
Expand Down

0 comments on commit eb02e28

Please sign in to comment.