Skip to content

Commit

Permalink
feat: add gtm environment config
Browse files Browse the repository at this point in the history
Corresponds to this change in the rudder-integrations-config repo:
rudderlabs/rudder-integrations-config#1894
  • Loading branch information
tmchow committed Feb 6, 2025
1 parent 7952ad3 commit f611aa3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class GoogleTagManager {
}
this.analytics = analytics;
this.containerID = config.containerID;
this.environmentID = config.environmentID;
this.authorizationToken = config.authorizationToken;
this.name = NAME;
this.serverUrl = config.serverUrl;
({
Expand All @@ -25,7 +27,7 @@ class GoogleTagManager {
}

init() {
loadNativeSdk(this.containerID, this.serverUrl);
loadNativeSdk(this.containerID, this.serverUrl, this.environmentID, this.authorizationToken);
}

isLoaded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LOAD_ORIGIN } from '@rudderstack/analytics-js-common/v1.1/utils/constants';

function loadNativeSdk(containerID, serverUrl) {
function loadNativeSdk(containerID, serverUrl, environmentID, authorizationToken) {
const defaultUrl = `https://www.googletagmanager.com`;
// ref: https://developers.google.com/tag-platform/tag-manager/server-side/send-data#update_the_gtmjs_source_domain

Expand All @@ -11,9 +11,12 @@ function loadNativeSdk(containerID, serverUrl) {
const f = d.getElementsByTagName(s)[0];
const j = d.createElement(s);
const dl = l !== 'dataLayer' ? `&l=${l}` : '';
const gtmEnv = environmentID ? `&gtm_preview=env-${environmentID}` : '';
const gtmAuth = authorizationToken ? `&gtm_auth=${authorizationToken}` : '';
const gtmCookies = '&gtm_cookies_win=x';
j.setAttribute('data-loader', LOAD_ORIGIN);
j.async = true;
j.src = `${window.finalUrl}/gtm.js?id=${i}${dl}`;
j.src = `${window.finalUrl}/gtm.js?id=${i}${dl}${gtmAuth}${gtmEnv}${gtmCookies}`;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', containerID);
}
Expand Down

0 comments on commit f611aa3

Please sign in to comment.