Google Tag Manager | Web Analytics

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

An updated version of this blog post was published.

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? Need help with Google Tag Manager? Send me an email!

Don't miss our GTM Articles.

Check out our Analytics services.