Automatically track Google Analytics Events with jQuery

Here’s a trick that could come handy for some of you. There’s a simple way to capture all click as events so you can later have a full behavioural report of in Google Analytics.

This trick is not a full solution if you’re looking forward tracking the behaviour of your users, but a very nice way to have an idea of what’s happening on your site with no much coding.

Capture Events automatically with jQuery

The trick consist in placing a small jQuery code in a js file that is being used site wide (if you don’t have one, just create a new js file and incorporate it on your footer).

These lines of code will automatically detect a click and set a Google Analytics event under the category you want.  Check it out!

// Insert Google Analytics Events for all links on website:
$('a').click(function() {
_gaq.push(['_trackEvent', 'My Category', $(this).attr('href'), $(this).text()]);

});
 

The code will track all clicks to an ‘a’ element and push the information about that specific link to Google Analytics, so you can latear retrieve a full report on the most used links on your site.  Be sure to customize “My Category” with something that makes sense to your site, like “Links”, “Clicks”, “General”, or something similar.

Later, after a few days, you can go to your Google Analytics account and search under Behavior / Events / Overview and you will see your category listed.  Click on the category and then you will have a full list of usage of links on your website.

google-analytics-event-overview-menu

TIP 1: This trick is specially useful to discover the most used links to external sites so you can have an idea of where people goes after navigating your site.

TIP 2: You can customize the jQuery line to capture different actions, for example you could capture form IDs and inputs to have an idea on how your forms are being used.

 

Leave a Reply