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