Setting a Dynamic Macro in Google Tag Manager

Tag: google-analytics , google-tag-manager Author: davidzeng0716 Date: 2013-06-16

I have two types Universal Analytics (UA) accounts: a rollup property ID that goes on all subdomains, and a subdomain-specific ID that only appears on that subdomain. There are a total of 7 subdomain properties.

I was wondering, instead of setting up 2 tags (ecomm transaction tag and the regular UA tag) for each subdomain/property - a total of 16 - if I could set up just those two tags and have the UA-ID dynamically changed for the corresponding property.

Example: set up the tag with the id as "UA-XXXXXXXX-{{UA ID}}" where "{{UA ID}}" is defined by me and set with a rule depending on what the current subdomain is. Somewhere - without asking the devs to create a new variable pair - have property one associated to "1", property two associated to "2" and so forth, so that the right tracker is fired on the right subdomain.


Best Answer

I did this via the following workaround:

  • I created a custom html tag that loads when the DOM is ready ( rule "{{event}} == gtm.dom)
  • inside that custom tag I put some javascript to determine the subdomain (or whatever dynamic property) and set a variable - let's call it "ua" - accordingly
  • inside the same tag I push the ua variable and a custom event to the datalayer :

    dataLayer.push({ 'ua':ua, 'event': 'INIT' });

  • I created a macro "UA ID" that gets it's value from the push variable above

  • I created a rule "on init" ( {{event}} == INIT
  • I used that rule to fire all subsquent tags (so they would be executed only after the UA variable has been set)

So far (i.e. for the last 2 months) this is working rather well, so it might work for you, too.


That sounds pretty good, would you mind sharing that script to determine the subdomain or property? Also, should integers not be enclosed within single quotes for the dataLayer or was that a typo?
I have an url macro called "hosts" thats set to "hostname". After that it's simple js: if({{hosts}} == "") { // do something }} else {{ // do something else }}. And yes, values in the datalayer should always be quoted.
I just gave it a shot, and it works perfectly. Thanks Eike!