Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track multiple accounts using NEW analytics.js?

I need to track pageview for two accounts on one page, using Google's new analytics.js. There is plenty of tutorials and examples how to do it with older ga.js. But all I have found was this Analytics documentation page. I have written my code to suit the given example, but it only tracks views for first (default) tracker, but not for the second one.

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXXXX-3', 'domain.com');
  ga('create', 'UA-ZZZZZZZZ-1', {'name':'b'});
  ga('send', 'pageview');
  ga('b.send', 'pageview');
</script>

Anyone has any idea what is wrong with my code ? Looks good to me according to Google's example.

like image 677
Frodik Avatar asked Sep 25 '13 06:09

Frodik


5 Answers

Working with Multiple Tracking Objects

To solve this, you must create a tracking object for each web property to which you want to send data:

ga('create', 'UA-12345-1', 'auto');
ga('create', 'UA-12345-6', 'auto', {'name': 'newTracker'});  // New tracker.

Once run, two tracker objects will be created. The first tracker will be the default tracking object, and not have a name. The second tracker will have the name of newTracker.

To send a pageview using both trackers, you prepend the name of the tracker to the beginning of the command, followed by a dot. So for example:

ga('send', 'pageview');
ga('newTracker.send', 'pageview'); // Send page view for new tracker
like image 127
Dylan Wood Avatar answered Sep 27 '22 13:09

Dylan Wood


Your code for multiple accounts tracking using analytics.js is correct, I have successfully tested a similar code in my site. So you need to check the following for any possible error:

1, Confirm if both tracking data are sent. For example in chrome, use GA debugger plugin for chrome and then in javascript console, see if you are getting the below details for both your tracking ids

adSenseId        (&a)   425734287 
apiVersion       (&v)   1 
clientId         (&cid) xx.xx
encoding         (&de)  UTF-8 
flashVersion     (&fl)  11.8
hitType          (&t)   pageview
javaEnabled      (&je)  1 
language         (&ul)  en-us 
location         (&dl)  domain.com 
referrer         (&dr)
screenColors     (&sd)  24-bit
screenResolution (&sr)  1366x768
title            (&dt)  yourdomaintitle 
trackingId       (&tid) UA-XXXXXXXX-3 
viewportSize     (&vp)  1364x361 

Ideally you should see this as your code is correct and this means your website is correctly sending 2 tracking signals.

2, For your second tracking id, ensure the tracking id is exactly same as the one in your GA web property

3, Ensure you have not applied any filters to the corresponding view inside your web property which may filter out the traffic . Incase you are using some filters, take an unfiltered view and see if you are seeing hits in the realtime overview

like image 32
tony m Avatar answered Sep 28 '22 13:09

tony m


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

This has since been simplified, using a fourth argument - updated by Google on Dec 15, 2015.

ga('create', 'UA-XXXXX-Y', 'auto');
ga('create', 'UA-XXXXX-Z', 'auto', 'clientTracker');
ga('send', 'pageview');
ga('clientTracker.send', 'pageview');
like image 43
grantpr Avatar answered Sep 27 '22 13:09

grantpr


I know this is an old answer, but since I didn't see anyone mention this solution for pushing to both accounts at once, I thought I would share it...

Using multiple trackers is the way to go, but if you want to always push in both accounts, override ga function like this:

ga('create', 'UA-XXXXXXXX-1', {
    'name': 'myCustomTracker',
    'cookieDomain': 'auto'
});
ga('create', 'UA-XXXXXXXX-2', 'auto');

ga(function () { //Wait for Analytics to be fully loaded
    var oldGa = ga;
    ga = function () { //Override ga function to call both trackers
        if (arguments && arguments.length > 0) {
            oldGa.apply(null, arguments);
            arguments[0] = "myCustomTracker." + arguments[0]; //Edit first argument to call second tracker.
            oldGa.apply(null, arguments);
        }
    };

    ga('send', 'pageview'); //Perform page view on both trackers at once.
});

Like this you'll be able to call gafunctions like before, pushing data to both trackers at once!

like image 33
alexbchr Avatar answered Sep 30 '22 13:09

alexbchr


According to the example in Analytics documentation page, aren't you supposed to do:

ga('create', 'UA-XXXXXXXX-3', 'auto');
ga('create', 'UA-ZZZZZZZZ-1', 'auto', {'name':'b'});
// note this last argument            ^^^^^^^^^^^^
like image 30
HenrikB Avatar answered Oct 01 '22 13:10

HenrikB