UPDATE (27.8.15): This blog post was written for Google Tag Manager version 1, which is now obsolete. See the updated version of this Google Tag Manager Tutorial for GTM version 2.
Part 2: Downloads
In Part 1 of our Google Tag Manager Guide we showed you how to set up tags and rules 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 the 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 the third major component of Google Tag Manager: Macros. Macros let you customize the pieces of data that you use in tags and rules. Google Tag Manager comes with many useful pre-configured macros like {{element url}}, but you can also create your own macro, as we will do in the first step:
1. Create a Filetype Macro
This macro 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. This macro comes from Simo Ahava’s excellent blog post on Google Tag Manager Macros.
- Click “New” and select “Macro” from the dropdown menu.
- Fill in Macro Name with “Filetype”.
- Select Type: Custom Javascript.
- Copy and paste the following into the input box for Custom Javascript:
function() {
var ext = {{element}}.pathname.split(".");
return ext.length>1?ext.pop():'html';
}
2. Create a Link Click Listener
See Step 1 of Part 1 of our Google Tag Manager Guide. If you already followed the steps in Part 1 to create the Link Click Listener, it is not necessary to make a second one. If you have not created a Link Click Listener so far, do it now.
3. Create a Link Click Download Rule
This rule 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
- Click New and select Rule from the dropdown menu.
- Fill in Rule Name with “LinkClick Download”.
- Set these Conditions:
- {{event}} equals gtm.linkClick
- {{element 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)$
- Click Save.
4. Create a Downloads Tag
Now that you have set up the necessary macros, rules, and listener tags 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 rule as file download events in Google Analytics.
- Click “New” and select “Tag” from the dropdown menu.
- Fill in tag name with “Downloads”.
- Select Tag Type: Google Analytics > Universal Analytics OR Classic Google Analytics, depending on which you use in the Google Analytics property for this site.
- Set the Tracking ID for the Google Analytics property for this site.
- Set Firing Rules: LinkClick Download.
- Select Track Type: Event.
- Set the Event Tracking Parameters:
- Category: Downloads
- Action: {{filetype}}
- {{element url}}
- Click Save.
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 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 event listener tags and macros that Google Tag Manager offers, you are hopefully ready to handle a little more complexity. Parts 3 and 4 of this guide will deal with tracking form submissions, and will require a little more customization for your specific website to work properly. Part 3 will show how to track a newsletter signup form, and Part 4 will illustrate a way to track contact forms.