#GTMtips: Track File Downloads In GTM V2
In this #GTMtips post, we’ll go over a simple method for tracking file downloads in Google Tag Manager, specifically the new, V2 interface. Also, “tracking file downloads” means sending Events to Google Analytics, so this is a GA for GTM guide as well.
Tip 15: Set up file download tracking in GTM
Since we’re using Google Analytics as the tracking platform, we’ll need the following ingredients to make this setup work:
Event Tag - which fires when a downloadable file link is clicked, and sends the Event to Google Analytics
Auto-Event Variable for click URL path - to capture the URL path of the clicked link
Event Trigger - which uses the Link Click trigger type and checks if URL path contains any of the extensions you want to track
So start with the Auto-Event Variable, and follow these steps:
Create new Variable of type Auto-Event Variable
Name it Click URL Path
Choose Element URL as the Variable type
Choose Path as the Component Type
I like this method, since using the Component Type “Path” strips the URL so that only the URI path remains. This means that query parameters, fragments, hostnames, protocols, and ports are all excluded from the string, leaving a much cleaner output.
For the Trigger, you need to create a Link Click trigger, where the Click URL Path is matched against a regular expression of all the file extensions you want to monitor. If the whole concept of Link Click tracking is unfamiliar to you, remember to check my guide on auto-event tracking.
Create a new Trigger, and choose Click as the Event Type
Name it Event - Link Click on downloadable
Choose Link Click as the Trigger Type
Select Wait for Tags and Check Validation if you wish
If you selected either Wait for Tags or Check Validation, choose Page URL matches RegEx .* as a filter in the “Enable When” step of the Link Click Trigger creation
For “Fire On”, add the following condition: Click URL Path matches RegEx .(ext1|ext2|ext3)$
It should look like this:
Couple of things to note here. First, if you select either “Wait for Tags” or “Check Validation” in the Link Click Trigger settings, you will have an extra step called “Enable When”. This step is for when GTM should listen for link clicks, so I suggest you put Page URL matches RegEx .* as the only condition.
Second, you will need to add the file extensions you want to track in the “Fire On” condition. For example, if I want to track all PDF, XLSX, PNG, and DOCX downloads on my site, the regular expression would look like this:
This would match any Click URL Path which ends in .pdf, .xlsx, .png, or .docx.
If your site has file extensions in capital letters (PNG, XLSX), you’ll need to change the operator from matches RegEx to matches RegEx (ignore case).
Finally, you’ll need the Tag itself. It’s just your run-of-the-mill Event Tag, and you need to attach the Trigger you just created to it.
Feel free to be creative with what you add as the Event fields. I’ve used a simple Click URL Variable to get the entire URL of the clicked link, but you could do some macro magic and return just the filename or the file extension (see my macro magic article for instructions).