Search code examples
javascriptgoogle-analyticsanalyticsgoogle-analytics-apiuniversal-analytics

How to properly use multiple tracking IDs in Google Analytics


My current setup for Google Analytics looks like this:

ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');

I read this article explaining how to track data when using more than 1 tracking ID:

https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers

So using the examples in the link, I need to add the following lines of script:

ga('create', 'UA-XXXXXXXX-2', 'auto', 'myTracker2');
ga('myTracker2.send', 'pageview');

What do I do with displayfeatures? Do I need to prepend myTracker2 to it as well? Something like:

ga('myTracker2.require', 'displayfeatures');

Solution

  • as per developer pages:

    Using Multiple Trackers

    To use the display features plugin with multiple trackers, prepend the require call with the tracker name, as in this example:

    // create a tracker named 'foo' for property UA-XXXXX-Y
    ga('create', 'UA-XXXXX-Y', {name: 'foo'});
    ga('foo.require', 'displayfeatures');
    ga('foo.send', 'pageview');
    
    // create a second tracker named 'bar' for a different property UA-XXXX-Z
    ga('create', 'UA-XXXXX-Z', {name: 'bar'});
    ga('bar.require', 'displayfeatures');
    ga('bar.send', 'pageview');
    

    Loading the display features plugin for a named tracker will result in the tracker name being appended to the cookie name. The above example would create the cookies _gat_foo and _gat_bar.

    Link to source: https://developers.google.com/analytics/devguides/collection/analyticsjs/display-features

    I hope this helps.