Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Add gtm #105

Merged
merged 4 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 20 additions & 17 deletions scripts/delayed.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
loadScript,
getMetadata,
fetchGraphQL,
clearDataLayer,
pushOneTrustConsentGroups,
sendAnalyticsPageEvent,
} from './scripts.js';

Expand Down Expand Up @@ -673,36 +671,41 @@ async function OptanonWrapper() {
const prevOptIn = localStorage.getItem('OptIn_PreviousPermissions');
if (prevOptIn) {
try {
const adobeSettings = JSON.parse(prevOptIn);
if (adobeSettings.tempImplied) {
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');
}
}

clearDataLayer();
loadScript(`https://assets.adobedtm.com/d17bac9530d5/a14f7717d75d/launch-aa66aad171be${isProd ? '.min' : ''}.js`, () => {
pushOneTrustConsentGroups();
// eslint-disable-next-line no-undef
gigya.accounts.getAccountInfo({
callback: (response) => {
window.gigyaAccountInfo = response;
sendAnalyticsPageEvent();
},
});
});
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', 'adobeDataLayer');
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('.ads')) {
Expand Down
22 changes: 4 additions & 18 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,20 +1122,6 @@ export function addHeaderSizing(block, classPrefix = 'heading', selector = 'h1,
});
}

export function clearDataLayer() {
window.adobeDataLayer = [];
}

export function pushOneTrustConsentGroups() {
window.adobeDataLayer = window.adobeDataLayer || [];
const dl = window.adobeDataLayer;
dl.push({
event: 'LaunchOTLoaded',
// eslint-disable-next-line no-undef
OnetrustActiveGroups: typeof OnetrustActiveGroups !== 'undefined' ? OnetrustActiveGroups : '',
});
}

function getPageNameAndSections() {
const pageSectionParts = window.location.pathname.split('/').filter((subPath) => subPath !== '');
const pageName = pageSectionParts.join(':');
Expand All @@ -1148,23 +1134,23 @@ function getPageNameAndSections() {
}

export async function sendAnalyticsPageEvent() {
window.adobeDataLayer = window.adobeDataLayer || [];
const dl = window.adobeDataLayer;
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: 'pageLoaded',
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: 'pgatour',
tourName: placeholders.tourName.toLowerCase().replaceAll(' ', '_'),
tournamentID: `${placeholders.tourCode.toUpperCase()}${placeholders.currentYear}${placeholders.tournamentId}`,
ipAddress: '127.0.0.1',
deviceType: 'Web',
Expand Down