Tracking the quantity of PDF downloads is a very common wish for business to business and/or lead generation campaigns. It makes sense right? You understand that if a visitor is requesting a PDF then that visitor wants to learn more and is interested in what they’ve learned from your site so far.
The problem with trying to track PDF downloads as a conversion point or a goal is that a PDF document is different than a normal page. You cannot insert Google Analytics code in that PDF to have it track like you would another page. That means you won’t be able to set it up as a goal in your Analytics profile.
The Good News
The good news is that it’s simple enough to setup Google Analytics Event Tracking. In a nutshell, what this will do it record an “event” each time the PDF link is clicked by a user. Here’s Google’s overview of how to set it up.
If you’re too lazy to read through all the documentation provided by Google, here’s the tl;dr:
Before adding the onClick, the link would look like this:
<a href="/some/pdf/file.pdf">Download this PDF</a>
After adding the onClick, it would look something like this:
<a onclick="_gaq.push(['_trackEvent', 'PDF', 'Download', '/some/pdf/file.pdf']);"" href="/some/pdf/file.pdf">Download this PDF</a>
Then, once the link is clicked, you will record an “Event” in Google Analytics with the category equal to PDF, the action equal to Download and the label equal to /some/pdf/file.pdf. Pretty simple right?
The Bad News
Now it’s time for the bad news. I’m typically in the consultant position. I’m trying to convince a site owner, marketing manager or webmaster why they should go through the trouble to tag each and every “a href” PDF link with this onClick attribute, each of which needs to be customized so we can measure appropriately. This is a lot of work. Typically, the project doesn’t even start. Other times when it tagging these links becomes a project, it rarely is done perfectly and almost never is kept up with. This leads to inconsistent reporting.
The only thing worse than no tracking is inaccurate tracking. There’s nothing worse to a marketing professional (who’s trying to justify one’s own existence) than having to explain that the reporting is invalid because there were errors made in the setup of the analytics. However, IT HAPPENS ALL THE TIME. It’s insanely frustrating.
This blog, after all, is titled “Automate Everything”. Hopefully my solution to this problem won’t disappoint. I’ve been learning Javascript, more specifically JQuery, over the past several months. I was thinking about how to solve this problem. How can I use my new skills to make it insanely easy and efficient for people to add event tracking for Google Analytics to all the PDF links on their site? I’m happy to say, I’ve figured it out.
PDF Event Tracking Solution
My solution will allow you to track every PDF link on your site by just pasting a few lines of JQuery in the footer of your site. Here’s my solution:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$("a[href$='pdf']").each(function(index) {
pdfLabel = $(this).attr('href');
pdfOnClick = "_gaq.push(['_trackEvent', 'PDF', 'Download', '" + pdfLabel + "']);";
$(this).attr("onClick", pdfOnClick);
});
</script>
You should be able to place this code in the footer of almost any site and when the page loads, it will go through all the <a href="" tags and look for links with the href ending in .pdf. When it finds one, it simply adds the onClick attribute to perform the event tracking in Google Analytics.
If your site already includes the JQuery framework then there’s no need to repeat it. Simply omit the opening <script> tag and include the rest like so…
<script>
$("a[href$='pdf']").each(function(index) {
pdfLabel = $(this).attr('href');
pdfOnClick = "_gaq.push(['_trackEvent', 'PDF', 'Download', '" + pdfLabel + "']);";
$(this).attr("onClick", pdfOnClick);
});
</script>
The only thing you need to keep in mind is that this code needs to be placed as far down on the page as possible. Once this code executes, it will only have an effect on the page elements (DOM elements) that have already been loaded by the browser.
This is a very quick, highly efficient way to enable tracking of your PDF downloads using event tracking in Google Analytics. I’m actually quite surprised that Google doesn’t offer suggestions like this on their help pages.
I hope you’ve found this helpful. Please leave a comment if you have questions and share it if you have found it to be helpful. Thanks for reading.
Happy automation!