Enhanced Visitor Event Tracking With Google Analytics and JQuery

Google Analytics has fast become the industry standard to track a plethora of web based information about your website. Whilst being totally free and easy to setup, you are limited to tracking elements that physically render in the browser – so items such as PDF, ZIP and RSS feeds links are not tracked, this because Google Analytics has a great reliance upon JavaScript. However, tracking such links can be achieved with a small amount of extra work.

Personally, I wasn’t aware you could track specific links with Analytics and only ever considored this when a client asked ‘why doesn’t Google show me the numbers of times my marketing report (read: a PDF file) has been clicked?’ – a totally valid request that I wanted to investigate.

Use JQuery to improve Google Analytics and track downloads, RSS, Email & external links

First things first, make sure you have a google Analytics account, the latest version of JQuery and the latest version of the analytics code running on your website πŸ™‚

As with the majority of the JQuery magic, everything happens within the doc ready event listener – this will used to capture various clicks to select elements.

Tracking Download Link Clicks (PDF, ZIPs etc.)

$(document).ready(function() {
	
	$("a[rel=download]").click( function() {
		var fileName = $(this).attr("href");
		pageTracker._trackPageview(fileName);
		return true;
	});

});

Then on every link you wish to track, simply add the rel attribute to your non HTML files as follows:

<a href="myData.zip" rel="download">Download My ZIP Data File</a>

Tracking Downloads of Specifc File Types (E.g. PDF files)

Using the dollar sign to match against links that end in .pdf (or any extension you wish to track).

$(document).ready(function() {

	$("a[href$=pdf]").click( function() {
		var myPDF = "/pdfDownloads/" . $(this).attr("href");
		pageTracker._trackPageview(myPDF);
		return true;
	});

});

The /pdfDownloads/ is used to identify and seperate report data within Google Analytics.

Tracking the click of a specific link such as an RSS feed

Simply add an identifier to your RSS feed link (in this example the link was given an id of ‘rssFeed’):

$(document).ready(function() {
	
	$("a#rssFeed").click( function() {
		pageTracker._trackEvent("RSS", "RSS Subscriber Link Clicked");
		return true;
	});

});

Tracking mailto: Link Clicks

$(document).ready(function() {
	
	$("a[href^=mailto:]").click( function() {
		pageTracker._trackEvent("Mail", "User clicked on mailto link");
		return true;
	});

});

Tidying up….

You should also disable the clicked element to prevent multiple event recording and provide feedback. To do this, simple add the following at the start of each piece of code – disbabling the element and changing the cursor to an egg timer (although you could display a small graphic to make things look prettier):

$(this).css("cursor", "wait");
$(this).attr("disabled", true);

Published by

Rob Allport

Web Developer based in Stoke-on-Trent Staffordshire Google+ - Twitter

9 thoughts on “Enhanced Visitor Event Tracking With Google Analytics and JQuery”

  1. Tony :

    What happens if I’m using an old browser or JavaScript isn’t on? Can I still track links being clicked?

    Regards

    Tony

    As with any that relies on JavaScript your links clicks wouldn’t be tracked unfortunately. Also, the normal page view data wouldn’t be recorded by Google Analtyics either, as that too relies on JavaScript. I’ve personally found that pretty much all visitors on this site and the companies main website (who I work for) have JavaScript enabled.

    You’ll find that a lot of modern websites wouldn’t look/feel/function the same with JavaScript disbaled.

    Also, as we’re just tracking clicks in the background, the page would still function exactly the same with JavaScript in this instance, as the code ‘degrades gracefully’.

    Rob

  2. Nice, been looking for something like this… read your other posts too, good stuff – have rss’d your blog πŸ™‚ next post better be interesting too πŸ˜‰

  3. @Tony – “What happens if I’m using an old browser or JavaScript isn’t on? Can I still track links being clicked?”

    Then the code will not execute and the tracking will not work.

Leave a Reply

Your email address will not be published. Required fields are marked *