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

I was stumped. After hours googling phrases like "Google Analytics two tracking codes on same page," I had gotten nowhere. Could it be that big a mystery? It was hard to find but easy to accomplish. Here's how.

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.