Updated auto tracking script for file downloads, outbound links and mailto links
Google Analytics Hacks June 8th, 2008Just to let you know that I have finally gotten round to updating the combined tracking script hack I had for urchin.js to the new ga.js. If you know what I am referring to and simply wish to go straight to the script, the new file is listed here: http://www.advanced-web-metrics.com/blog/ga-scripts/
Essentially the combined tracking script is a modification of the GATC so that it automatically tracks:
- outbound links – Automatically
- file downloads – Automatically
- mailto links – Automatically
Therefore, instead of having to manually modify such links by adding an onClick event handler to your ‘a’ tags, this script will do it all for you.
Why is this necessary?
Outbound links (links to other websites from yours), file downloads (e.g. PDFs, exe, xls etc.) and mailto links (links to an email address) can be important indicators as to whether your site is engaging with visitors. If so, you will certainly want to track these and potentially define them as goals in your Google Analytics reports.
Normally you do this by creating a virtual pageview. However if you have hundreds/thousands of these, or even just a few dozen that constantly change, manually tagging them is an administrative nightmare.
This script uses the properties of the browser (Document Object Model, or DOM for short), to capture links that are non-standard pageviews and automatically create the event handler for you.
The result is that your outbound links, file downloads and mailto links are tracked for you and are always up to date. The default virutal locations for these in your Google Analytics reports are:
/ext/, /downloads/, /mailto/
respectively, though of course you can change these as you wish. Typically you will wish to view these in your Content > Top Content report as shown below:
IMPORTANT: The position of this code within your page is important. This must placed after your call to the GATC. Add addLinkerEvents() 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=”addLinkerEvents()”>
...your remaining web page content...
</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.
Related posts:
- Tracking links to direct downloads automatically
- Tracking banners and other outgoing links automatically
Are you using this or creating your own hacks for Google Analytics? Please post your comments and share your thoughts here.
---Possible related posts (auto generated):



(19 votes, average: 4.00 out of 5)


July 1st, 2008 at 6:10 am
I wonder if it is possible (or how) to hack custom search engine script, so you could measure also engines with rewriten urls – so they do not have example.com?search=something but example.com/search/something
July 3rd, 2008 at 2:07 pm
Hello Brian
Thanks for the new combined tracking script for ga.js
I have pasted your code as given below the ga.js code in my pages but they throw up xhtml validation errors whereas the google script does not? Is this something I have to live with or should I be using your script as a separate file with an onload call?
If so, what is the correct onload call code syntax and where should I place this? – I know nothing about js I’m afraid.
Thanks
July 11th, 2008 at 12:13 pm
Tomas: Unfortunatley not. I tried this earlier in the year trying to capture the search terms that result in click throughs from Technorati which follows a similar format.
Essentially the issue is that the search engine detection is all done on the landing page (via the GATC). At first I tried to manipulate the referring string in the DOM, but of course this is a security violation as far as the brower is concerned and so not allowed.
July 11th, 2008 at 2:15 pm
Glenn: The script should work fine once installed correctly (it is running on this site).
What browser/OS are you using? Also, please send me the URL to the page in question and I’ll take a look.
July 15th, 2008 at 2:56 pm
Hi Brian,
First of all, thanks for maintaining this great resource. I just purchased your book and am looking forward to reading it!
My developers and I are having a bit of issue with this code.
All of our sites are built XHTML strict so most of the anchors that would be tracked by this script use onclick=”window.open(this.href); return false” to open a new window, as “target” is not valid.
From our tests it seems that this breaks your tracking script. Do you have any solutions in place for this scenario?
We looked at this solution (http://archivist.incutio.com/viewlist/css-discuss/55345, but haven’t yet tested it.
Thanks!
Danny
July 15th, 2008 at 10:43 pm
Danny: For trouble shooting I would try printing out all the onClick event handlers and checking the syntax. e.g.
within the loop:
for(var i=0; i
and after var tmp line, add an alert statement:
alert(“tmp” +i+ ” = ” +tmp)