Google Analytics通用事件跟踪

Oak*_*Oak 1 google-analytics

我们正在使用新的Google Analytics通用代码,但我们的事件跟踪代码无法正常运行.关于什么是错的任何想法?谢谢.

<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>
Run Code Online (Sandbox Code Playgroud)

Google Analytics中不会显示任何事件.

Ste*_*n V 7

这是因为Google Analytics跟踪代码是异步的.用户在Google Analytics Javascript代码可以ping分析服务器之前离开页面.

要解决此问题,您需要在新窗口中打开出站链接,或设置点击回调,这将确保在导航之前发生"点击".

<a onclick="ga('send', 'event', 'button', 'click', 'label', { 'hitCallback': function() { document.location = 'http://outbound.com'; }}); return false;" href="http://outbound.com">Click Here</a>
Run Code Online (Sandbox Code Playgroud)

示例代码允许您使用和不使用javascript导航网站,搜索引擎将看到链接.