GTM & GA: Track all downloads with one tag

There are a few things that live in almost every Google Tag Manager container I set up, and I'll be covering several in the coming days/weeks. One of those is download tracking. GTM's auto events are simply wonderful, because most of the time, there's no need to handle one-offs at all anymore; I just add one tag and one trigger, and BAM, all downloads are tracked.Not only that, you can dynamically send more useful data to GA than you probably ever have in the past. Here's how it works!

Step 1: Enable default variables

You'll need the "Click URL" and "Click Text" variables for this.click_vars

Step 2: Set up the GA tag.

Before setting up any GA tag, it's definitely a good idea to think through the data architecture you want to create in GA. We're going to send this as a GA event, which means we'll need to define these parameters:

  • Event Category: downloads
  • Event Action: the file URL (of the file that's being downloaded)
  • Event Label: I like to put the text of whatever the user clicked here
  • Note: Remember that the URL of the Page that the file was downloaded *from* will always be populated in the Page dimension, so there's no need to explicitly include it as an event parameter.

Let's break this down.

Event Category: downloads

This value never changes, so you can just type "downloads" into this field in GTM (see below). I like to keep events in all lowercase, because I often pass URLs as event parameters, and since URLs are usually case-insensitive, I like to apply lowercase filters to these fields in GA. Since I plan to lowercase the data anyway, I'd rather just be consistent in GTM.

Event Action: file URL = {{Click URL}}

We enabled the {{Click URL}} variable for this purpose; it will contain the URL of the element that was clicked. This is pretty straightforward. Just one note: if you're managing a site with multiple domains, and doing cross-domain linking between them, and if it's possible that one domain might include links to download files on another domain, {{Click URL}} will give you trouble. I'll go over a fix for that in a future post. If you're not sure if this applies to you, don't worry about it and just stick with {{Click URL}}. You'll still get your data; it'll just be a less easily readable.

Event Label: text that was clicked = {{Click Text}}

Again, we enabled {{Click Text}} for just this occasion. It will contain the text of whatever you clicked.When complete, it looks like this:download_event_params

Step 3: Create and apply the Trigger.

Let's assume for the moment that we only want to track PDF downloads. In that case, the trigger works like this:

  • Track every time the user clicks...
  • ...on a link...
  • ...that points to a PDF file.

This is it:downloads_trigger_pdfTo add more file types, the most straightforward thing would be to create additional triggers, and apply more of them to this same tag. But, due to what I like to think of as a healthy programmer's laziness, I would far prefer to just have one slightly-more-complex trigger.downloads_triggerThe only difference there is at the end. Instead of matching the {{Click URL}} to be sure it contains ".pdf", we're using a regular expression match, and matching against this regular expression: (\.pdf|\.jpg|\.gif|\.mp3). You can find regular expression tutorials and tools all over the internet, and I'll add a primer here eventually, but for now, here's what you need to know:

  • "." is a "wild card" character in regex - to use it to mean an actual period, you need to escape it first with a backslash, like so: "\.".
  • "(" plus "|" plus "|" plus ")" is a big "OR" statement. For example, (x|y|z) matches x, y, or z.

With that, the regex above should make sense. We're telling it to match anything with a pdf, gif, jpg, or mp3 extension. You can easily add more to this list, or remove some if they're unwanted.

Step 4: Preview & Publish.

Naturally, you'll want to Preview to make sure this is working, and then publish it to start collecting download data immediately.

Step 5: See data in GA.

Here, I'm looking at the Behavior > Events > Top Events report. I clicked into the downloads category, and I'm viewing the Event Action with Event label as the secondary dimension.downloads_ga_dataThe {{Click Text}} in the Event Label is so useful! This client has many different brochures available for download. We can now easily roll them all up (and differentiate them from other downloads) by viewing only Event Label and looking for "DOWNLOAD A BROCHURE".And by the way, we can also easily see where these brochures are being downloaded. Still in Behavior > Top Events, I've set my primary dimension to Page, and my secondary dimension to Event Label. Then, I filtered for Event Labels containing "DOWNLOAD A BROCHURE".downloads_filterNow we have this data:downloads_ga_page_dataYou can see that around 3/4 of all brochure downloads come from a brochure-specific page, and the rest are from CTAs on a variety of other pages.

Set up a custom report for even better data

With a custom report, you can see the Page, the text of the link, and the file that was downloaded all at once:downloads_custom_report_setup...gives us data like this:downloads_custom_report

Resources

Whenever possible, I'm going to try to provide reusable resources that you can download and put into action right away.

GTM Container Export
(includes downloads only):
http://angelagrammatas.com/gtmtips/downloads_gtm_angela_grammatas.json
Custom Report Template: https://www.google.com/analytics/web/template?uid=OUgKseVSQ2G9z3HGp7GxbA


 
I hope that's helpful, and a good start with these GTM posts. More to come in the near future!

Previous
Previous

New painting: Polygons (7&8)

Next
Next

GA Summit 2015 - awesome as always, and they used my quote!