5

I need to split collecting data for two GA accounts, let`s name them UA-XXXXXXX-1 and UA-XXXXXXX-2. To implement this, I used example code from https://developers.google.com/analytics/devguides/collection/gajs/ (under "Pushing commands to multiple trackers also works" text) and here is my code:

    _gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
    _gaq.push(['_trackPageview']);
    _gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);

    _gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
    _gaq.push(['second._trackPageview']);
    _gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);

It is working, but I have both custom vars in both accounts. What I really need, is to track customVar1 only for UA-XXXXXXX-1 account, and customVar2 only for UA-XXXXXXX-2 account. Any ideas how to implement this?

Memfis
  • 73
  • 8
  • Have you considered migrating to Universal Analytics and using the new analytics.js api? Multiple Trackers use case is not well supported in Classic Analytics but it is in Universal. – Eduardo May 08 '14 at 04:09
  • @Eduardo is this even possible with Universal Analytics? – Linda Lawton - DaImTo May 08 '14 at 10:35
  • @Eduardo, I hope there is a solution with classic analytics, because it would be better not to change all GA code on our project) but we will look to universal analytics and check if there is such functionality, thanks for reply! – Memfis May 11 '14 at 03:10

1 Answers1

8

First of all, _setCustomVar must come before _trackPageview.

Now to your problem:

This happens because User level custom vars are stored in the cookie. Since both your trackers share the same cookie the second tracker will be sent with the vars set on the first tracker.

You have 3 options.

1) Go With Universal Analytics

The right path here is to use Universal Analytics. Multi-tracking is not officially supported in Classic because it's buggy, as you probably noticed. And things are easy to break.

On Universal all custom dimensions are evaluated server side so this setup is supported. No data is stored on cookies for Custom Dimensions.

eg: Provided you configured dimension1 on UA-XXXXXXX-1 and dimension2 on UA-XXXXXXX-2 through the Admin interface.

ga('create', 'UA-XXXXXXX-1', 'auto');
ga('send', 'pageview', {
  'dimension1': 'cv1'
});

ga('create', 'UA-XXXXXXX-2', 'auto', {'name': 'newTracker'});
ga('newTracker.send', 'pageview', {
  'dimension2': 'cv2'
});

More info:

2) Keep Classic Analytics but, use session level customVars

If you definitively can't move to Universal Analytics and want to keep using Classic you can get around this issue by just using Session Level Custom Vars. To make it work you would only need to change the scope of the custom Var as seen below (from 1 to 2).

Unlike User scoped Custom Vars, Session Scoped CVs don't get stored on the cookie. So you will get around this issue. The downside is that the value will only be valid for that session, not future sessions from the same user.

_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 2]);
_gaq.push(['_trackPageview']);


_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 2]);
_gaq.push(['second._trackPageview']);

3) Keep Classic and User scoped CVs but use different cookies per tracker

You can configure GA to create 2 sets of cookies, one for each tracker one at the root domain and one at the subdomain.

If your site is: http://www.example.net setup your trackers like this:

_gaq.push(['_setAccount', 'UA-XXXXXXX-1']);
_gaq.push(['_setDomainName', 'example.net']);
_gaq.push(['_setCustomVar', 1, 'customVar1', 'cv1', 1]);
_gaq.push(['_trackPageview']);


_gaq.push(['second._setAccount', 'UA-XXXXXXX-2']);
_gaq.push(['second._setDomainName', 'www.example.net']);
_gaq.push(['second._setCustomVar', 2, 'customVar2', 'cv2', 1]);
_gaq.push(['second._trackPageview']);

This MUST to be done in all pages of your site. Not only this one. This will make sure that each tracker uses it's isolated cookieset and customVars won't leak from one to another.

Notice that if your site can be accessed without www.. eg: http://example.net/ this will fail and there is no workaround. You can't create 2 sets of cookies with the same name in the same domain and path. You just can't.

Also if you use _gaq.push(['_setDomainName', 'none']); or _gaq.push(['_setAllowHash', false]);, the above trick won't work and cookies will conflict. Your data will be weird. Just don't do it. You've been warned.

I can't stress enough that this is provided without guarantees and if your data breaks it's on you. Multiple trackers are tricky and that's why it was never officially supported.

More info:

Igor Milla
  • 2,767
  • 4
  • 36
  • 44
Eduardo
  • 22,574
  • 11
  • 76
  • 94
  • 1
    great answer. I always love it when Googlers are willing to spread there wealth of knowledge. :) – Linda Lawton - DaImTo May 13 '14 at 06:37
  • 1
    @Eduardo Thank you for such detailed response! We used #2 and it works perfect! – Memfis May 14 '14 at 07:15
  • @Memfis, it is highly recommended that you "bite the bullet" and upgrade to Universal Analytics. Google has probably already pushed your property into UA, and you are using old code that will be deprecated once they finish the transfer. It's not hard to set up Custom Dimensions. I actually feel they're easier to implement than Custom Variables. – Sean Kendle May 22 '14 at 19:44
  • @Eduardo, I hope you can help me with my own related issue with UA. Please see my question here: http://stackoverflow.com/questions/23815965/universal-analytics-custom-dimensions-with-multiple-trackers-old-tracker-recor – Sean Kendle May 22 '14 at 20:04
  • @Eduardo, I know this question is already old, but I can't seems find answer anywhere to my problem. Can you please take a look at my UA problems? I feel that the solution is easy, but I can't find any answer for that on the internet. I have read your answer on this page, but it doesn't quite answer my problem. Thank you. http://stackoverflow.com/questions/29007427/google-analytics-multi-tracking-account-dimension-data-partially-not-showing-in – Chen Li Yong Mar 12 '15 at 11:09