问题描述
我们正在使用新的 Google Analytics Universal 代码,但我们的事件跟踪代码似乎无法正常工作.关于什么是错的任何想法?谢谢.
We are using the new Google Analytics Universal code, but our event tracking code does not seam to be working. Any ideas on what is wrong? Thank you.
<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', 'mywebsite.com');
ga('send', 'pageview');
</script>
<a href="http://www.outbound.com" onclick="ga('send', 'event', 'link', 'click', 'label');">Click Here</a>
Google Analytics 中未显示任何事件.
No events show up in Google Analytics.
推荐答案
这是因为 Google Analytics 跟踪代码是异步的.用户在 Google Analytics Javascript 代码可以 ping 分析服务器之前离开页面.
This is because the Google Analytics tracking code is asynchronous. The user leaves the page before Google Analytics Javascript code can ping the Analytics server.
要解决此问题,您需要在新窗口中打开出站链接,或设置 hit callback,这将确保 Analyticshit"发生在导航之前.
To fix this, you need to either open the outbound link in a new window, or set a hit callback, which will ensure the Analytics "hit" happens before the navigation.
<a onclick="ga('send', 'event', 'button', 'click', 'label', { 'hitCallback': function() { document.location = 'http://outbound.com'; }}); return false;" href="http://outbound.com">Click Here</a>
示例代码将允许您使用和不使用 JavaScript 浏览网站,搜索引擎将看到链接.
The example code would allow you to navigate the site with and without javascript, and search engines will see the link.
这篇关于谷歌分析通用事件跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!