问题
I have a website, and it already has the following tracking code:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Now I need to add tracking to another account. Is the following code right? (I just inserted a line for UA-yyyyyyyy-1 without changing anything else)
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_setAccount', 'UA-yyyyyyyy-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Update
Followed this link: How to track multiple accounts using NEW analytics.js?
Created similar code as follows. It seems working. Any possible issue?
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxxxxx-1', 'auto');
ga('create', 'UA-yyyyyyyyy-1', {'name':'b'});
ga('send', 'pageview');
ga('b.send', 'pageview');
</script>
回答1:
No, your code will track only for UA-yyyyyyyy-1
property. Change the code like below:
var _gaq = _gaq || [];
_gaq.push(['_trackPageview','UA-xxxxxxxx-1']);
_gaq.push(['_trackPageview','UA-yyyyyyyy-1']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
This will send the two pageview hits, one to UA-yyyyyyyy-1
and other to UA-xxxxxxxx-1
.
Debug / Validate
The code is very easy to validate. Steps for Chrome browser:
- Right click on any web page, inspect.
- Go to console tab, paste the above code and press enter.
- Go to network tab, and you will see two separate requests (__utm.gif) sent for
UA-yyyyyyyy-1
andUA-xxxxxxxx-1
.
来源:https://stackoverflow.com/questions/44205845/is-my-code-for-correct-for-having-two-accounts-for-the-same-website