Track non HTML files with Google Analytics

Google Analytics is based on Javascript. Since Javascript can be placed only in HTML files, tracking non HTML files like pdf, zip, mp3 and others is a little bit tricky. But it is possible.

In order for Google Analytics track downloads of an mp3 file from your site, do the following:

1. Add the Google Analitycs tracking code ABOVE the the link to your downloaded file. If you already have the Google Analytics code in your site footer, and you probably do, place the same code again, but remove the call to _trackPageview().


<script type=”text/javascript”>
var gaJsHost = ((”https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(”%3Cscript src=’” + gaJsHost + “’ type=’text/javascript’%3E%3C/script%3E”));
<script type=”text/javascript”>
var pageTracker = _gat._getTracker(”UA-xxxyyyy-z”);

2. Edit every link you want to track, in the following way:
In case your link looks like this:

<a href=”/downloads/file1.mp3″>Hava Nagila</a>

change it to look like that:

<a href=”/downloads/file1.mp3″ onClick=”javascript: pageTracker._trackPageview(’/downloads/HavaNagila’); >Hava Nagila</a>

The clicks on this link will appear in Google Analytics as if the page /downloads/HavaNagila was actually viewed, even though there isn’t really such a page.

You can read more about this issue and others in Google Help Center.

This entry was posted in google analytics. Bookmark the permalink.

2 Responses to Track non HTML files with Google Analytics

  1. g1smd says:

    Beware that two calls to _initdata on the same page will register as two visits to that page, however one of them will register no other supporting data for that visit.

  2. yair says:

    Hi g1smd,
    I’m almost sure that removing _trackPageview() is enough and only one visit will be recorded.
    I will check it out and update.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>