Google Analytics Universal Event Tracking

僤鯓⒐⒋嵵緔 提交于 2019-12-21 03:01:09

问题


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>

No events show up in Google Analytics.


回答1:


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.

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>

The example code would allow you to navigate the site with and without javascript, and search engines will see the link.




回答2:


<a onclick="ga('send', 'event', 'link', 'click', 'label', {'hitCallback': function() {document.location = 'http://outbound.com'}});">Click Here</a>


来源:https://stackoverflow.com/questions/20403853/google-analytics-universal-event-tracking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!