Google Tag Manager Tutorial Part 2: Tracking Downloads in GTM Version 2

Google Tag Manager Menu

|

27. August 2015

 | 

In Part 1 of our updated Google Tag Manager Version 2 tutorial we showed you how to track outbound links. If you have not already read it, please at least have a look at the introduction: all the requirements described apply here as well.

In this second part, we will walk you through the process of setting up tracking for downloads from your website. The setup below will allow you to keep track of the number of times users download different types of files from your website. Every time a user clicks on a link that contains the file extensions you define (for example, .pdf) an event will be recorded that is labeled with the type of the file and the URL of the file. You then can easily see in Google Analytics how many pdfs, for example, were downloaded from your site, and which ones.

This part also introduces a major component of Google Tag Manager: user-defined variables. Variables let you customize the pieces of data that you use in tags and triggers.  Google Tag Manager comes with many useful built-in variables but you can also create your own, as we will do in the first step:

1. Create a Filetype Variable

This user-defined variable is made up of a few lines of JavaScript that chop the file extension off the end of a link, so that you can tell what type it is. The custom JavaScript comes from Simo Ahava’s excellent blog post on Google Tag Manager Macros (for GTM version 1).

  1. Under User-Defined Variables, click "New".
  2. Replace "Untitled Variable" with a descriptive name like "Filetype".
  3. Choose Type: Custom Javascript.
  4. Copy and paste the following into the "Configure Variable" input box for Custom Javascript:
function() {
  var ext = {{Click Element}}.pathname.split(".");
  return ext.length>1?ext.pop():'html';
}
  1. Click "Create Variable" to save.
  2. Enable the built-in "Click Element" by checking the box next to "Click Element" on the Variables page.

Configuring a custom variable for download link clicks in Google Tag Manager version 2

2. Create a Download Link Click Trigger

This trigger determines whether a click on a link is a file download. It gives a long list of file extensions, and if a link url contains one of them, then the rule says, yes, tag, do your action. Optionally, you can customize these file extensions to include only the ones that interest you, or add other ones specific to the type of downloads you offer on your site.

  1. Under Triggers, click "New".
  2. Replace "Untitled Trigger" with a descriptive name like "Download Link Click".
  3. Configure Trigger:
    1. Select Targets "Just Links". This requires that the click is on a link, not on anything else, like a button or other page element.
    2. Tick checkbox "Wait for Tags" and set the Max wait time as 2000 milliseconds. This allows the trigger to wait up to two seconds for all the tags that use this trigger to be ready before taking the user to the link.
    3. Tick checkbox "Check Validation". This checks that a link is actually a valid link that takes the user to another page.
  4. Enable When:
    1. Select options: Page URL matches RegEx ".*". This enables the trigger on all pages of the website.
  5. Fire On:
    1. Select options: Click URL matches Regex "^.*\.(7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip)$". 
      This regular expression checks if the link clicked ends with one of these file extensions that are usually downloaded, not opened in the browser.
  6. Click "Create Trigger" to save.

(Updated screenshot as of 19.7.2016)

 

Change the list of "|" separated file extensions in the  "Fire On" options if you only want to track certain types of downloads.

 

3. Create a Download Event Tag

Now that you have set up the necessary variable and trigger in the previous steps, you can create the event tracking tag. This will record clicks on links that have the file extensions that you set up in the trigger in Google Analytics.

  1. Under "Tags", click "New".
  2. Replace "Untitled Tag" with a descriptive name like "Download Event".
  3. Choose Product: Select "Google Analytics".
  4. Choose a Tag Type: Select "Universal Analytics" or "Classic Google Analytics", depending on which tracking codes you currently use on your website.
  5. Configure Tag:
    1. Tracking ID: Enter the Tracking ID of the Google Analytics property for your website.
    2. Track Type: Select "Event".
    3. Category: Enter "Downloads".
    4. Action: Enter "{{Filetype}}".
    5. Label: Enter "{{Click URL}}". This built-in variable must be enabled first, see Part 1, step 1 of this tutorial.
    6. Non-Interaction Hit: Select False.  This means that this event will change your bounce rate, because it will be counted as an interaction on your site. You should decide if you prefer to count downloads as interactions or not, and adjust this accordingly.
  6. Fire On:
    1. Select "Click".
    2. From the "Choose from existing Click Triggers" menu that pops up, select the "Download Link Click" trigger you created in step 2.
  7. Click "Create Tag" to save the tag.

Creating a Tag for download link clicks in Google Tag Manager version 2

The download events that this tag tracks will appear in the events report in Google Analytics with the category "Downloads", the action will appear as the type of file (pdf, doc, etc.), and the label will give the url of the link.

That’s it!  Debug the setup (see step 4 of part 1 of this tutorial) to make sure it works the way you want, save a version, and publish to start tracking downloads in Google Analytics.

Now that you have used some of the built-in tags and variables that Google Tag Manager version 2 offers, you are hopefully ready to handle a little more complexity. Part 3 of this tutorial will deal with tracking form submissions, and will require a little more customization for your specific website to work properly.

Have you set up outbound links and/or downloads tracking in Google Tag Manager? Leave us a comment and let us know how it went! Need help with Google Tag Manager? Send me an email!

Comments

Hi Addy,

Thanks for the comment, we are glad you found the posts useful. Unfortunately we do not yet have a video tracking tutorial. It is a good idea though and is something we hope to look at creating soon.

This blog post for setting up downloads and your other post for setting up outbound links was so helpful and easy to implement. Thank you so much! Do you have similar instructions for setting up video tracking in GTM? Thanks!

Hi Benjamin

I am not 100% sure I understand the question but I think you are wanting to track when the user actually downloaded the PDF to their device and when they saw it in the browser preview seperately. In this case unfortunately you are not going to be able to using GTM and GA.

The PDF preview does not take place on your website and so there is no GTM container on the preview site for you to track the download. It may be possible to force the browser to download the PDF without displaying it in certain circumstances which means every click would be a download. There are details of various methods to accomplish this in several places but I will include one link as an example.

http://stackoverflow.com/questions/2598658/how-to-force-a-pdf-download-a...

Another option would be to put the pdf inside a zip file as browsers wont display a preivew thereof. The setup we took you through in this blogpost includes tracking for zip files.

The final option is to use a service that tracks interactions with PDF documents. You can find a few options in this thread:

https://www.quora.com/Are-there-any-solutions-to-track-if-a-PDF-has-been...

Hope this helps if you have any other questions please let us know.

 

Hello there guys, I have a question. Many websites do not have a download option for their .pdf's; you just click the .pdf and your browser opens the file in a preview in another window. From the dropdown menu in that page you can then download the file. Meaning every page visit of this file link won't be a download. How can I track the download in this kind of situation ?

Thank you,
Benjamin

Hi Timbo

Thanks for your question.

Yes absolutely. You can add to the regex used in the outbound link tracking tag to do this.

Your's should look something like this:

{Click URL} does not match regex: .*example\.com.+

You can add another URL by changing this regex to include a list.

{Click URL} does not match regex: .*(example\.com|example2\.com).+

This will stop outbound link tracking if the URL is example.com or example2.com. If you replace example2.com with the domain of your download host it should work as you want it to.

Hope that helps. If you have any more questions just let us know.

Thanks so much for this most useful walk through. From the previous blog on tracking outbound links and this one on downloads I have managed to set up both. But as my downloads are hosted on a external site I get both Outbound and Download tags firing on the Outbound Event. Is there a way to exclude outbound links from Downloads?

Hi Sam

Thanks for reaching out to us.

Once you have published the container you should start seeing data in the real time analytics almost immediately if your tags are triggering. If you need to check if your tags are triggering properly you can use the preview mode in GTM (Preview mode will also send events to GTM so you can check triggering on the page and the event in Real-Time analytics). Or you can use a Google Chrome extension like Omnibug or dataSlayer.

I hope this helps.

Regards

Jonathan

Just set this up, can't see any events showing in Real Time (yet) will it take a while to show on our live site?

This is by far the best explanation I've found for setting up outbound link events in GTM v2. Thanks.

Christina Meyer's picture

Hi Megan, the best way is to set up event tracking with Google Tag Manager, create a goal for this event in Google Analytics and import the conversion in AdWords.
Check out our tutorial for setting up goals in Google Analytics.

Add comment