My 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…
GA Hacks

[This hack is for the legacy urchin.js tracking code.
Always refer to the Scripts & Downloads section for the latest version.
]

April 2011 – New async hack launched
This hack has been completely revise for the latest GA async code. Further info >>

 

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 &#038;&#038; tmp.indexOf('urchinTracker') > -1) continue;
for (var j=0; j<extDoc.length; j++) {
if (as[i].href.indexOf(extTrack[0]) != -1 &#038;&#038;
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,

IMPORTANT: 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 you can place the addExtDocEvents() call in an onLoad event handler and host the JavaScript in a separate file. As an example I show this below, assuming the javascript is hosted in a file called trackExternal.js, as follows:

<script src="http://www.google-analytics.com/urchin.js" type="text/JavaScript">
</script>
<script src="/trackExternal.js" type="text/JavaScript"></script>
<script type="text/JavaScript">
	_uacct = "UA-XXXXX-Y";
	urchinTracker();
</script>
 
<body onLoad=”addExtDocEvents()>
	<em>...your remaining web page content...</em>
</body>

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.

***Update***
I have now combined this hack for downloads with the Tracking of banners and other outgoing links – automatically into a single file that is available in the Scripts & Downloads section.

Did you find this tip useful? All tips are being grouped under the category GA Hacks. Please provide your feedback with a comment.