sleestak
sleestak

Reputation: 65

Passing data from Google Tag Manager to Google Analytics

I have two Google Analytics profiles in my account. One, for my production account with property ID: UA-XXXXXXXX-1. My second profile is for my staging account to test and it has property ID: UA_XXXXXXXX-2.

In tag manager I have my "-1" tag firing if my url includes "mydomain.com" and my "-2" tag firing if url includes "mydomain.it".

The firing of the GA tag with this setup works fine. I have my staging data separate from production and am able to test basic GA pageviews.

What now need is the ability to separate events. Currently, I call setAccount prior to each event tracking (I know this can be in single call).

_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Gone With the Wind']);

What I'd like to do is call the _setAccount method and dynamically pass in the value from Google Tag Manager where I set the property ID depending on the url.

Is there a way to do something like this (or something better) so I can set the account based on a Tag Manager rule?

_gaq.push(['_setAccount', [Tag Manager Property ID] ]);

I appreciate any advice/help you have.

Thank you!

Upvotes: 3

Views: 5295

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32780

IMO passing data from the tag manager to the website, apart from all practical difficulties, would totally defeat the purpose of having asychronous loading code.

You "push" methods on a stack so you can process them when the code has loaded at some undetermined point in the future. If you want to take the account id from the code you'd have to wait until it's loaded, so you'd loose all advantages from asynchronous loading.

As far as I can tell (haven't used tag manager extensively yet) the correct way would be to push the event data to the "data layer" variable :

dataLayer.push({'myevent': 'mylabel'});

and use the variable (like dataLayer.myevent) in the code deployed via the tag manager (so you do the actual event tracking in the domain specific code).

This is described at: https://developers.google.com/tag-manager/devguide

There is actually an event tracking template for Google Analytics in the tag manager. For the event value you can create a new macro and set it to a dataLayer variable. Then add your domain specific rule and you should be all set.

Upvotes: 2

Related Questions