Question

I don't seem to be able to get Google analytics event tracking to work with the "new" style tracking code.

I cobbled together this code, but it might be wrong:

<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-42805759-5', 'my-domain.com');
 ga('send', 'pageview');

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-42805759-5']);
 _gaq.push(['_trackPageview']);

</script>

And an example link is:

<a href="my-page.aspx" onclick="_gaq.push(['_trackEvent', 'Link click', 'My page clicked', 'Something']);">View this page</a>

Should this work? And also, should tracked events be visible in Real-time > Events area of the dashboard?

Thanks.

Was it helpful?

Solution

You have two problems:

  1. The classic style with _gaq is not interchangeable with the new universal analytics, you will be using ga('send','event', param1, param2...).

  2. If you track an event on click with the classic style code, its unlikely the event will be reported before the page is unloaded. If you set this up correctly with the universal code, it will work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top