javascriptgoogle-analytics

Dual Google analytic codes from two different accounts?


I would like to have dual Google Analytics tracings sent to two different Google Analytics accounts from the same webpage.

I have this Analytics-code today (the standard 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-12345678-9', 'auto');
      ga('send', 'pageview');
</script>

I would like to add tracking to one more account. Can I just add one more "UA-XXXXXXXX-X" to the existing one?

Like so:

<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-12345678-9', 'auto');
      ga('create', 'UA-87654321-0', 'auto');
      ga('send', 'pageview');   
</script>

Would this work properly?

Kind regards Johan Linnarsson


Solution

  • 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 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.

    Kindly refer below links for additional information

    1. Multiple tracking codes on web pages
    2. Ok, but what does it mean? Multiple Google Analytics tracking objects.
    3. Implications