Skip to content

Commit

Permalink
Merge pull request #292 from rudderlabs/gtm-traits-support
Browse files Browse the repository at this point in the history
Added traits support to GTM
  • Loading branch information
arnab-p authored Aug 3, 2021
2 parents 46224f0 + b3b8981 commit c2a80a9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions integrations/GoogleTagManager/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ class GoogleTagManager {
w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
const f = d.getElementsByTagName(s)[0];
const j = d.createElement(s);
const dl = l != "dataLayer" ? `&l=${l}` : "";
const 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", this.containerID);
}

identify(rudderElement) {
logger.debug("[GTM] identify:: method not supported");
// set the traits to the datalayer and put everything under the key traits
// keeping it under the traits key as destructing might conflict with `message.properties`
const rudderMessage = rudderElement.message;
const props = { traits: rudderMessage.context.traits };
this.sendToGTMDatalayer(props);
}

track(rudderElement) {
Expand All @@ -31,6 +35,9 @@ class GoogleTagManager {
event: rudderMessage.event,
userId: rudderMessage.userId,
anonymousId: rudderMessage.anonymousId,
// set the traits as well if there is any
// it'll be null/undefined before identify call is made
traits: rudderMessage.context.traits,
...rudderMessage.properties,
};
this.sendToGTMDatalayer(props);
Expand Down Expand Up @@ -60,6 +67,9 @@ class GoogleTagManager {
event: eventName,
userId: rudderMessage.userId,
anonymousId: rudderMessage.anonymousId,
// set the traits as well if there is any
// it'll be null/undefined before identify call is made
traits: rudderMessage.context.traits,
...rudderMessage.properties,
};

Expand Down

0 comments on commit c2a80a9

Please sign in to comment.