TJ Kelly

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

[blog_stripe]

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:

[/blog_stripe]

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.

[blog_stripe]

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');

[/blog_stripe]

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!

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

    1. TJ Kelly

      Google mentions different account owners for different sections of a site (marketing vs. IT vs. sales, perhaps?). In my case, I goofed and swapped UA numbers a few months back. Now tracking both old and new UAs.

  1. Matt

    Is it not better, especially where affiliates are involved to just add a new ‘User’ to the ‘Property’ or ‘View’ with their email address. That way you can filter what they can view and don’t have to keep updating JavaScript in the source files?

    Reply
  2. Frederik

    Thanks a lot, TJ! The official Google Analytics Help states it is possible to “install multiple instances of the tracking code on a web page” but doesn’t bother explaining how … Problem solved!

    Reply
    1. TJ Kelly Post author

      Hi Jack. Yes you can. The process is the same for adding a second, third, and 300th set of tracking codes.

      You’ll need two ga(create) functions and two ga(send) functions.

      becomes, to use 10 sets of tracking codes for example:

      You’ll need 10 ga(create) functions and 10 ga(send) functions.

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

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

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

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

    2. Jaya Pratha

      Hi Kelly,

      I have redirected site from abc.com to xyz.com and remove abc.com from live. But the bothe are same site, i removed abc.com site from live. I have Google analytics code for abc.com, now i want to track xyz.com. Shall i create new GA for xyz.com site?

    3. TJ Kelly

      I’m not actually an analytics expert, so please ask some other people too. But I think the answer is NO you don’t have to create a new account or property. As far as I know, the same GA code you used on abc.com will continue to work just fine on xyz.com.

    4. Jaya Pratha

      Thank you..

      And i have one more doubt in Webmaster. abc.com has submitted already in webmaster, but now i want to give xyz.com to webmaster or not?

  3. nate

    The Stackoverflow link you gave shows an extra parameter of ‘auto’ in the second tracking code, though, ie:

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

    Is this just not necessary or did it cause you problems. What caused you to drop the ‘auto’?

    Thanks!

    Reply
    1. TJ Kelly Post author

      Thanks for your comment, Nate. Anything after the second parameter is optional, according to Google’s docs.

      Honestly, your question caught me by surprise. I don’t think the 3rd param was listed when I wrote this almost 2 years ago. When I saw your question, I panicked that I had been doing it wrong. Instead, I think Google has updated their API a bit and it now accepts (and therefore lists) new params.

      I can confirm that my analytics are still working properly without adding the new param. I’m sure it could be useful for Analytics power users, but that’s not me. Barebones is just fine for my use case.

      Thanks again for asking. Hope my article and answer are helpful.

  4. nate

    Thank you for responding (and quickly, too!). Do you think I should be concerned about this error message the client has now relayed to me (I have anonymized the tracking codes below for public consumption, I didn’t really add them to my code this way):

    I just checked now, and it does seem to be working! However, when I logged in, I received this message. Not sure of the significance, if it’s in fact working. I’ll check back to keep an eye on it. Error message:

    The default URL http://yourdomain.com, for property UA-YYYYYYYY-Y [the additional second tracker] has no tracking code for this property, although it does have tracking codes for other properties:

    UA-XXXXXXXX-X [the original first tracker]

    If your Tracking ID and your Default URL setting are out of sync, you don’t get valid data in your Analytics account for the Default URL.

    Either update the tracking code to use the Tracking ID for property alvr.com, or open the Property Settings to update the Default URL for property alvr.com to be http://yourdomain.com


    Is that normal?

    Reply
    1. TJ Kelly Post author

      Hi Nate. I’m sorry, I have absolutely no idea. I’m far from an expert at Analytics. I don’t think I can be of any help. Have you tried the r/Analytics community on Reddit? Ask your question there. They should be able to help you.

  5. Aidan McNally

    Why do you need multiple tracking codes?

    I’m trying to find the answer after seeing different tracking codes on some sites, and wondered whether it was intentional or not.

    Reply
    1. TJ Kelly Post author

      Thanks for your comment, Aidan. The most common use-case I’ve seen is for businesses that employ multiple vendors. If you have one company running PPC campaigns and another doing SEO work, they might each need to track analytics totally independently from each other. It’s rare, I know, but it works!

  6. Eshwaren M

    GA keep ignoring the 3rd line.
    This method works for me:

    ga(‘create’, ‘UA-70252731-1’, ‘auto’);
    ga(‘send’, ‘pageview’);
    ga(‘create’, ‘UA-41234210-1’, ‘auto’, ‘newTracker’);
    ga(‘newTracker.send’, ‘pageview’);

    Reply
  7. brian mathers

    I came across your post this evening and welcomed what I was reading. I have these same frustrating search problems you do at times here also. And this was a search that I needed an answer too. The reason I want to have two tracking codes (and correct me if I am taking the wrong approach) I have a website that used a domain name and google analytics tracking code on it. Later a new website was built but also it was given a new brand domain name. However, people would still remember the old brand and we may use that domain for some specific adwords campaigns. So, I wanted the ability to see how the old domain would perform over in adwords and pitch it against the new domain that was organically supporting the website. The old domain currently 301 redirects to the new website domain

    Reply
    1. TJ Kelly

      Thanks for your comment, @brianmathers:disqus. I don’t think you need a second property for this. I think you’re just going to end up with duplicates of the same tracking data.

      One (the old domain/GA tracking code) will have historical data going back to when you first installed it. The second (new domain/new GA tracking code) won’t have historical data.

      But looking at today’s data, I’d expect old and new tracking codes to be showing identical traffic data.

      Therefore, I see no need for the new one. Keep the old one that has the historical data on it.

      The exception to this is the 301 redirect. It might be stripping some referrer or other data. In which case, you’ve got it set up properly.

      But again: I’m not a GA expert! Please ask around :)

  8. eSparkBiz

    Very Informative Topic, Kelly
    I want to ask 1 question about my website.
    From last 7 years, I am targeting my website in India google.co.in
    But, Now I want to target as global google.com
    Can I add new analytic code to my site?
    Please help me how I can get rank in global?

    Reply
    1. TJ Kelly

      Yes, you can add a second analytics tracking code using this method. But it won’t help with ranking in google.com. That’s an SEO question, and the data in your analytics should be used to help you determine how to rank!

  9. Chethanika_ devini

    I have added another analytic account for my website and the code is fired through tag manager. However I am getting different data from the 2 analytic accounts. I’ve checked all the filters and made sure that the both accounts have same filters but the data is different from each other. Can you please explain why this is happening?

    Reply
    1. TJ Kelly

      That could be the difference, then. I don’t have a ton of experience with Tag Manager, but if one tracking code runs on Tag Manager and the other runs on analytics.js embedded code, that could account for some differences.

      Have you checked to see if any of your own traffic is represented? I.e. are you filtering out your own visits to the site? Tag Manager may automatically filter out some traffic that the embedded version doesn’t (but I can’t confirm that…just speculation).

  10. Peter Munkholm

    I needed this to both create a new property for a completely new website design, and keep some historical data updated in a new property.
    Thank you very much!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *