SHARE

How to track file downloads in Google Tag Manager V2

Setting up tracking of file downloads in GTM V2 is much easier thanks to the new configuration wizard. It is more intuitive and takes you through the set up step-by-step.

Let’s have a look at the basic configuration for sending tracking of file downloads from Google Tag Manager to Google Analytics as events.

To set up this events tag you need to firstly create a trigger.

Create a trigger

This trigger will recognise every time someone clicks to download the file you want to track.

In the given example I am using a simple regular expression to capture a number of file types I want to track -.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$

Here the * means it will capture any repetitions of the file types it is next to, ie it will include file types doc and docx for Word documents, xls and xlsx for Excel spreadsheets, ppt and pptx for PowerPoint presentations.

Save the trigger and create a new tag.

Create a new tag

Give your tag a meaningful name so you can easily recognise what the tag is for.

We have previously created a variable (formerly known as macro) that stores our GA tracking code, which I use in the configuration settings.

This way I do not have to re-enter the GA property ID every time I need it. This variable does all the work.

Here I use the following but modify the fields based on what works for your business:

Category is Download

Action is Click

Label is {{element url}

Element url in the label field will store the URL of the file that was downloaded.

Advanced tracking

For advanced tracking, you can create a custom javascript variable with a code that will strip out the title of the downloaded file and store it in your GA. Have a look at Simo’s example of returning file name.

Set your tag to fire

Last step is to add a firing rule, ie select a trigger that will fire your tag.

Select the previously created trigger ‘Click to Download’ and you’re all set.

Violetta

Subscribe to our blog

Thanks for signing up!

Related posts

Most companies will see a discrepancy between the transaction volumes recorded via web analytics and those recorded via internal sales or financial database. This article focuses on how to find and reduce that discrepancy, to give greater credibility to your web analytics data.
Following on from our article on common Google Analytics setup problems, we are often asked why Google Analytics ecommerce tracking is not a 100% match with other records, and what is an acceptable level of difference.
Inspired by a talk from Richard Pickett at Ensighten, here is a checklist to run through to reduce the sources of mismatch. The focus here is Google Analytics Ecommerce tracking, but it could apply to other systems.
In summary, you wouldn’t ever expect there to be a 1:1 match, due to the different paths the two events take over the internet. The general consensus is that anything less than 4% of difference in transaction volumes is good, but could sometimes persist up to 10%.
Factors that affect this target rate include how many users have got ad blockers or disable Google Analytics (popular in Germany, for example), what proportion are on mobile devices (which suffer from more network interruptions) and how the purchase thank you / confirmation page is built.
So on to the list.
1. Are other Javascript errors on the page blocking the ecommerce event in certain situations?
The most common reason for the tracking script not executing in the browser is that another bug on your page has blocked it (see GDS research). The bug may only be affecting certain older browsers (like Internet Explorer 7), and have missed your own QA process, so the best approach is to use Google Tag Manager to listen for any Javascript error events on the confirmation page and send these to Google Analytics as custom events.
That way your users do the testing for you, and you can drill into exactly which browsers and versions the bugs are affecting.
2. Is the tracking code as far up the page as it could be?
If the user drops their internet connection before the whole page loads then the ecommerce event data won’t get a chance to fire. The best approach is to load the script at the bottom of the <head> element or top of the <body>. The Google Analytics script itself won't block the page load, and arguably in this one purchase confirmation page, the tracking is more important than the user experience.
3. Is the tracking code firing before all the page data has loaded?
The inverse of the previous problem: you may need to delay firing the tracking code until the data is ready.
This is particularly an issue if your ecommerce transaction data is ‘scraped’ from the HTML elements via Google Tag Manager. If the page elements in question have not loaded before the ecommerce tracking script runs, then the product names, SKUs and prices will be empty – or returning an error.
4. Is the problem only your ecommerce tracking script or just page tracking is general?
It could be that the way you are sending the transaction data (e.g. product name, price, quantity) is the problem, or that the page tracking overall is failing in some cases. You can pinpoint where the problem lies by comparing the pageviews of the confirmation page, with the number of ecommerce events tracked.
Caveat: on many sites, there’s another route to seeing the purchase confirmation page, which doesn’t involve purchasing (for example as a receipt of a historic purchase). In that case, you may need to capture a unique purchase event, which only fires when a new purchase is confirmed – but without any information on the transaction or products.
5. Are events from your test site excluded?
Most companies will have a development, staging or user acceptance testing server to where the website is tested, and test users can purchase. Are you blocking the tracking from these test sites?
Some possible ways to block the test site(s) would be:
Set up sub-domain specific blocking rules in Google Tag Manager
(or better) Divert the tracking from your test subdomains to a test Google Analytics account, using a lookup macro/variable
Set up filters in the Google Analytics view to exclude
6. Is your tag set with a high priority?
Tag manager only.
If you use Google Tag Manager and have multiple tags firing on the tracking page it’s possible that other tags are blocking your ecommerce data tag from firing. Under ‘Advanced settings’ in the tag editor, you can set a higher priority number for tag firing; I assume the ecommerce data to Google Analytics is always the first priority.
7. Are any strings in the product name properly escaped?
A common problem is apostrophes: if your product name contains a quote mark character, then it will break the following Javascript. See Pete’s bunnies – the strings in yellow are valid, and everything after the stray apostrophe will be misinterpreted.
The solution is to run a script across any text field to either strip out the quotation marks or replace any quotes with their HTML equivalent (eg &quot;).
8. Are your quantities all integers?
One of our clients was selling time slots, and so had the ‘quantity’ of the ecommerce tracking data equivalent to a number of hours. Timeslots sold in half-hours (e.g. 1.5 hours) were not tracking… because Google Analytics only recognises a quantity which is a whole number, so sending ‘1.05’ will not be recognised as 1.
9. Are any possible ‘undefined’ values handled?
It may be that the data on your products is incomplete, and some products that people buy do not have a name, price or SKU.
The safest approach is to have some fall-back values in your Javascript tracking code to look for undefined or non-text variables and post a default value to Google Analytics. E.g. If ‘product name’ is undefined then post ‘No product name’, or for price, the default should be ‘0.00’.
These will then clearly show up in your Ecommerce Product performance reports and the data can be cleaned up.
10. Are users reloading the page and firing duplicate tracking events?
Check whether this is a problem for your site by using our duplicate transactions custom report to see multiple events with the same transaction ID.
A solution is to set a ‘has tracked’ cookie after the ecommerce tracking has been sent the first time, and then check whether the cookie is set before sending again.
11. Are users going back to the page and firing the tracking at a later date?
The sessions column in the transactionID report in step 9 should give you an idea of whether the problem is repeat page loads in one session, or users revisiting the page in another session.
If you see duplicate transaction IDs appearing in other sessions there are a couple of possibilities to investigate:
Could users be seeing the page again by clicking on a link to an email, or from a list of historic orders?
Are there any back-end admin pages that might link to the confirmation page as a receipt?
In both cases, the solution is to have a different URL for the receipt that the one where the ecommerce tracking is fired.
If there are any other troubleshooting steps you have found helpful, please let us know in the comments or get in touch!

Many contact forms now use Javascript to submit and do not redirect to a new page. So to track the form, unless you trigger an event on the submit button, you need to listen for a piece of text (usually saying thank you).
We have created a custom HTML script that listens to the changes in the page and triggers an event called 'formSubmitted'.
This event can then be used to fire a separate tag with event details to Google Analytics.
We've tested this on our contact form at Littledata and here's how you can set it up too.
Step 1
The first step is to go through the contact form and see what the steps are in completing it.
On ours, you just enter the information in the fields and press "SUBMIT MESSAGE". When the message is sent out, the button will say "SENT!". Here the only thing that changed was the text on the button from 'submit message' to 'sent'.
We built this HTML script that listens to the changes on the page, but you'll need to change line 10 to be whatever the message is in your form.
You will also need to change line 15 if you have multiple forms on the page.
[code lang="js"]
&lt;script&gt;
// **** Littledata Javascript form tracker ****
// Generates a GTM custom event called 'formSubmitted'
// When an on-page form is submitted
// CHANGE the text to match the message displayed
// when the form is successfully completed
// It is not case sensitive
var text = "sent!"
// By default it will search for text within the first form
// Set to false if text is outside a form
// or change to a higher false if there are multiple forms
var formIndex = 0;
// OPTIONALLY, restrict the search to an HTML element ID
// If you leave this blank, the whole page will be searched;
// this causes the script to run more slowly
var targetId = ""
// **** No changes needed to the script below ****
text = text.toLowerCase()
dataLayer = dataLayer || [];
if (!formIndex &amp;&amp; targetId.length == 0) console.error('Form tracker needs either a form or an element ID')
var checkEveryMilliseconds = 500;
formTrackerInterval = window.setInterval(function(){
var target = ""
if (formIndex &gt;= 0) {
var form = document.getElementsByTagName('form')
target = (form.length &gt; 0) ? form[formIndex].textContent : "";
}
else target = document.getElementById(targetId).textContent
target = target.toLowerCase()
if (target.indexOf(text) &gt; -1) {
window.clearInterval(formTrackerInterval);
dataLayer.push({
event: 'formSubmitted'
})
}
},checkEveryMilliseconds)
&lt;/script&gt;
[/code]
Step 2
Now we need to add the script to listen out for when the form is submitted.
Create a custom HTML tag in your GTM container.
You can name the tag 'LISTENER Contact form submit event' or anything else you will remember it by.
Choose the tag type 'Custom HTML'.
Copy and paste your HTML/Javascript into the textbox, and remember to change the var text (line 10) with your own text.
Step 3
This tag needs a firing trigger, specifying the rules when it needs to be activated.
If you can, only fire on specific pages - the script will slow down the page a little, as it runs every half a second to check the form.
Give the trigger a descriptive name - here I've chosen "PAGE About us"
Select trigger type as 'Custom Event' and for the event name put " gtm.load ", which means this trigger at page load.
We want this trigger to work on a specific page only, so the firing rule goes 'page path equals /about-us', which means that our trigger will work on the www.littledata.io/about-us page only.
If you have a number of pages that have the form you're tracking, then you could use 'contains' rule and select part of the link that is applicable to all. For example, if all of your links have word 'contact' in them, then your firing rule would say 'page path contains contact'.
Step 4
Now that you have your listener tag set up, you need to create a separate tag to send the event details to Google Analytics.
Again, give it a descriptive name so you know what it's for - here I've used 'GA event - contact form submitted'.
Select tag type as 'Universal Analytics' and in the tracking ID field, select the variable that contains your GA tracking id.
For event category, action and label you have to specify the namings by which this data will be categorised in Google Analytics.
Step 5
This tag needs its own trigger to know when to fire, and here you have to use the event created by the listener tag set up during steps 2-3.
Here you have to specify that this tag can only fire when event 'formSubmitted' happens.
I've called my trigger 'Contact form event', selected trigger type as 'custom event' and entered event name 'formSubmitted.
Now you can save it and test in the debugger mode. Try submitting your contact form and see if the event 'formSubmitted' appears. You should also see the tag 'GA event - contact form submitted' fire.
If everything's ok, publish the container and do a final test. Make a new form submission and check if you can see the event details come through in Google Analytics real time reports, under events.
Need some help setting this up or Google Tag Manger? Why not get in touch by contacting our lovely Google Analytics experts?
Get Social! Follow us on LinkedIn, Twitter, and Facebook and keep up-to-date with our Google Analytics insights.
Further reading:
How to set up event tracking in Google Tag Manager
Why should you tag your campaigns?
Set up Ecommerce tracking with Google Tag Manager