Updated auto tracking script for file downloads, outbound links and mailto links
GA specific 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, the new script 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. Typical you will wish to view these in your Content > Top Content report as shown below:
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.
Email This Post
Print This Post


(8 votes, average: 3.63 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)
July 16th, 2008 at 10:44 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.
July 17th, 2008 at 2:34 pm
Brian,
This is good stuff. I am running the script on a couple of pages (keepingchildrensafe.org and writemedown.org) and they are working fine. The new annotated version is helpful. You might get more users if you provided a brief guide on configuration. I worked it out from the html of your site, puzzled a bit over having the GA script in the header.
One question: I have included the sites with and without “www” in the section for excluding my own domain from tracking. Is that necessary?
Oh, and I have read large chunks of the book and am well impressed so far.
July 21st, 2008 at 12:16 pm
John: agreed!
I will be adding more comments to the script itself and creating an install page just for this script.
Essentially the script has grown out of blog posts over time which makes it a little difficult to follow. Its now time for me to put this all together on a single page.
In terms of placement, I just prefer to put the GATC in the header as opposed to the footer - just my preference. I take the risk that the chances of Google’s servers preventing the rest of page loading is minimal!
August 11th, 2008 at 12:20 am
Hi Brian
I have had my web-design company implement the script to automatically track downloads in GA but where in GA do I go to view the results? I don’t really understand the part in this post where you say ‘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.’
Thanks in advance for your help!
Regards,
Frederikke
August 15th, 2008 at 8:18 am
Nice script.
We sometimes advice our customers to use a similar script to also measure downloads, mailto, clickouts. I also believe using this script is a must. In example knowing what links on your website gave you the biggest loss of traffic is a real interesting metric.
I believe in some cases it might be advisable to add the pageurl on which the click was made.
In example did a lot of visitors click my email address on the contact page or on the page to submit complains.
Measuring everything gives you the biggest insight and the biggest worries.
August 15th, 2008 at 1:47 pm
Brian -
Great book, finished it last week and it has shaped the way I approach GA for our clients and sites I work on. I have also been having issues with this script and wondered if you can answer a question regarding it.
Will the script pick up mailto links that are formatted with script to insert custom subject lines and content into the email? For example - if the mailto link looks like this: sales@example.com?subject=question&body=example or will it only work if the mailto link is sales@example.com
Thanks!
August 17th, 2008 at 10:48 pm
Frederikke: Good point. I have clarified this in the post and added a screenshot. The data will show in numerous places - where ever there is a page URL listed. The most common place you will see this is in the Top Content report. HTH.
Martijn: Thanks for the feedback. Good to see you made it through to a GAAC. A long process I know
Adam: The script captures everything after the “mailto:” in the a tag and puts into a virtual URL. So yes, you will see the subject and can filter on this.
August 20th, 2008 at 12:34 pm
I do not seem to get this script nor the custom_se-ga.js script to work. No external links are tracked and google traffic shows up just like before as “google / organic”. Can I not have it both ways? I also noted that the GA code has recently changed (just a bit):
http://www.morevisibility.com/analyticsblog/rest-in-peace-_initdata.html
August 20th, 2008 at 11:46 pm
Jonas - I have this script running on numerous sites, both the urchin.js and ga.js. What appears to be the problem?
BTW, I do not recommend using custom_se-ga.js at the moment. Something has indeed changed with the GATC - I am out of the direct loop with Google now, so I don’t know exactly what, but I am pretty sure it has nothing to do with _initData.
Essentially custom_se-ga.j is still in “testing”
August 21st, 2008 at 1:13 pm
Brian, on my site (jonas.ax) I have installed the script, but I see no data for:
/ext/, /downloads/, /mailto/
in the “Content Overview”/”Top Content” reports.
I’ve tested by clicking on links to external sites and clicking on the mailto links as well (from several different computers). I’ve also made sure to wait for the pages to load completely, but still no luck only ordinary page views are displayed in GA.
I removed the custom_se-ga.js.
August 21st, 2008 at 10:59 pm
I’ve been using the custom_se-ga.js on a few sites and haven’t had any issues. The data is coming through ok for me. For what its worth at least
August 22nd, 2008 at 9:59 am
Jonas:
I have implemented the same code recently onto our website, and encountered exactly the same problem as yourself. After doing some research I think that I understand what the issue may be.
Where did you implement the script? The custom script should be above the GA tracking code you have implemented. Therefore:
< script type=”text/javascript” src=”http://www.yoursite.com/scripts/custom_se-ga.js
var gaJsHost = ((”https:” == document.location.protocol) ? “https://
ssl.” : “http://www.”);
[removed](unescape(”< script src=’” gaJsHost “google-
“));
var pageTracker = _gat._getTracker(”UA-0000-1″);
pageTracker._initData();
pageTracker._trackPageview();
Does that make sense?
August 25th, 2008 at 9:06 pm
Hi Brian,
I’m assuming the combined code can be modified to track file download links only.
Can you indicate which parts of the js file should be removed to avoid tracking the outbound and email links.
Thanks,
Michael
August 27th, 2008 at 12:33 pm
Gabe,
Thanks for your advice.
I’ve been trying to get it to work on jonas.ax.
I moved the custom script, in this case addLinkerEvents-ga.js above the GA tracking code and did some testing and waiting. But still no luck. John (earlier in the comments) has the script after the GA code.
I’ve placed the script in a folder called /google-scripts/ rather than just /scripts/ (but the link to it in the GA code is correct). Perhaps this could be it? The GA code is also at the end of each page, perhaps moving it to the beginning would do it?
September 1st, 2008 at 7:16 pm
Jonas: The reference to the .js can be any path you wish but it must be placed below the GATC. This is because it is calling routines that are defined in the GATC.
Michael: Yes you can modify the code to only track downloads. Seek the help of a web devloper who understands JavaScript for this. Its not hard to do, but it does need to be exact.
September 1st, 2008 at 7:31 pm
custom_se-ga.js
I am going to setup a separate thread for discussing this script. Just want to do some testing here first…
September 4th, 2008 at 11:37 am
I think I did everything the way it should be done but obviously I did not: Nothing shows up under
/external/
/directdownloads/
/mailto/
which is where they should show up since I modified the script accordingly.
However, I can see nothing in GA even though I’ve had the script on a few pages for a couple of days now. To be sure I checked for the original (/ext/, /downloads/, /mailto/) as well.
So I’m afraid I might be missing something really, really obvious.
Here is how I implemented it:
var gaJsHost = ((”https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(”%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
var pageTracker = _gat._getTracker(”UA-2851757-1″);
pageTracker._initData();
pageTracker._trackPageview();
Do you see any glaringly obvious problems? tracker.js is the tracking script.
Thanks,
Timo
September 4th, 2008 at 11:40 am
Damn. The blog-software killed part of my post.
After the last line of code it should have been:
< script type=”text/javascript” src=”/common/includes/foot/tracker.js”
(hope this works now)