javascript 如何使用 Google Analytics 添加自定义事件跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20360777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a custom event tracking with Google Analytics
提问by user2962526
Google Analytics custom event not working, as it is not tracked on Google Analytics statistic View. I written code for onclick, but it is not tracked. Here is my code:
谷歌分析自定义事件不起作用,因为它没有在谷歌分析统计视图中跟踪。我为 onclick 编写了代码,但它没有被跟踪。这是我的代码:
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-42651041-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-42651041-1', '1800accountant.com');
ga('send', 'pageview', '/Step1');
</script>
<script type="text/javascript">
function test(){
_gaq.push(['_trackEvent','Popup','Click','Step1']);
alert("GA Code executed");
}
</script>
<p onClick="return test()" style="cursor:pointer;">Click Here</p>
回答by MrSponge
This is because you are using the old syntax gaq.push
, which is for the Classic Analytics. You have actually implemented the newer version of GA called Universal Analytics, which uses a different syntax for sending events.
这是因为您使用的gaq.push
是用于经典分析的旧语法。您实际上已经实现了称为 Universal Analytics 的较新版本的 GA,它使用不同的语法发送事件。
The correct code to use, according to the Google Developer information, it should be written like this:
正确使用的代码,根据谷歌开发者资料,应该是这样写的:
ga('send', 'event', 'category', 'action', 'label', value);
So, using your example, I'd write it something like this:
所以,使用你的例子,我会写这样的:
ga('send', 'event', 'Popup', 'Click','Step1');
Event, categoryand actionare mandatory, while label and value are optional.
Event、category和action是必需的,而 label 和 value 是可选的。