我想在谷歌分析中放置自定义变量,但我对语法有点困惑.
@H_404_2@这就是Google给我放在我的网站上的内容: –
(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');@H_404_2@这就是我想用于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]);@H_404_2@现在我看到的例子说我要将自定义变量代码设置为: –
var _gaq = _gaq || []; _gaq.push(['_setAccount',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); })();@H_404_2@但似乎最后三行是谷歌如何进行页面浏览的旧方式,谷歌现在提供的(第一个发布的代码)是谷歌现在如何做到的.你认为这是对的吗?或者我应该将此代码粘贴到谷歌给我的顶部? @H_404_2@那么,简而言之就是这个正确的Google Analytics代码,我可以添加自定义变量吗?
var _gaq = _gaq || []; _gaq.push(['_setAccount','UA-xxxxxx-XX']); _gaq.push(['_setCustomVar',1]); _gaq.push(['_setCustomVar',1]); _gaq.push(['_trackPageview']); (function(i,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},m) })(window,'ga'); ga('create','xxxxx.com'); ga('send','pageview');
解决方法
您确实混合了2个不兼容的Google Analytics库 –
ga.js和
analytics.js.
@H_404_2@analytics.js库中不存在自定义变量,您应该使用Custom dimensions.如果您在Javascript中定义年龄和性别变量,则可以使用以下调用将它们与网页浏览一起传递:
<script> (function(i,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},m) })(window,'ga'); ga('create','UA-XXXX-Y'); ga('send','pageview',{ 'dimension1': age,'dimension2': gender }); </script>@H_404_2@范围(匹配/访问/访问者)和变量名称在Google Analytics Custom Dimension界面中定义 – 而不是在您的代码中.