Google Analytics 自定义事件不起作用,因为它未在 Google Analytics 统计信息 View 中进行跟踪。我为 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>
最佳答案
这是因为您使用的是用于经典分析的旧语法 gaq.push
。您实际上已经实现了称为 Universal Analytics 的更新版本的 GA,它使用不同的语法来发送事件。
要使用的正确代码,根据 Google Developer information ,应该这样写:
ga('send', 'event', 'category', 'action', 'label', value);
所以,使用你的例子,我会这样写:
ga('send', 'event', 'Popup', 'Click','Step1');
Event、category 和action 是强制性的,而 label 和 value 是可选的。
关于javascript - 如何使用 Google Analytics 添加自定义事件跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360777/