Google Tag Manager | Web Analytics

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

Google Tag Manager Menu

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!

16 Comments

  1. One mistake I made that I had
    One mistake I made that I had trouble with…please correct me if I’m wrong. I got an error message in the “filetype” variable when I tried to preview this. It looks like the error was in {{element}} below:

    function() {
    var ext = {{element}}.pathname.split(“.”);
    return ext.length>1?ext.pop():’html’;
    }

    It should be {{Click Element}} and “Click Element” box should be checked in the enabled, built in variables page (which I didn’t originally have checked).

    After I made that change, I got the tag to work and show up in my GA events tracking.

  2. Hi Jason,

    Hi Jason,

    Yes, good catch.  {{element}} was a relic of Google Tag Manager version 1.  I’ll update the post. Thanks!

  3. Thank you so much for this. I
    Thank you so much for this. I spent an hour with Google Analytics Chat people today, and they told me this was not possible without using an onClick event on every single link I wanted to track. I was just sure it was possible using just GTM, and lo and behold, you’ve illustrated it perfectly. This is a win — high five!

  4. Hi Jason, I found the same
    Hi Jason, I found the same error. Tag Manager preview told me that {{Filetype}} was unknown and so I have changed it into {{element}} and go it to work 🙂

    I see the tracking in GA

  5. Hi Luca,

    Hi Luca,

    Glad the tracking is working for you.  If you enable the built-in variable “Click Element” then you should be able to use {{Click Element}} in the Filetype variable. Was that your issue?

    best,

    Laura

  6. Can you describe tracking PDF
    Can you describe tracking PDF downloads that report to AdWords instead of Analytics? I have the Conversion action set up in AdWords but am unclear on setting up the tag in Tag Manager. Thank you!

  7. Just set this up, can’t see
    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?

  8. Hi Sam

    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

  9. Thanks so much for this most
    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?

  10. Hi Timbo

    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.

  11. Hello there guys, I have a
    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

  12. Hi Benjiman

    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-automatically

    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-opened

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

     

  13. This blog post for setting up
    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!

  14. Hi Addy,

    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.

Add a Comment

Your email address will not be published.

By clicking “Submit” you accept our Data Privacy Notice.