如何将自定义变量放置在新的Google Analytics代码中 [英] How to place Custom Variables in the new Google Analytics Code

查看:99
本文介绍了如何将自定义变量放置在新的Google Analytics代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将自定义变量放置在Google Analytics中,但Ima对于语法有点困惑。



这就是Google让我在我的网站上放置的内容: p>

 (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-xxxxxxx','xxxxx.com');
ga('send','pageview');

这就是我想用于Custom Vars的内容: - $ / b
$ b

  var _gaq = _gaq || []; 
_gaq.push(['_ setAccount','UA-xxxxxx-XX']);
_gaq.push(['_ setCustomVar',1,'age','<?php echo $ _GET [age];?>',1]);
_gaq.push(['_ setCustomVar',2,'gender','<?php echo $ _GET [gender];?>',1]);

现在我看到的例子说我必须将Custom Variables代码设置为: - p>

  var _gaq = _gaq || []; 
_gaq.push(['_ setAccount','UA-xxxxxx-XX']);
_gaq.push(['_ setCustomVar',1,'age','<?php echo $ _GET [age];?>',1]);
_gaq.push(['_ setCustomVar',2,'gender','<?php echo $ _GET [gender];?>',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);
})();

但最后三行看起来似乎是谷歌做过页面浏览的旧方式,现在google什么(第一个发布的代码)是谷歌现在如何做的。你认为这是正确的吗?或者我应该只将这些代码粘贴到谷歌给我的东西上?

所以,简而言之,这是正确的谷歌分析代码,为我添加一个自定义的变量?

/ p>

  var _gaq = _gaq || []; 
_gaq.push(['_ setAccount','UA-xxxxxx-XX']);
_gaq.push(['_ setCustomVar',1,'age','<?php echo $ _GET [age];?>',1]);
_gaq.push(['_ setCustomVar',2,'gender','<?php echo $ _GET [gender];?>',1]);
_gaq.push(['_ trackPageview']);


(函数(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)
}) (窗口,文件, '脚本', '// www.google-analytics.com/analytics.js','ga');

ga('create','UA-xxxxxxx','xxxxx.com');
ga('send','pageview');


解决方案

您确实混合了2个不兼容的Google Analytics库 - ga.js analytics.js

自定义变量不存在于analytics.js库,您应该使用自定义维度 。如果您在Javascript中定义您的年龄和性别变量,则可以使用以下调用将它们与pageview一起传递:

  <脚本> 

(函数(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 , '脚本', '// www.google-analytics.com/analytics.js','ga');

ga('create','UA-XXXX-Y');
ga('send','pageview',{
'dimension1':age,
'dimension2':gender
});

< / script>

范围(hit / visit / visitor)和变量名称在Google Analytics中定义自定义维度界面 - 不在您的代码中。


I wanna place Custom Variables in Google Analytics but Ima little bit confused about the syntax.

This is what Google gave me to place on my site:-

(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-xxxxxxx', 'xxxxx.com');
  ga('send', 'pageview');

This is what I want to use for Custom Vars:-

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
  _gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
  _gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);

Now the example I saw says that I've to put the Custom Variables code as :-

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
  _gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
  _gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 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);
  })();

but it seems that the last three lines are old way of how google did page views, what google now gives (the first posted code) is how google does it now. Do you think it is correct? or should I just paste this code on top of what google gave me?

So, in short is this correct Google Analytics code, for me to add a custom Variables?

  var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
      _gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
      _gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
      _gaq.push(['_trackPageview']);


(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-xxxxxxx', 'xxxxx.com');
      ga('send', 'pageview');

解决方案

You are indeed mixing up 2 incompatible Google Analytics libraries - ga.js and analytics.js.

Custom Variables as such do not exist in the analytics.js library and you should use Custom dimensions instead. If you define your age and gender variables in Javascript, you can then use the following call to pass them along with a pageview :

<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-XXXX-Y');
ga('send', 'pageview', {
 'dimension1':  age,
 'dimension2':  gender
 });

</script>

Scope (hit/visit/visitor) and variable name are defined in the Google Analytics Custom Dimension interface - not in your code.

这篇关于如何将自定义变量放置在新的Google Analytics代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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