Home > JQuery, SEO, google analytics > Enhanced Visitor Event Tracking With Google Analytics and JQuery

Enhanced Visitor Event Tracking With Google Analytics and JQuery

February 16th, 2010 Rob Leave a comment Go to comments

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);
Categories: JQuery, SEO, google analytics Tags:
  1. February 20th, 2010 at 21:31 | #1

    About time you posted a new article. :)

  2. February 21st, 2010 at 00:07 | #2

    :)

    There’ll be another couple in the next week too ;}

  3. Tony
    February 22nd, 2010 at 15:28 | #3

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

    Regards

    Tony

  4. February 27th, 2010 at 23:47 | #4

    @Rob
    Awesome. I can’t wait. :)

  5. February 28th, 2010 at 15:22 | #5

    Thanks for the info.

  6. February 28th, 2010 at 15:23 | #6

    Hehe :)

    Nice portfolio you have there btw.

  7. February 28th, 2010 at 15:34 | #7

    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

  8. March 17th, 2010 at 12:51 | #8

    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 ;)

  1. No trackbacks yet.