我的代码是否适用于拥有同一网站的两个帐户? [英] Is my code for correct for having two accounts for the same website?

查看:99
本文介绍了我的代码是否适用于拥有同一网站的两个帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,它已经有了以下跟踪代码:

  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 =文档.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();

现在我需要为其他帐户添加跟踪功能。以下代码是否正确? (我只是为UA-yyyyyyyy-1插入了一行,而没有改变任何东西)

  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 =文档.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();

谢谢!

更新

按照以下链接:如何使用新的analytics.js跟踪多个帐户?

创建类似的代码如下。它似乎工作。任何可能的问题?

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


解决方案

不,您的代码只会跟踪 UA-yyyyyyyy-1 属性。更改下面的代码:

  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 =文档.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga,s);
})();

这会发送两次综合浏览量命中,一次为 UA-yyyyyyyy-1 和其他转换为 UA-xxxxxxxx-1






调试/验证
$ b

代码非常容易验证。
Chrome浏览器的步骤:




  • 右键点击任何网页,检查。

  • 转到控制台选项卡,粘贴上面的代码并按回车键。

  • 转到网络选项卡,您将看到两个单独的请求(__utm.gif)发送给 UA-yyyyyyyy-1 UA-xxxxxxxx-1


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);
  })();

Thanks!

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>

解决方案

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 and UA-xxxxxxxx-1.

这篇关于我的代码是否适用于拥有同一网站的两个帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆