Reputation: 478
I have been looking for a while before coming to ask here, because I know this topic has already been covered. I needed to put two different analytics on the same website, so I inserted this code before the closing head tag of every page on my website. I found a solution that seemed to work, by creating two different trackers and sending them separately. But the results don't match : the first tracker shows 70 visits, whereas the second one shows 800, and I set both at the same moment.
(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-40380819-3', 'auto',{'name': 'tracker1'});
ga('tracker1.send', 'pageview');
(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-64657532-1', 'auto',{'name': 'tracker2'});
ga('tracker2.send', 'pageview');
What am I doing wrong here ? If there is a better solution I am opened to it.
Thanks for your help.
Upvotes: 2
Views: 94
Reputation: 1529
(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-40380819-3', 'auto',{'name': 'tracker1'});
ga('tracker1.send', 'pageview');
ga('create', 'UA-64657532-1', 'auto',{'name': 'tracker2'});
ga('tracker2.send', 'pageview');
70 visits from first tracker are there because first async loader is faster than second one and it fires pageview before second tracker overwrites first one. That 800 visits should be almost correct.
Upvotes: 2