javascriptgoogle-analyticstrackinganalytics.js

Google analytics analytics.js 2 two trackers colliding?


I am using the analytics.js script to track 3 sites - two 2nd level domains and one is a subdomain... for example: dom1.com, sub.dom1.com, dom2.com

The script and site I have trouble with is the dom1.com in this example. I have 2 properties which should aggregate the following data:

Property A : dom1.com + sub.dom1.com Property B : dom1.com + sub.dom1.com + dom2.com

I have been looking for a bug in my code but can't figure out the problem, which is:

In Property A everything is aggregated correctly and both the domain and subdomain are sending their data fine. In Property B sub.dom1.com and dom2.com do send their data but dom1.com does not.

This is the script in the header of dom1.com:

ga('create', 'PropertyA', 'auto', {'name': 'trackerA'}, {'allowLinker': true});
ga('trackerA.send', 'pageview');
ga('require', 'linker');
ga('linker:autoLink', ['sub.dom1.com', 'dom1.com']);

ga('create', 'PropertyB', 'auto', {'name': 'trackerB'}, {'allowLinker': true});
ga('trackerB.send', 'pageview');
ga('require', 'linker');
ga('linker:autoLink', ['sub.dom1.com', 'dom1.com', 'dom2.com']);

I have tried moving the second tracker above in case the script is not run, but it didn't solve anything. I figure the problem is somewhere in the way I try to use the linker 2 times, but maybe do it wrong?

Thank you for any help, hope it helps someone else too.


Solution

  • There are a couple things wrong with your implementation (not all of these are contributing to your problem, but they are still generally best-practices):

    1. In general, you should always require all plugins and invoke plugin initialization before doing anything else with a tracker since many plugins alter the behavior and/or data stored on the tracker(s).

    2. You do not need to specify subdomains in the autoLink method, they get tracked automatically since you're using auto to enable automatic cookie domain configuration.

    3. You cannot pass two objects to the create method, instead you should combine those options into a single object or use the shorthand (e.g. ga('create', trackingID, cookieDomain, trackerName, additionalConfigOptions);

    4. When using multiple trackers, you must specify the tracker name when requiring plugins and invoking plugins methods (e.g. ga('trackerName.require', 'pluginName'); and ga('trackerName.pluginName:methodName', methodOptions);)

    If you update your code as follows, it should work:

    ga('create', 'PropertyA', 'auto', 'trackerA', {'allowLinker': true});
    ga('trackerA.require', 'linker');
    ga('trackerA.linker:autoLink', ['dom1.com']);
    ga('trackerA.send', 'pageview');
    
    ga('create', 'PropertyB', 'auto', 'trackerB', {'allowLinker': true});
    ga('trackerB.require', 'linker');
    ga('trackerB.linker:autoLink', ['dom1.com', 'dom2.com']);
    ga('trackerB.send', 'pageview');