Eric Clancy
Eric Clancy

Reputation: 25

Using multiple Google Analytics accounts in the same website

My team is working on a set of tools and pages that require google analytics for us to analyze. We have an Analytics account for our team where we gather our data that we've used for a good while now.

Recently we've had to combine our website with another repository, under another team's website. Because of this, our analytics has ceased to send data to our analytics account. The data is instead sent to the other team's account. The framework their using injects certain code into all pages on the website.

We would like to instead send our event data to our account, and figure out how to properly separate our analytics object from theirs. The code below is an example of what we would like to change in order to allow communication to 2 different accounts. Any help is greatly appreciated.

    (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-2', 'auto');

(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-YYYYYYYY-2', {'name':'b'});


ga('send', 'event', 'TheirAnalytics', 'TheirEvent');
ga('send', 'event', 'MyAnalytics', 'MyEvent');

Upvotes: 1

Views: 3286

Answers (2)

Philip Walton
Philip Walton

Reputation: 30431

The two existing answers on this page are not correct and will actually not work. You definitely do not need to download the analytics.js script twice, and you definitely do not want to create two command queue functions.

What you do want to do is create two tracker objects, give at least one of them a name, and then send data using both.

Here's an example:

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

// Creates the first tracker for one of your accounts,
// and uses the default name.
ga('create', 'UA-XXXXX-1', 'auto');

// Creates the second tracker for the other account,
// and give it the name "secondary"
ga('create', 'UA-XXXXX-2', 'auto', 'secondary');

// Sends events from both trackers,
// i.e. one event hit will be sent to each account.
ga('send', 'event', 'MyAnalytics', 'MyEvent');
ga('secondary.send', 'event', 'TheirAnalytics', 'TheirEvent');

</script>

Take a look at the documentation on working with multiple trackers and using named trackers for more details.

Upvotes: 6

Xposedbones
Xposedbones

Reputation: 597

change the last (window,document,'script','//www.google-analytics.com/analytics.js','ga'); to (window,document,'script','//www.google-analytics.com/analytics.js','ga2');

and use ga2('send', 'event', 'MyAnalytics', 'MyEvent');

Upvotes: -1

Related Questions