Google Analytics tracks a lot of things, but not file downloads – at least, not out of the box. Depending on the nature of your site, it can be very useful to see which downloads were most popular. File downloads can be tracked using events, and better yet, the whole process can be automated entirely withing Google Analytics without changing a line of code of your website using Google Tag Manager’s Auto Event Tracking.
Monitoring Clicks Automatically
The Google Tag Manager (GTM) can be configured to monitor clicks on download links so that it fires an event tag. These event tags can be tracked in Google Analytics.
Events tags can be fired in different ways. The firing mechanism can be hardcoded in a link:
However, this would require you to change every link on your website to include the required
onclick attribute. A better way is to configure the Google Tag Manager’s Auto Event Tracking to listen for link or button clicks, then fire the event with the link text as the event label. This will require you to edit no code on the website at all.
Setting up Auto Event Tracking
<iframe> in your website. It is the tags added by the Google Tag Manager that we’ll use to send events to Google Analytics.
Once configured, visit the Variables section of the Google Tag Manager. It looks like this:
You will notice that there are five built-in variables available: Event, Page Hostname, Page Path, Page URL and Referrer. We’ll need to add two more to be able to track download clicks. To do so, click the Configure button and add the Click Text and Click URL built-in variables:
Now we’ll need to create a tag. Visit the Tags section of the Google Tag Manager.
Add a new tag. For the tag type, pick Classic Google Analytics, and configure it as follows:
For the Web Property ID, fill in your Google Analytics ID. You can find in Google Analytics administration panels, or from the tracking code you pasted into your website when you set up Google Analytics; it’s of the form UA-xxxxxx-x. We’ll be tracking events for this tag, so select Event for the Track Type. The Category and Action are free text; we’ll be using them in Google Analytics. Lastly, connect the Label to the built-in variable “Click Text” we activated previously. You can either type the name (in curly braces) or click the button next to the label to select it from a list.
Next. configure how the tag is triggered. Click the Trigger section and a screen will come up stating that the Tag fires for “All Pages”. We want it to fire for clicks, so click the
+ button (top right) to add a different trigger type.
We’ll want this tag to fire when a link is clicked, so select Just Links. Also, we’ll want the tag to fire only when a link is clicked that points to a resource ending in .pdf, so we’ll configure that, too:
Here, we’ll look at the Click URL value for the link (remember the Click URL built-in variable that we activated?) and see if it ends in PDF. The tag will fire only if it does. It is possible to select a regular expression instead of “ends with”, which will allow you to track files with names ending in a variety of extensions.
This is all the configuration we need. Save your trigger and save your tag.
IMPORTANT: There is a final thing that we must do: publish the changes. Google Tag Manager does nothing unless you publish the changes to your website. To do so, find the big Submit button and click it. Give your version a name, and click Publish. Now Google Tag Manager is active and will fire events when visitors access PDF links on your site.
Testing the trigger firing mechanism
Now we’ll need a way to see if the events actually get fired when a visitor to our website downloads a PDF file. In Google Analytics, we can actually see the events being fired in real-time. Open up your Google Analytics, and find the section Events under Real-time:
Here, I clicked a PDF link on my site to see if the events worked. My click fired an Event tag, with category “Downloads” and action “Download” as previously configured. By clicking the Event Category, we can drill down and see which document was downloaded:
And there it is. The text of the link that I clicked was actually “here” as in “click here to download”. That’ll teach me to pick better link texts!
After a while, this information will start appearing in the reports Events under the Behavior tab and you’ll be able to see exactly which files were downloaded, and their number of downloads, over time.