我想在 Google Analytics 中放置自定义变量,但我对语法有点困惑。
这是谷歌让我放置在我的网站上的内容:-
(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');
这就是我想用于自定义变量的:-
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]);
现在我看到的例子说我必须把自定义变量代码: -
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 Analytics 代码,我可以添加自定义变量吗?
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');