Skip to content

Instantly share code, notes, and snippets.

@rubentd
Last active January 21, 2020 17:55
Show Gist options
  • Save rubentd/c9dc9fe0e223fb42bbd13ee25ef32c1e to your computer and use it in GitHub Desktop.
Save rubentd/c9dc9fe0e223fb42bbd13ee25ef32c1e to your computer and use it in GitHub Desktop.
import { gaSettings } from 'config';
// parse the country code from the url
const parseCountry = (url) => {
const parts = url.split('/');
const locale = parts[3];
if (locale) {
return locale.split('-')[1];
}
return '';
};
// wait until OneTrust object is available
export const waitForOneTrust = () => {
console.log('waiting for OneTrust...', window.OneTrust);
return new Promise((resolve, reject) => {
if (window.OneTrust) {
resolve();
} else {
setTimeout(() => {
resolve(waitForOneTrust());
}, 1000);
}
});
};
// Set up PgDataLayer
export const initGTM = () => {
console.log('Initializing gtm');
if (!window.OneTrust) {
return;
}
if (typeof window.PGdataLayer === 'undefined') {
window.PGdataLayer = {};
}
const oneTrustDomainData = window.OneTrust.GetDomainData();
const country = parseCountry(window.location.href);
const gtmSettings = {
SiteEnvironment: gaSettings.environment, // Test or Prod
SiteCountry: country,
SiteHost: window.location.host,
SiteTechnicalAgency: 'treum',
SiteLocalContainer: '',
GoogleAnalyticsLocal: gaSettings.code, // GA property tracking id
GoogleAnalyticsStaging: '',
GoogleAnalyticsOptimizeContainerID: '',
ConsentOverlay: 'onetrust',
ConsentOverlayID: oneTrustDomainData.cctId,
SiteGDPR: true,
GoogleAnalyticsDisabled: '',
};
window.PGdataLayer.GTM = gtmSettings;
window.console.log(gtmSettings);
};
// virtual page view event
export const gtmEvent = () => {
window.dataLayer.push({ event: 'virtualPageview' });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment