1/* eslint-disable */ 2/** 3 * Google Tag Manager initialization script 4 * Gets brand, environment, containerId, and GTM URL from config values 5 * Uses a page-level flag to prevent duplicate GTM script injection within 6 * the same JS context, while allowing GTM to load on each new page navigation. 7 */ 8 9import { getConfigValue } from './configs.js'; 10 11// Data Layer Initialization 12window.dataLayer = window.dataLayer || []; 13 14// Module-level flag: resets on every page load (new JS context). 15// Prevents duplicate GTM script injection if initGTM() is called more than 16// once within the same page (e.g. AEM editor soft-refresh or block re-init), 17// without blocking GTM from loading on subsequent full-page navigations. 18let gtmInitializedThisPage = false; 19 20// Initialize GTM with values from config 21async function initGTM() { 22 // Check if GTM is enabled (defaults to true if not specified) 23 const enableGTM = (await getConfigValue('enable-gtm')) || 'true'; 24 if (enableGTM === 'false') { 25 return; // Skip GTM initialization if explicitly disabled 26 } 27 28 // Get values from config without defaults 29 const brand = await getConfigValue('brand-name'); 30 const environment = await getConfigValue('environment'); 31 const containerId = await getConfigValue('gtm-containerId'); 32 const gtmUrl = await getConfigValue('gtm-url') || 'https://www.googletagmanager.com/gtm.js'; 33 34 // Only initialize if we have a container ID 35 if (!containerId) { 36 console.warn('GTM initialization skipped: No container ID found in config'); 37 return; 38 } 39 40 // Guard against duplicate initialization within the same page/JS context. 41 // Using a module-level variable (not sessionStorage) so it resets on each 42 // full page navigation while still protecting against same-page double calls. 43 if (gtmInitializedThisPage) { 44 // Ensure dataLayer has current page context without firing gtm.js event 45 const dataLayerObj = { siteType: 'aem' }; 46 if (brand) dataLayerObj.brand = brand; 47 if (environment) dataLayerObj.environment = environment; 48 window.dataLayer.push(dataLayerObj); 49 return; 50 } 51 52 // Prepare dataLayer object with only valid values 53 const dataLayerObj = { siteType: 'aem' }; 54 55 // Only add brand and environment if they exist 56 if (brand) dataLayerObj.brand = brand; 57 if (environment) dataLayerObj.environment = environment; 58 59 // Push the variables into the dataLayer 60 window.dataLayer.push(dataLayerObj); 61 62 // Initialize GTM with the container ID and URL from config 63 (function initGTMScript(w, d, s, l, i) { 64 w[l] = w[l] || []; 65 w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' }); 66 const f = d.getElementsByTagName(s)[0]; 67 const j = d.createElement(s); 68 const dl = l !== 'dataLayer' ? `&l=${l}` : ''; 69 j.async = true; 70 j.src = `${gtmUrl}?id=${i}${dl}`; 71 f.parentNode.insertBefore(j, f); 72 }(window, document, 'script', 'dataLayer', containerId)); 73 74 // Mark GTM as initialized for this page load 75 gtmInitializedThisPage = true; 76 77 // GTM initialized with container ID 78} 79 80// Export the function for use in other modules 81export default initGTM;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.