Track outbound links and file downloads automatically in Google Analytics
Just to let you know that I have finally gotten round to updating the original combined tracking script hack I had for urchin.js to the new ga.js. 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.
April 2011 – New async hack launched
This hack has been completely revise for the latest GA async code. Further info >>
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 virtual locations for these in your Google Analytics reports are: /ext/, /downloads/, and /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
…your remaining web page content…
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.
- 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.