Back
Performance marketing — April 30, 2014

Tracking PDF downloads as pageviews

Image
Robert Petković Google Analytics Specialist
Image

Have you ever had the task to measure how many people actually read those lovely PDF files that were too heavy to convert into HTML?

 UPDATE: Google Tag Manager will soon be available in version 2 only, so follow 5 easy steps for setting up PDF download tracking in Google Analytics using Google Tag Manager v2.0.!

Did you actually know Google Analytics is able to track PDF downloads as well? If you are a Google Analytics geek as I am, you are probably tracking those already. But are you sure you are tracking them the proper, easy way? Also, did you know that some latest Google Tag Manager updates enable you to track all PDF files within your website using a rather simple procedure? Let’s find out how!

There are many blog posts out there covering this issue for a long time, but most of them suggest that: “You should track your PDF documents as Google Analytics Events”. I don’t say that’s wrong, I just think PDF documents might be treated as (virtual) Pageviews, since PDF documents are usually opened within the browser itself. In most cases they are read on the screen, unlike spreadsheets which we open for further calculations in another application, or some files we use for application installation.

That’s exactly the reason why I’ll write about creating Virtual Pageviews while clicking to PDF documents, but you are free to track them as Events too, if you would like. (I suppose you have your Google Tag Manager installation ready. If you don’t, feel free to check all about it on Google Tag Manager website. Let’s say you have previously implemented basic Pageview tag, too (and you know the purpose of Tag, Rule and Macro). However, if you are not sure how to do it, please refer to Google Tag Manager Documentation.)

Recently, Google Tag Manager announced a really cool feature: The Event Listener tag. It is a Tag Manager “code” that you use for listening to what is going on on your web page(s). There are few different types of event listeners available, and you can learn more about them at Google Tag Manager help. Today, we will be using only one of them: the link click listener. It fires every time a user clicks to an anchor link, giving us further information about that click that we can use for our Google Analytics reports. Since PDF documents are mostly downloaded by clicking to a certain website link, this particular listener would be an appropriate actor for this example.

Step 1: Link Click Listener tag

How do we set it up? Firstly, we should create one new Tag for that event listener. Let’s call it “Link Click Listener”. In your Tag Manager, select New – Tag and name it “Link click Listener”. Under “Tag Type” select “Event Listener” and then choose “Link Click Listener”. Be sure to enable it to trigger on every page by adding “All Pages” rule to that tag. If you have PDF documents on few pages only, you can set up a new rule that will trigger this tag only on those pages, but let’s stick here to “All Pages” since this listener might be useful for other purposes too. Save your tag and we are ready to move further!

Step 2: One simple rule

If we want to include clicks to PDF files only, we should set up some rules. In order to do that, let’s click to “New – Rule” which we will name “PDF links only”. You should know that link click listener we have set up before actually triggers a reserved event called gtm.linkClick once we click on any webpage anchor. That is why we will first add an event that equals (or contains) “linkClick”, followed by one Regular Expression rule that says “In case this link URL contains PDF only”. That should look like this:

Now save your tag and take one step forward.

 Step 3: Virtual Pageview

Remember how we said at the beginning we will use virtual Pageviews for PDF downloads and not events? OK, let’s configure a new Tag that will send some virtual Pageview information to Google Analytics, but only when we click to PDF document everywhere within your website.

First create a new tag and name it “PDF Virtual Pageview”. Select its type to be “Universal Analytics” and type the appropriate Google Analytics ID (or let the macro populate that value, the way I am using Tag Manager for a while).

Now select “Pageview” under type and expand “Basic Configuration”. Here under “Document Path” we should put the PDF document file name and it will be shown together with all other Pageviews in your Google Analytics reports. The easiest way is to put here the value of “element URL” macro this way:

Don’t forget to fire this tag on PDF links only! Under “Tag Rules” select that rule we created before: “PDF links only”, save your tag and voilà! We have a Pageview tag that will send it’s information to Google Analytics only when a user clicks on PDF document anywhere on our website! Isn’t that great? In the past you had to edit the website code (or use CMS to edit the HTML) on every web page with PDF document on it, just to be sure you are tracking all PDF downloads! Plus, once you edit all website code, someone will probably add another PDF in a day or two without telling you that, and you will have incomplete data! Using these few simple steps you saved yourself a lot of time and effort!

 Step 4: Publish!

Now, you can create a new Tag Manager container version, publish it and your website will immediately start sending the proper PDF download information to Google Analytics. You will be able to see the full URL of that document under “Behaviour”‘ section, among other Pageviews.

Step 5: One Extra Tip with One Extra Macro

But – wait! There is something wrong here, especially if you are a nit-picker! I can hear you asking: “But wait, other Pageviews’ URLs doesn’t start with “http://” but with a slash only! Those other pages also have titles in reports, while PDFs don’t! Can we somehow fix that?”.

Well, today is your lucky day and you can get extra tips for the regular price! Google Tag Manager recently knows how to fix that and help us, so let’s make it work.

We should start by creating one macro that returns only the portion of the URL after the domain name. Let’s name it “element path”, because it is very similar to existing “element” macros (and I hope Google will soon make it a part of standard macros package that appears once you create new Tag Manager container). Its type should be “Auto-event Variable” with Variable Type of “Element URL” and (very important!) a Component Type “Path”. Save that macro.

Step 5b: Virtual Pageview revision

Now go back to the “PDF virtual Pageviews” tag we created earlier and take a look at its Basic configuration. We should replace the existing “Document Path” value with the macro we just created: “element path” and that’s it! Our Pageviews will no longer contain domain names and will be consistent with other website Pageviews!

But what about the Document Title? Well, if you know SEO or know basic principles of good web writing, your PDF document link will probably look like this: “Please take a moment and download our Product catalogue as a 5 MB PDF file”, with “Product catalogue” being an actual link to a PDF. Google Tag Manager is getting that text via macro called “element text”, so that’s the actual macro value you should put under “Document Title”. Therefore your tag Basic Configuration should at the end look like the example below:

Save the tag, create and publish a new container version and now you have PDF documents with URL and title in your Google Analytics reports! As easy as one-two-three, right?

Just one more thing before we finish! If you are a lousy copywriter and your links to PDF documents usually look like “You can download our PDF price list here.” then you will not fully benefit from this post, nor should we be blamed for the fact that all your PDF documents in Google Analytics reports are titled “here”. But we sincerely hope you will soon figure out how to solve this problem using your own CMS. 😉

Feel free to comment on this solution or to send even better examples, I would be glad to analyze or comment on them. And if you find this solution useful, let me know that too, so that I can prepare more posts like this!