Google Analytics Won’t Track Your PDFs – but We’ve Fixed That.

— Written By

Google Analytics is most often (almost always) implemented by adding a small snippet of javascript to a webpage. Once the page has finished loading, the Google Analytics script can collect whichever metrics are needed about the user and the nature of their visit and make a record of it by passing the data back to Google’s servers. However, the same isn’t true for PDF files on your website. Your PDF files are standalone files (frozen in time), already packaged and ready to be served out – in essence, there isn’t a way for you to add the necessary Google Analytics javascript snippet to a PDF like we can for a webpage. Does that mean you can’t record any information about your PDFs? No. There are a few work arounds.

Option 1: Fall back to your Server Side Analytics

You could roll your own server-side analytics software like AWstats or Webalizer to help take a closer look at all of the traffic happening on your web server, the number of PDF hits will most certainly be accessible here. Unfortunately the user interfaces for a lot of these server-side analytics packages haven’t matured with time and they can be a real pain to use. There’s also a chance you already have been recording the vast amount of your web traffic in Google Analytics and having your data split between two different sources isn’t ideal. Fortunately, we have a solution.

Option 2: Hijack the click on the front end (easiest, and least technical)

You can use a front-end library like jQuery (or plain javascript) to temporarily intercept the click, just long enough to fire off a note to Google Analytics to interpret the action as a pageview, and then pass the user along to their intended destination. The interception and ping to Google happen at lightning fast speed (we’re talking milliseconds here), too fast for any user to perceive any latency at all. It’s just as if they had clicked the link and no data had been recorded.


$(function(){

/* only crawl links in your content region */
$('#content a').each(function(){

var linkUrl = $(this).attr('href');

/* make sure it's a PDF ... you could also make sure it's not an external resource here */
if(linkUrl.match(/\.pdf$/)){

$(this).click(function(){
_gaq.push(['_trackPageview', linkUrl]);
});

}

});

});

Option 2: Pass all PDFs through a server side file like “tracking.php” using mod_rewrite


# .htaccess file ... RewriteEngine On
RewriteRule ^uploads\/([^\/]+)\.pdf$ /tracking.php?filename=$1 [L]


You would have to tweak the regular expression in the RewriteRule to fit your particular use case, but you get the point. Now all PDFs matching our expression will be passed through tracking.php which with the help of this Google Analytics PHP Library will ensure they get properly recorded into Google Analytics behind the scenes (again with a minimal latency impact to the user). Now, I should also mention that while this particular library works, it is no longer supported because Google has provided some docs that will allow developers to do this on their own using cURL and POST parameters.

Which option does Extension IT use?

We use a combination of both, hijacking the click where appropriate, and recording the PDF using a tracking file when needed. The reason for our hybrid approach really centers around the fact that some folks may link directly to a PDF on your site from their site, blog, twitter, etc -> http://yourdomain.com/some-file-they-love.pdf . In this particular case, any javascript solution you had come up with has been circumvented. Keep in mind that depending on how you’ve structured your site, Google may also be indexing these PDF files directly and serving them up in search results, in which case you most certainly won’t get to run any javascript.

Written By

Photo of Mike Vysocka, N.C. Cooperative ExtensionMike VysockaBusiness and Technology Application Specialist (919) 513-3362 mike_vysocka@ncsu.eduExtension Information Technology - NC State University
Updated on Jul 20, 2015
Was the information on this page helpful? Yes check No close
Scannable QR Code to Access Electronic Version This page can also be accessed from: go.ncsu.edu/readext?299481