Feb 11 2008

How to track downloads in Google Analytics automatically

Print
Written by David Towers   
Monday, 11 February 2008

Google Analytics download tracking We use Google Analytics here because it is a great tool to help us understand our visitors. Today we're going to talk about a way to track if visitors download files or click on links to external websites.

Tracking this sort of behaviour may be really important to see if people are interacting with the web site the way intended. For example, businesses that have their brochures or product catalogues downloadable as PDF's.

The official Google Analytics help says that any link can be monitored if this script is added in the link:

onclick="pageTracker._trackPageview('/file_name.file_extension')

Adding this script to every download link on your website can be a laborious task.

Tracking downloads automatically

The simple solution we found is to use a JavaScript that automatically attaches event listeners to files which are downloaded from your web pages.

What sort of downloads can you track automatically?

Using the javascript you can track the following file extensions:

  • doc, eps, jpg, png, svg, xls, ppt, pdf, xls, zip, txt, vsd, vxd, js, css, rar, exe, wma, mov, avi, wmv, mp3
In addition to tracking these downloads, the JavaScript will also track clicks on links to external websites and include these in your Google Analytics statistics.

How do you install the tracking JavaScript?

Is it really that simple?

Yes, but note that this tracker is written for Google Analytics new tracking code which uses the lighter ga.js. If you are using the old tracking code with urchin.js you can still track file downloads but not using this link tracking JavaScript. Check out this site for more information on tracking download links with the old tracking code.

How might download statistics be useful?

Taking the example of a business that has its product catalogues downloadable as PDF's: If through Google Analytics they can see that users are not downloading their catalogues they might consider redesigning the web pages to give more prominence to these downloads.

Another example could be a software company wanting to measure how many users download their software from their web site. Using this JavaScript they could measure the number of downloads and even set up goals in Analytics to measure the conversion rate.

How will you use the statistics generated by this Javascript? Please post any comments or questions you have about this JavaScript in the comments below.

add51 Comments
Bobwill
February 12, 2008
Votes: +0

I have a question after reading your suggestion. Could I add your code to the front of the ga.js code and then just do the following in my html file or would this mess up the loading or would there be some other modifications to make?

< script src="http://www.mysite.com/scripts/ga.js" type="text/javascript">

report abuse
vote down
vote up
David T
February 12, 2008
Votes: +2

Hi Bobwill,

I'm not sure I've understood your question... The idea of this code is simply to add it before the analytics code on each web page...

Implementation is really simple because you can just search through your entire site replacing your standard analytics code:

< script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://
ssl." : "http://www.");
document.write(unescape("< script src='" gaJsHost "google-
analytics.com/ga.js' type='text/javascript'>< /script >"));
< /script >
< script type="text/javascript">
var pageTracker = _gat._getTracker("UA-0000-1");
pageTracker._initData();
pageTracker._trackPageview();
< /script >

with:

< script type="text/javascript" src="http://www.yoursite.com/script/
gatag.js">< /script >

< script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://
ssl." : "http://www.");
document.write(unescape("< script src='" gaJsHost "google-
analytics.com/ga.js' type='text/javascript'>< /script >"));
< /script >
< script type="text/javascript">
var pageTracker = _gat._getTracker("UA-0000-1");
pageTracker._initData();
pageTracker._trackPageview();
< /script >

Does that clarify it?

report abuse
vote down
vote up
Bobwill
February 12, 2008
Votes: -1

OK, I misunderstood your explanation. The statement on your web site says "To use, place this file on all pages just above the Google Analytics tracking code." To me that meant all the "js" code (the file) needed to be inserted as the top of the "ga.js" code. Your saying to place a line of code that loads the "gatag.js" script at the beginning of the "ga.js" script. I guess I am dense. I will try to make it work.

report abuse
vote down
vote up
David T
February 12, 2008
Votes: +0

Glad it's clear now, thanks for your comments. Hope you get it working!

report abuse
vote down
vote up
JP
February 22, 2008
Votes: +0

Excellent Excellent Excellent Article!!!! Great Job Indeed!!!!

report abuse
vote down
vote up
David T
February 22, 2008
Votes: -1

Hi JP!

Welcome to Good Web Practices! Thanks for posting your encouragaing message! smilies/smiley.gif

report abuse
vote down
vote up
eagleeye
February 23, 2008
Votes: +1

Seems a simple and great solution to tracking downloads. I have added the code and am just waiting for Analytics to process.

Will be using this to track downloads of PDF brochures and application forms.

Maybe I missed something but where in Analytics will this be displayed? The little graphics at the top are too small to make out.

report abuse
vote down
vote up
David T
February 23, 2008
Votes: +0

Welcome to Good Web Practices Eagleeye!

It's great you've installed the script.

So the statistics for the downloads and external links will appear as content items. E.g. Within Google Analytics navigate to the "Content" section then select "Top content". The PDF's will be listed in here along with your websites pages. If you can't see the PDF files use the "Find URL" box and enter pdf then press "Go". This will bring up all the stats for your PDF downloads!

This script is a great way to track PDF brochure downloads, we've actually put it to use for the same purpose on other sites! Please let us know how you get on!

report abuse
vote down
vote up
Alex A
February 24, 2008
Votes: +1

Remember you can also set Analytics to have the download of files set up as particular goals for your website. This can be very useful to view your success rate!

report abuse
vote down
vote up
eagleeye
February 26, 2008
Votes: +0

So far I have added the script to one site and it has recorded a few PDF downloads and clicks on external links. It seems to be doing the job. I will add it to our main site and see how it compares with the onclick code we are currently using.

Alex A - we are already using goals to monitor specific downloads we want to track. I don't see the above code as replacing this but more as an easier way to monitor all downloads and clicks on external links.

report abuse
vote down
vote up
David T
February 27, 2008
Votes: +1

Delighted to learn that you have added this tracking script to one of your websites and it has worked eagleeye! Thanks for giving us the update!

report abuse
vote down
vote up
Raju
March 06, 2008
Votes: +0

hi david
i read your article, well i implemented the way you have explained it, but when i insert the onClick code in the link, my page got scattered around, i just want to track how many people are downloading my zip file.
Can you pls help me in this?
raju

report abuse
vote down
vote up
David T
March 07, 2008
Votes: +0

Hi Raju welcome to Good Web Practices!

You don't need to add the onClick code to the link!

The only thing you need to do is add the Javascript code before your analytics code. The Javascript automatically adds the tracking for downloads so that you don't need to add it to each link. You just need to follow the instructions above... There is a discussion about someone having problems adding the code here: http://groups.google.com/group...3be1899603

Hope you'll get it working Raju!

report abuse
vote down
vote up
super015
March 12, 2008
Votes: +0

we have built in a document repository so that true URLs and file names are obfuscated. This means that the filepath is in the db and you simply get a pointer, so that Competition_FAQ.pdf is actually a URL like http:///download/12345.

It seems that the best way to track this is to add "|download" to the line

var isDoc = path.match(/(? :doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js |css|rar|exe|wma|mov|avi|wmv|mp3|download|forward)($|&)/);

Is that correct?

report abuse
vote down
vote up
Danny
March 14, 2008
Votes: +0

Will this be able to track and uniquely identify the downloads if they are loaded via a javascript pop up window ?

I have my site setup so that our PDF catalogs open in a new pop up window.
example url:

report abuse
vote down
vote up
Danny
March 14, 2008
Votes: +0

my sample url seems to have been blocked somehow...but it is a basic javascript pop up javascript:pic('catalogs/golight.pdf')

will it be able to track a url like this ?

report abuse
vote down
vote up
Don
March 14, 2008
Votes: +0

Hi,
Would this code work with any stats package that uses a paste in javascript snippet to do it's counting? What would the other package have to do to mesh up with your code?

I ask because I'm testing some others in WordPress.
GA is nice but it takes way too long to find the data I want and I don't need most of what it provides.

report abuse
vote down
vote up
David T
March 19, 2008
Votes: +0

Hi Super015, Danny and Don! Thanks very much for your posts.

With regards to the implementation of this code, you'll find the answers to the majority of your questions here: http://groups.google.com/group...3be1899603

Super015, if your download link is http:///download/12345, the download will be included in the stats as "external/http:///download/12345" This means your downloads are tracked without needing to edit the javascript.

Danny, if the downloads are loaded via a javascript window, I'm afraid that this javascript won't be able to track the downloads. You could ask ShoreTel about how to do this on the Google Analytics forums.

Don this script is designed to work with Google Analytics. I'm surprised Analytics hasn't met your needs, I'd encourage you to stick with GA as once you get familiar with the setup its fantastic.

report abuse
vote down
vote up
Brian Katz
March 21, 2008
Votes: +0

Hi David T and Don

David - I am yet to implement it but its clearly a great piece of code,

Don, if you need your analytics almost instantaneously you may have to spend $$$ on SiteCatalyst or use something like AWStats
(http://awstats.sourceforge.net/)

The code should work with any JS-based tracking code that implements link tracking. Just substitute the call to pageTracker._trackPageview for the equivalent in the other package.

However, YGWYPF - in analytics apps that you pay for it might be unnecessary - eg SiteCatalyst allows you to track specified extensions automatically.

Brian Katz
www.vkistudios.com

report abuse
vote down
vote up
super015
March 25, 2008
Votes: +0

Thanks David.

When I go to Google Analytics and look up content > overview or content > drilldown, I do a search for anything with "download" in the URL and they are not showing up. Do I need to remove the edit I made to the isDoc variable?

Thanks again

report abuse
vote down
vote up
Brian Katz
March 25, 2008
Votes: +1

Hi super015 and Danny

super015, at a glance it seems like the mod to isDoc will not work because it requires that the URL must *end* with one of the extensions - note the ($|&) at the end of the regex pattern.
However, David's code, I understand, is intended where you cannot modify all the code on a site.

There must be a single piece of server-side code generating your URLs - that should be changed to generate the complete tag you need.

Danny, if your downloads are opening in another window, is that window being tracked - if it is, you will probably not want to trigger a _trackPageview because you would be counting 2 pageviews for the "download"

Brian

report abuse
vote down
vote up
Mark Freeman
March 27, 2008
Votes: +0

I added the code, but I am getting an error:

Error: uncaught exception: [Exception... "Component returned failure code: 0x804b000a [nsIDOMNSHTMLAnchorElement.hostname]" nsresult: "0x804b000a ()" location: "JS frame :: http://webauthoring.tercenim.com/gatag.js :: :: line 20" data: no]

Any ideas?

report abuse
vote down
vote up
super015
March 27, 2008
Votes: +0

Brian, thanks for the info. I'm no longer that technical and am very rusty on regex. I do know that with our repository, every document path ends in a 5-digit number (e.g., /forward/12345 or /download/54321). How could I modify the isDoc regex for this?

report abuse
vote down
vote up
Brian Katz
March 28, 2008
Votes: +2

super015 & Mark,

Mark, in what environment are you executing the script?

super015,
I'm an Analyst with enough JS to be dangerous smilies/smiley.gif

On the assumption that *all* your downloads are in the form described and that the urls end as you indicated above, it looks safe to change the Regular Expression pattern to

/(?:document|forward)/(?:[0-9]{5})(?:$|&|?)/


You want to be careful not to match URIs other than those intended, so that pattern limits matches to 5th digit either ending the URI or being followed by a query string or a &

If you want to test various scenarios, use The RegEx Coach (http://weitz.de/files/regex-coach.exe)

Brian
http://www.vkistudios.com/people.html

report abuse
vote down
vote up
BrianKatz
March 28, 2008
Votes: +1

Hi David T and Mark

I did not look closely at Mark's error that closely but I got a similar error totally unrelated to gatag.js. It was within FireFox's code, or more likely, one of its add-ons (Web Developer)
I returned to look at Mark's error and see that unlike mine, it refers to line 20 of gata.js itself.

Mark, what are the values of all variables before line 20?

Brian
http://www.vkistudios.com/people.html

Oh! Mark - what browsers have you tested this in and in which one(s) does the error show up?

report abuse
vote down
vote up
Brian
March 29, 2008
Votes: +1

Hi David T and Mark

I've now encountered that error too:
Error: uncaught exception: [Exception... "Component returned failure
code: 0x804b000a [nsIDOMNSHTMLAnchorElement.hostname]" nsresult:
"0x804b000a ()" location: "JS frame ::
http://www.goodwebpractices.com/script/gatag.js :: :: line 20" data: no]

It occurs if the page contains anchors that have no href attribute at all.

There will have to be a test to see if hrefs has an href attribute.

Brian
http://www.vkistudios.com

PS I'm going to be at the eMetrics Summit in Toronto next week - if you're going to be there, visit me at VKI Studios' Exhibitor's booth


report abuse
vote down
vote up
Brian
March 29, 2008
Votes: +1

Last post should read "hrefs[ l ] has an href attribute". Brian http://www.vkistudios.com
-

report abuse
vote down
vote up
super015
March 31, 2008
Votes: +1

OK I've figured out my question with a little but of regex brushup.

All of our links go to either download/ or forward/ with a 5-digit id number after the slash. Ergo the regex is:

var isDoc = path.match(/(?:download|forward)(/d{5})/);

report abuse
vote down
vote up
Travis Knutson
April 01, 2008
Votes: +0

I put this in and PDFs and downloads work great, but I can't seem to find external links. Am I missing something on where to look in top content?

report abuse
vote down
vote up
Erik Vold
April 07, 2008
Votes: -1

Why do you make the following suggestion:
Add the following JavaScript code just before your Google Analytics tracking code:

< script src="http://www.yoursite.com/scripts/gatag.js" type="text/javascript" > < /script >

??

The order doesn't really matter from what I can tell, did I miss something?

Also, gatag.js should really be as close to the tag as possible, because if ga.js were called right after the tag, and you placed gatag.js before ga.js, then gatag.js wouldn't work at all, because document.getElementsByTagName("a") would return an empty array, even though there maybe links on the page.

report abuse
vote down
vote up
Erik Vold
April 07, 2008
Votes: +0

I meant to say: 'gatag.js should really be as close to the [end body tag] as possible, because if ga.js were called right after the [start body tag],...'

report abuse
vote down
vote up
RobP.
April 22, 2008
Votes: +0

I've tried the script on our website but I don't see any PDF's in my Stats online. Am i looking in the wrong place maybe or do I have to setup something like a goal or filters to view the stats online in Google? Thanks

report abuse
vote down
vote up
David T
April 22, 2008
Votes: +0

Hi everyone, thanks for posting.

RobP. and Erik, you need to wait a couple of days after installing the stats to see them.

The statistics for the downloads and external links will appear as content items. To see the external pages: navigate to the "Content" section then select "Top Exit Pages". The external pages will be listed in here along with other exit pages. If you can't see the external page use the "Find URL" box and enter external then press "Go". This will bring up all the external pages the script has tracked.

report abuse
vote down
vote up
RobP.
April 23, 2008
Votes: +0

Unfortunately still nothing. I have had it on our site for over 2-3weeks now. I don't know what I'm doing wrong I've checked everything I could think of. Can you check the source on any of my pages to see if it's in there correctly? The only change I made was where the gatag.js is stored which is the js folder vs. scripts. That wouldn't make any diff. would it? Site: www.uconucedd.org

report abuse
vote down
vote up
RobP.
April 23, 2008
Votes: +0

sorry that should be www.uconnucedd.org

report abuse
vote down
vote up
David T
April 24, 2008
Votes: +0

Hi Rob! Just checked out your page. You have got the link to gatag wrong! You've made a mistake in the path. Change from this:

../js/gatag.js

to this:

http://www.uconnucedd.org/js/gatag.js

That will work.

BTW I'd recommend you remove your old google tracking code and taglinks!

report abuse
vote down
vote up
RobP.
April 28, 2008
Votes: +0

That was just the homepage. The others used a diff. template which I did update previously to the correct path. But I'll update the home page too. Thanks. But do you think that the hompage if incorrect would stop the other pages that have pdf's from showing up in the reports?

Thanks Again!

report abuse
vote down
vote up
David T
April 28, 2008
Votes: +0

Hi Rob,

I'm sure we will be able to get it working on your site!

I checked the tag you added and you've done it wrong again: " < http://www.uconnucedd.org/js/gatag.js "

report abuse
vote down
vote up
RobP.
April 28, 2008
Votes: +0

DOH!! :-D

report abuse
vote down
vote up
Declan
April 28, 2008
Votes: +0

I'd like to post a link on an external site that goes directly to my .zip file for download. How do I track that? It works great if you get the link from one of my existing pages.

How can I get it so that the zip file gets measured if it is downloaded directly from external sites?

report abuse
vote down
vote up
Richard N
April 29, 2008
Votes: +0

Declan why don't you send them to the webpage that contains the .zip file first? Then all they have to do is click the download button and you'll be able to track it using this script.

Have you got the updated ga.js installed?

report abuse
vote down
vote up
David T
April 29, 2008
Votes: +0

Hi Richard N - thanks for your good advice - it's always better to get people to the webpage as you can track them better... But it does happen that people hotlink files.

Hi Declan. Using Gatag.js you will be able to track on "site 2" how many people go to download the zip file on "site 1" because "site 2" will record how many people click on the link to download the zip file as the link will be included as an external link in the content stats.

If you want to see on "site 1" how many times the zip file has been downloaded from external sites, I would suggest checking the server log files to see how many times the zip was downloaded... Most hosting have AWstats installed which you could use to check this. Otherwise, take Richard N's suggestion and send the users to the html page. :-)

report abuse
vote down
vote up
Richard N
April 30, 2008
Votes: +0

Wow, I installed the script yesterday and now I can actually find out which PDF files and external websites are the most popular. It certainly is worth downloading and installing.

David T, have you guys got a paypal account for donations?

report abuse
vote down
vote up
David T
April 30, 2008
Votes: +0

Hi Richard! Delighted the script has worked as hoped! We'd appreciate any donations, here is how you can donate:
http://www.goodwebpractices.com/donate.html
Thanks again Richard.

report abuse
vote down
vote up
ben
May 05, 2008
Votes: +0

If I use target="_blank" to open a file in a new window, I get a script error:

urchinTracker is not defined
urchinTracker(file_path);

Has anyone else had this problem?

report abuse
vote down
vote up
David T
May 06, 2008
Votes: +0

Hi Ben! I've not had this error no... If you provide a link we could perhaps check that out for you?

report abuse
vote down
vote up
ben
May 08, 2008
Votes: +0

http://www.esrdnetwork6.org/data-forms/test.html

report abuse
vote down
vote up
Richard N
May 09, 2008
Votes: +0

David, the PDF and external tracker worked for a day and now it has stopped working.

Can you see anything wrong with the tracker on www.rockingham.wa.gov.au? The links on the right hand side should be mostly externals and PDFs are under the Employment section.

report abuse
vote down
vote up
David T
May 09, 2008
Votes: +0

Hi Richard, try putting gatag.js just above the analytics code, so below externalsitesPDF.js. What is externalsitesPDF.js by the way?

Where in Analytics are you looking for to see how many pdfs have been downloaded?

Ben, the error occurs in IE, not in Firefox. I notice you edited the script, might it be because you changed something? Otherwise the other option would be to get ride of target="_blank"...

report abuse
vote down
vote up
stefan
May 10, 2008
Votes: +1

Thank you very much David, it works perfect for us!

I can see downloaded pdf and hits on external links. It even tracks if someone hits the back button, like: /external//history.go(-1)

We have most clicks on: /external//
What is happening in that case? Is it our "home?"

Saludos

report abuse
vote down
vote up
Richard N
May 12, 2008
Votes: +0

Hi David

Thanks for that, I think I had duplicated the javascript by accident, it looks to be working now.

I was looking under Top Content and typing in pdf and external as the filter criterion.

Cheers

Richard

report abuse
vote down
vote up

Write comment
smaller | bigger

busy
 

Latest comments

RSS feeds in Joomla:...
Also, have you used the standard Joomla ...
RSS feeds in Joomla:...
David, Is RSS Factory used to syndicate...
RSS feeds in Joomla:...
GreatGatsby - DS-Syndicate is for Joomla...