Google Analytics custom event tracking

Google Analytics can be very useful for tracking visitors to your website or other web-enabled applications. The reporting interface enables you to drill-down on your visitors across many dimensions to get a clear picture of the demographics and to analyse visitor patterns (top pages, top referrers, time on page, etc.).

Analytics also enables you to track your own custom events. So, for example, you can attach a javascript listener to log every time a user hovers the mouse cursor over a DOM element, or every time a button is clicked, or any other data that you can think of. You may then use the reporting interface to drill-down into these events to get even deeper insight into your application.

Using custom event tracking is pretty simple to implement.

First, load your analytics profile as usual:
 (function() {  
   var ga = document.createElement('script');  
   ga.type = 'text/javascript';  
   ga.async = true;  
   ga.src = '';  
   var s = document.getElementsByTagName('script')[0];  
   s.parentNode.insertBefore(ga, s);  

Second, initialize the global analytics variables (using your own analytics id).
 var _gaq = _gaq || [];  
 _gaq.push(['_setAccount', 'UA-XXXXXXXXX-X']);  

Third, generate events!
            'my-button-events', 'the big green button', 'userid']);  

Test this by executing it on a page, then go to your Analytics account and check under Behavior/Events (make sure to select the reporting date to include today, and give it a few minutes).

The _gaq.push(['_trackEvent', 'category', 'action', 'vars'); can be attached to whatever javascript action-listener you want. Some examples I've seen only took two custom parameters ('category' and 'action'), which I found did not work for me. Including the third parameter, which can be whatever string you want, or empty string, seemed to fix it for me.

Note that this will also work in Chrome Extensions, and may be a good way to get some insights into your users.


Popular posts from this blog

Wkhtmltopdf font and sizing issues

Import Google Contacts to Nokia PC Suite

Can't delete last blank page from Word