### Initialize Google Tag Manager in VTEX App Source: https://github.com/vtex-apps/google-tag-manager/blob/master/pixel/head.html This JavaScript snippet sets up Google Tag Manager (GTM) by retrieving the GTM ID and GA4 event configuration from application settings. It initializes the `dataLayer` object, pushes a configurable blacklist, and dynamically injects the official GTM script into the page. An error is logged if the GTM ID is not provided. ```JavaScript (function() { var gtmId = "{{settings.gtmId}}"; var sendGA4EventsValue = "{{settings.sendGA4Events}}" var sendGA4Events = sendGA4EventsValue == 'true' window.__gtm__ = { sendGA4Events } if (!gtmId) { console.error('Warning: No Google Tag Manager ID is defined. Please configure it in the apps admin.'); } else { window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'gtm.blacklist': {{ blacklist }} }); // GTM script snippet. Taken from: https://developers.google.com/tag-manager/quickstart (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) } })() ``` -------------------------------- ### Configure GTM Original Referrer Variable Source: https://github.com/vtex-apps/google-tag-manager/blob/master/docs/README.md This configuration step outlines how to create the `originalReferrer` user-defined variable in Google Tag Manager. This variable captures the initial referrer and is essential for maintaining consistent campaign data across user sessions, avoiding discrepancies in Google Analytics. It requires setting the Data Layer Variable Name to `originalReferrer` and its default value to `{{Referrer}}`. ```GTM Configuration {{Referrer}} ``` -------------------------------- ### Configure GTM Original Location Variable Source: https://github.com/vtex-apps/google-tag-manager/blob/master/docs/README.md This configuration step details how to create the `originalLocation` user-defined variable in Google Tag Manager. This variable captures the initial page URL and is crucial for persisting campaign data throughout a user session, preventing inconsistent data in Google Analytics. It involves setting the Data Layer Variable Name to `originalLocation` and its default value to `{{Page URL}}`. ```GTM Configuration {{Page URL}} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.