Tracking links to direct downloads - Automatically
GA Hacks, GA specific September 10th, 2007My standard word of caution - This is a tech tip and requires you to have a knowledge of html and javascript to implement and use it…

Following on from my previous post Tracking banners and other outgoing links automatically, this GA hack allows you to track downloads automatically. As you may know, tracking download files such as PDF, EXE, DOC and XLS can be achieved quite easily with the modification of the link to include an urchinTracker call to log a virtual pageview. However, as for tracking outgoing links, manually modifying each download link becomes inefficient when there are large numbers of ever changing files to track. You can overcome this by applying the JavaScript code below:
<script type="text/JavaScript">
// Only links written to the page (already in the DOM) will be tagged
// Script can be called multiple times
function addExtDocEvents() {
var as = document.getElementsByTagName("a");
var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js"];
// add further document types as required
for(var i=0; i<as.length; i++) {
var tmp = as[i].getAttribute("onclick");
// Tracking electronic documents - doc, xls, pdf, exe, zip
if (tmp != null && tmp.indexOf('urchinTracker') > -1) continue;
for (var j=0; j<extDoc.length; j++) {
if (as[i].href.indexOf(extTrack[0]) != -1 &&
as[i].href.indexOf(extDoc[j]) != -1) {
var splitResult = as[i].href.split(extTrack[0]);
as[i].setAttribute("onclick",((tmp != null) ? tmp : "") +
"urchinTracker('/downloads" +splitResult[1]+ "');");
break;
}
}
}
}
addExtDocEvents()
</script>
The script works by looking for links within the browser’s Document Object Model (DOM) that match the file extension given in the variable array extDoc. If so it is modified to include the urchinTracker call. By this method, all file downloads will be reported as:
/downloads/the-url-that-is-clicked-on
Where the-url-that-is-on clicked on is minus ‘http://’. You can modify the JavaScript to adjust the path as required,
Tip: As for the Tracking of banners and other outgoing links, the position of this code within your page is important. This must placed after your call to the GATC. Alternatively place the addExtDocEvents() call in an onLoad event handler and host the JavaScript in a separate file.
A note on performance: Each time your page loads, this script will go through all links referenced on the page to see if it is for a download. Clearly the more links on your page, the harder the script must work. As long as the number of links on each page number in the hundreds and not thousands, performance should not be a problem. Also, pages with a large number of links, it is possible that visitors will click on a download link before the script has modified it. The result is that click through will not be tracked by Google Analytics.
I have combined both the Tracking of banners and other outgoing links - automatically with this hack into a single script that is available at: http://www.advanced-web-metrics.com/blog/scripts.
Did you find this tip useful? All tips are being grouped under the category GA Hacks. Please provide your feedback with a comment.
Email This Post
Print This Post


September 12th, 2007 at 1:45 pm
Brian,
It looks like the link to the Scripts section at the end of your post is broken.
September 12th, 2007 at 2:21 pm
Thanks Marco - fixed now
September 13th, 2007 at 3:00 pm
I’t really cool you made a scripts sections on this site. I’m look forward to get inspired by your code.
October 17th, 2007 at 12:48 pm
Quick question - can this be modified to track flv, and swf files also?
October 18th, 2007 at 11:03 pm
Courtney: Yes, just modify the array defined in:
var extDoc
December 4th, 2007 at 8:35 pm
Can you suggest how to track file downloads from links, based on other sites?
December 4th, 2007 at 9:43 pm
Hello PosterManiac
Not sure what you mean here by ‘based on other sites’. The use of urchinTracker to create virtual pageviews is based on the onClick event handler. In other words, it is the click event that is being tracked - not the file itself. It does not matter where the link takes the visitor after they have clicked. Does that make sense?
February 27th, 2008 at 5:03 pm
Brian,
This approach is great if the pdf is clicked from within your own site, but… do you have any suggestions how to track when the pdf was found via a search listing e.g. a google search result?
March 1st, 2008 at 10:29 pm
AJ: That’s a great question and yes this can be achieved. Its covered in Chapter 9 (see ToC) “Tracking Links to Direct Downloads”.
Essentially, you use a landing page to capture all the campaign variables and fire off a hit to the GATC. The page then (within a fraction of second) forwards the visitor to the pdf file.
April 21st, 2008 at 12:36 am
Great code mate and can be altered to work with any type of analytics program - or by yourself if you called your own page and just count them.
July 16th, 2008 at 11:17 pm
Today I uploaded a new combined tracking script (over writing the original) to fix a bug for tracking outbound links. Essentially the original code only worked for the first domain specified in the extTrack array and effects both ga.js and urchin.js. This is now fixed.
The script is at: http://www.advanced-web-metrics.com/blog/ga-scripts/
I have also added an email address form to this page so you can be notified of any further updates.