Setting a Dynamic Macro in Google Tag Manager

2019-04-15 02:49发布

问题:

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.

Thanks!

回答1:

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.



回答2:

Actually I think it will be a lot easier for you if create a lookup table with hostname as a base. Then just add the UA properties for all of your subdomains, each on new row in the lookup table. Afterwards you just use this lookup table as the variable for your UA:

It's a cleaner option to manage a lot of GA properties.