(877) 473-9016


PDFs and Google Analytics: jQuery Script To Track Files

Friday, September 17, 2010
Content Management SystemsTechnology

The trouble with tracking PDF downloads (or any other file, such as PPT, MP3, ZIP, etc) with Google Analytics is not necessarily getting Google Analytics to track them. Google tells you exactly how to do it. The problem is automating the tracking so you have the flexibility to upload any type of file through your CMS (like WordPress) or via FTP without requiring website admins to do anything special or using a dedicated upload field within your CMS interface.

Here’s what Google says to do:

1. Set up Event Tracking in your Analytics Tracking code (very easy –just add a line of code).
2. Add a JavaScript method in the head of your document to delay the outbound click by a fraction of a second.
3. Update your outbound links to call the new function without first following the link.

This seems simple, but #3 means that you have to modify each link to each document on your website to make sure they are tracked. This is where developers usually turn to a specific upload field in the CMS interface, but that doesn’t address FTP uploads or text area uploads. And, since some solutions cover all outbound links (like to other websites, for example), you might end up tracking links to other websites in addition to the files you are hosting, which may not be your goal. So, here is Workbox’s solution for WordPress:

Rather than editing the links themselves, we use jQuery to intercept every click on every link. Then our code checks to see whether the link is outbound or points to a document (like a PDF, MP3 or any type you specify). If it is the type of document specified, the Google tracker is launched, so this info is sent to Google Analytics before the browser redirects to the link. Here’s the code we are using for your reference:

<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(“<?php echo $option[‘ga_id’]?>”);


pageTracker._setDomainName(“.<?php echo $option[‘domain’] ?>”);

} catch(err) {}


<script type=”text/javascript”>

jQuery(document).ready( function() {

jQuery(‘a’).click(function(ev) {

var link = this.toString();

var aExt = new Array(‘<?php echo implode(“‘,'”,$option[‘track_ext’])?>’);

var aPieces = new Array();

var aPieces = link.split(‘\.’);

if (aPieces.length>0)


var ext = aPieces[aPieces.length-1];

for (var i = 0;i<aExt.length; i++)


if (aExt[i] == ext)










Red – inclusion of the tracker script;
Blue – launching the tracker;
Black – detecting the links we need;

We think this is a fast and efficient way of handling this because:

1) It allows you to use an existing and trustworthy statistics service (Google Analytics).
2) You don’t have to add code to every link.
3) It is highly customizable since you can easily change what kind of links you want to track.
4) Installation is easy and the code is free!

We implemented the solution in the form of a WordPress Plugin so we can configure the settings such as the domain name, the file extensions, etc. in the admin panel.

Have thoughts or other ways of tackling this issue? Let us know!

Thanks, Eric Weidner & Gleb Aksyutchenko