Menu

Google Analytics: Two Tracking Codes on Same Page – 2014 Analytics.js Version

Before we start…

If you’re fighting with Google Analytics, you’re obviously smart and technical. If you have any interest in digital marketing and SEO, you might like my SEO company in Central MA, Mxt Media.

Check us out! I’d really appreciate your feedback. Thanks. Now onto the good stuff:

Two GA Tracking Codes on Same Page

The code you’re looking for is right here. You’ll need two ga(create) functions and two ga(send) functions. The second of each is slightly different than the first.

// Standard code. Replace X's and domain with your own
ga('create', 'UA-XXXXXXXX-X', 'domain.com');

// Replace Y's with your account number. "b" can be anything you want.
ga('create', 'UA-YYYYYYYY-Y', {'name':'b'});

// Standard code. No change necessary.
ga('send', 'pageview');

// "b" can again be anything you want, but it MUST match "b" above.
ga('b.send', 'pageview');

Add this code to your Google Analytics tracking code <script> tags. You’ll already have the standard create/send lines, provided by Google. Replace them with the example above and remember to replace all those X’s and Y’s with your proper Google Analytics UA tracking code numbers.

Tracking in two places means defining two tracking objects. Per Google’s API/documentation, if you use more than one tracking object, all but the first need a name. That’s the part I was missing.

Ok, but what does it mean? Multiple Google Analytics tracking objects.

In order to track data from one page in two different places, Google Analytics has to have a tracking object for each web property (I’ve mistakenly called these “accounts” before. Not the same.) Hence the double ga(create) functions. That tells GA to initialize two reports if you will—they’re actually called “tracker objects.”

The ga(create) lines will create two tracker objects. The first tracker will be the default tracking object. Therefore, it does not need a name. The second tracker will have the name of b. Again, I’m a relative noob, so I’m not sure why one needs a name and the other doesn’t. But so says the mighty Goog.

Now we have to send the data. To send a pageview using both trackers, specify the name of the second tracker in the beginning of the ga(send) command. We’ve created a tracker named b, so our syntax for sending “b”‘s data is ga('b.send', 'pageview');

Why it was hard to find

Google updates their analytics code and tracking system periodically. It has improved over time and become much easier to work with. Older versions were harder to use. And therefore, more people has questions and more people published those questions and their answers.

Fast-forward to 2014, and searching for things like “google analytics two tracking codes on same page” brings up A LOT of results from 2010 an 2011. Helpful in theory, perhaps. But when the API and tracking code are totally different, those answers don’t solve the problem.

Thanks to this post on Stack Overflow for ultimately solving the mystery. The original question links back to the dev guide, under Advanced Configuration – Web Tracking (analytics.js). But frankly, I found the dev guide hard to follow. Maybe I’m too much of an Analytics noob, but the documentation never helped me much. I hope my answer helps you!