How to add custom event tracking to Google Analytics

The Google Analytics custom event does not work because it is not tracked in the Google Analytics statistical view. I wrote code for onclick, but it is not tracked. Here is my code:

<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> 
+7
javascript html google-analytics
source share
1 answer

This is because you are using the old gaq.push syntax, which is used for classic analytics. In fact, you implemented a new version of GA called Universal Analytics, which uses a different syntax to send events.

The correct code to use, according to Google’s developer information , should be written as follows:

 ga('send', 'event', 'category', 'action', 'label', value); 

So, using your example, I would write it something like this:

 ga('send', 'event', 'Popup', 'Click','Step1'); 

The event , category, and action are required, and the label and value are optional.

+18
source share

All Articles