gpt4 book ai didi

javascript - 如何在新的 Google Analytics 代码中放置自定义变量

转载 作者:数据小太阳 更新时间:2023-10-29 04:29:05 25 4
gpt4 key购买 nike

我想在 Google Analytics(分析)中放置自定义变量,但我对语法有点困惑。

这是 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');

这是我想用于自定义变量的内容:-

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 给我的代码之上?

那么,简而言之,这是正确的 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');

最佳答案

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

analytics.js 库中不存在此类自定义变量,您应该使用 Custom dimensions反而。如果您在 Javascript 中定义年龄和性别变量,则可以使用以下调用将它们与综合浏览量一起传递:

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

范围(点击/访问/访客)和变量名称在 Google Analytics Custom Dimension 中定义界面 - 不在您的代码中。

关于javascript - 如何在新的 Google Analytics 代码中放置自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com