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…
[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:
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:
...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.
***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.
Great script, thanks.
I was having a problem where I was seeing a lot of “/ext/undefined” in analytics, I believe it was tracking links like href=”javascript:foo();”.. so I added
&& as[i].href.indexOf(‘http’) == 0
to the if statement on line 46 and it seems to fix that issue I think.
Russell: I am actually in the process of producing a combined tracking script to allow users to switch between using the virtual pageview method or using Event Tracking. The necessary changes are straight forward if you are familiar with JavaScript – just familiarise yourself with how event tracking works from the book (Chapter 7).
Note the book is slightly different to the latest syntax but still works – see my comments in http://www.advanced-web-metrics.com/blog/2008/05/26/book-corrections-and-typos/
In terms of site overlay, the onClick event handlers will not effect this as site overlay uses the href attribute of the anchor tag. Could it be something else?
Hi Brian, I was interested in using GA event tracking to track the outbound links and downloads instead of registering them as pageviews. Any ideas of what I would need to modify in your script to enable this?
Also, I’ve found that some JS calls to load an overlay window aren’t working because the current script overwrites every href tag with the onclick functionality. Do you have any ideas on how I could modify the script to ignore these href tags (maybe by recognizing them by some kind of id, class or as having an existing onclick behavior)?
Thanks again, this script is a great additional to our site tracking!
Hi Brian,
Trying to use this script on http://www.earth-kind.com and having some problems. Tried posting the code directly into our page and for some reason the dotnetnuke is choking on it, so I put the entire script in an external javascript file
http://www.earth-kind.com/js/analytics-auto-tagging.js
Since the tracking script needs to be called after my call to google analytics (right?)
I then put the google analytics code in the head and called it immediately after the analytics code in the head with
Big problems occured after that. Most noticeable being the stylesheet exploded even though we hadn’t touched any code controlling that.
Is there something you can see I’m doing wrong? Are there additional instructions I’m missing? I saw something about…”place the addExtDocEvents() call in an onLoad event handler and host the JavaScript in a separate file.” but I don’t see that in the final version of the script posted in your library.
thanks –
I see the stats will be under downloads folder.
How long is the turn around time once I place this code on the website and then click on the pdf file? Should I wait for 24 hrs to show up in google analytics? Also where can I find the stats for the same.
Thanks.
Timo: take a look at http://www.advanced-web-metrics.com/blog/ga-scripts/ – there are versions for both urchin.js and ga.js
This looks like a great script. One question: GA recently changed their tracking code. Is this script written for the “old” tracking code or for the new one? Or doesn’t it matter? Thanks!
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.
Great code mate and can be altered to work with any type of analytics program – or by yourself if you called your own page and just count them.
AJ: That’s a great question and yes this can be achieved. Its covered in Chapter 9 (see ToC) “Tracking Links to Direct Downloads”.
Essentially, you use a landing page to capture all the campaign variables and fire off a hit to the GATC. The page then (within a fraction of second) forwards the visitor to the pdf file.
Brian,
This approach is great if the pdf is clicked from within your own site, but… do you have any suggestions how to track when the pdf was found via a search listing e.g. a google search result?
Hello PosterManiac
Not sure what you mean here by ‘based on other sites’. The use of urchinTracker to create virtual pageviews is based on the onClick event handler. In other words, it is the click event that is being tracked – not the file itself. It does not matter where the link takes the visitor after they have clicked. Does that make sense?
Can you suggest how to track file downloads from links, based on other sites?
Courtney: Yes, just modify the array defined in:
var extDoc
Quick question – can this be modified to track flv, and swf files also?
I’t really cool you made a scripts sections on this site. I’m look forward to get inspired by your code.
Thanks Marco – fixed now
Brian,
It looks like the link to the Scripts section at the end of your post is broken.