Track File Downloads & Outbound Exit Links Automatically
What this Google Analytics Plugin does
Automatic tracking of file downloads and outbound exit links links in Google Analytics - without the pain of modifying each link manually across your site. Aimed at webmasters, this JavaScript plugin scans all your page links for you in the background, and automatically adjusts them accordingly for Google Analytics by automatically adding an onClick event handler for you. There is also the option to modify the bounce rate calculation. Read the blog post. Summary of features:
- Massive time saver – Automatic tracking. No manual modifications of your exit links required, just drop this script into your page header
- Automatically track File downloads – MS Office (doc/x, xls/x, ppt/x), exe, zip, txt, pdf
- Automatically track all mailto: links
- Automatically track outbound exit links i.e. those not local to yourdomain.com
- Flexible – For each type (download link, outbound link, exit link, mailto link), select if these are tracked as a virtual pageview or an event
- Multiple pageTracker support – works with multiple page tags. See the post Roll-up reporting in Google Analytics for when this is useful
- Improve bounce rate calculation – You can optionally set a timeout to redefine a bounced visit e.g. treat a single page visit as a bounce if time on page = 30+ seconds
- Completely self-contained and independent of all other libraries, such as jquery
As you may know, I wrote the original file download and outbound link tracking script back in 2007. Things have moved on since then. This Plugin has been completely rewritten for the ga.js async code, with more flexibility, better performance and improved browser compatibility.
Also try this with the Customising the SEO list for Google Analytics plugin.
Usage
- For use with ga.js (async) only.
- The code is regularly updated. All subscribers will be notified of updates via email.
- Last code update: April-2012
Easy Install
- Purchase either the Standard or the Developer version of the automatic plugin to receive the JavaScript file.
- Upload the .js file into your website root directory (used in this example) or other suitable place
- Modify all your web pages (once only change) to reference the .js file above your Google Analytics Tracking Code.
- That’s it… Or perhaps you thought there was more!
Example GATC – the only line added is #1. The rest is your remaining tracking code:
1 2 3 4 5 6 7 8 9 10 11 12 | <script type="text/javascript" src="/addLinkerEvents-std.js"></script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-123456-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> |
You can view the new GA calls in your Real-Time reports, or by using the the GA Debugger extension for Chrome (see item #4 of my post The Best Google Analytics Addons).
In your standard reports, allow approx. 4 hours for your reports to be populated with the new information. To view these, log in to your Google Analytics account and goto ‘Content > Site Content > All Pages’ report (for virtual pageviews – see below image), or ‘Content > Site Content > Events > Top Events’ report (if you have configured it for event tracking).
If you are combining this with the Custom Search Engine Hack, your GATC will look like the following (lines #1,2 and 7 are the changes):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <script type="text/javascript" src="/addLinkerEvents-std.js"></script> <script type="text/javascript" src="/custom_se-async.js"></script> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-123456-1']); addCustomOrganic(_gaq); // custom search engines _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> |
Who uses this…?
Small businesses, universities and blue-chip clients rely on this plugin…

Feedback
As always, I appreciate feedback. Feel free to suggest new features. Also, check out the Custom Search Engine list for Google Analytics plugin.


April 20th, 2011 at 9:40 pm
What about if outbound links are internal redirects (PHP redirect, 302/307 redirects, etc) such as mydomain.com/outboundlink.php – would each link have to be manually tagged as such, or will this hack “auto-detect” these as outbound links?
April 20th, 2011 at 10:11 pm
Nick: That won’t work as the href element of the link will still be pointing to your-domain.com. That is, the hack will not see the outbound link as that only appears server-sider after the click-through.
Is there a specific reason why you are handling outbound links in this way? It sounds counter-productive from a user-experience point of view i.e. users like to know if they are clicking an outbound link first…
May 5th, 2011 at 11:00 am
I reckon you have sorted the case where outbound links already contain onClick event?
May 6th, 2011 at 9:40 am
Antti: yes,twill append the call to an existing onClick action. That was the case with the older version as well. Have you tried it?
May 6th, 2011 at 12:19 pm
Good work Brian. I’m not that technical, so how monkey-proof is it. Can I offer it to my clients without getting techical questions in return. In other words, what’s the experience in the real world?
May 8th, 2011 at 7:37 pm
Stanley: As you can see form the setup I have posted, its pretty straightforward to install. That said, a webmaster who understands html and javascript should be the one to install this script.
May 10th, 2011 at 2:30 pm
Maybe you said this and I didn’t see it, but can you track MP3′s with this?
May 10th, 2011 at 3:20 pm
Christine: The Developer version will allow you to set any file extension to track.
As I am still working on the Developer version, I would be happy to add MP3 support to your standard version for now at no extra cost. Just go ahead and purchase and I will look out for your order
June 1st, 2011 at 3:55 am
Great resource at a very fair price Brian. Supposing you have a site with multiple outbound links under different categories. Is there an elegent way to capture those categories in the Category field for event tracking?
June 7th, 2011 at 10:00 pm
Doesn’t work with links within flash, right?
June 8th, 2011 at 3:17 pm
Vlad: You are correct. Links within a Flash object are not a part of the browser DOM, so this technique cannot be used.
August 25th, 2011 at 7:59 pm
Brian, will this track clicks on phone numbers from mobile devices (those are not href links in HTML)? Does Standard version can utilize tracking via Events?
September 20th, 2011 at 10:09 pm
You forgot to say (1A) that one’s domain name **does** have to be put inside the js file by editing, its omitted from this instruction page here but is in the js file instructions.
August 1st, 2012 at 6:00 am
What a great plugin. I am really getting into finding tools to use to help track and improve conversion optimisation on client sites.
August 15th, 2012 at 9:08 pm
Is it possible to use this code on individual html pages of site instead of in a master layout that contains the Google Analytics code?
November 26th, 2012 at 7:55 am
I’ve implemented this plugin and 47% of the tracked outbund clicks/events have event label = undefined. What’s that?
Also 41 out of 776 events tracked are internal clicks.
Site is http://www.visitaland.com (and m.visitland.com).
Strange.
November 27th, 2012 at 8:08 pm
@Jonas: whenever a href attribute does not point to a real http link you will see undefined because the link is not “real”. So for example, I found this in your html:
Because the href is pointing to a js (rather than to a webpage), the plugin does not know what to do with it.
HTH
January 23rd, 2013 at 10:51 pm
Hi Brian,
We want to roll up this report into Event Tracking on Google Analytics to avoid inflating pageviews.
If we use your script, will we have the ability to control the Category, Action and Label parameters?
Thanks,
Ryota
January 24th, 2013 at 11:17 am
@Ryota – you will need the Developer license in order to modify those settings.
February 14th, 2013 at 1:03 pm
Hi Brian,
Can I run the script with Google Tag Manager? If yes how?
Thank you,
Aris
February 15th, 2013 at 8:06 am
@Aris – I don’t see any issue using Google Tag Manager or any other tag management solution – its just a call to another javascript file. FOr GTM the custom HTML tag is the way to go.