April 30, 2024
Track PDF Downloads with Google Tag Manager
Updated: April 30th, 2024
Tracking file downloads on your website is essential for understanding user engagement and ensuring that the content you create resonates with visitors. By tracking downloads, you can gauge the value of your efforts and save time by focusing on content that generates the most interest.
You can follow two methods to track PDF downloads: (1) use the built-in GA4 enhanced measurement events, or (2) create a custom setup using GTM and GA4.
In this article, we will focus on the second method, how to do it, and the reasons why (for some people) tracking PDF downloads with GTM makes more sense than using the enhanced measurement event.
I presume you already know at least the basics of Google Tag Manager and have installed it on your website. If you still need to, read this tutorial.
Table of Contents
Here’s what you will learn in this article
- Enhanced measurement
- Reasons why it makes sense to track PDF downloads with GTM
- Tracking PDF downloads with GTM
- Where can you find PDF download data in GA4?
- Can I track if a visitor opens a PDF directly?
- Final Words
Video tutorial
If you prefer video content, here’s a tutorial from my Youtube channel.
Tracking file downloads with enhanced measurement
Google Analytics 4 has a feature called enhanced measurement that automatically tracks multiple actions, like file downloads. These events allow you to get more out-of-the-box tracking without configuring GTM or editing your website’s code.
The file_download event is triggered when a link is clicked, and the file extension of the link matches the following regular expression: pdf|xlsx?|docx?|txt|rtf|csv|exe|key|pp(s|t|tx)|
7z|pkg|rar|gz|zip|avi|mov|mp4|mpe?g|wmv|midi?|mp3|wav|wma
When enhanced measurement is enabled, and the file_download event is triggered, the following dimensions will automatically populate: file_extension, file_name, link_classes, link_id, link_text, and link_URL.
Google Analytics 4 automatically tracks these dimensions, which you can learn more about here, and you don’t need to add them as custom dimensions in the interface (which is not true for all automatically tracked dimensions and metrics, so check the list).
You can learn more about implementing enhanced measurement here and here (specifically for file downloads).
However, there are limitations to using enhanced measurement. You probably noticed that the event is triggered on many file extensions, which might cause issues if you just want to track PDF downloads. Let’s get into a few more reasons.
Reasons why it makes sense to track PDF downloads with GTM
Sometimes, you just need to enable tracking quickly, and enhanced measurement events are great for this purpose. However, you might eventually find limitations in the data obtained from these events. In the case of file downloads, a few things might make you consider implementing a custom setup for tracking.
Cannot customize the list of supported file extensions
Automated tracking runs on certain assumptions, so if there are ways to download files from your site that aren’t standard, as defined by Google, those file downloads will not get tracked. For example, a file on your site could exist with an extension that doesn’t match any file extensions in the list that triggers the event.
Google Analytics 4 file download tracking supports only the following file extensions: .pdf, .xls, .xlsx, .doc, .docx, .txt, .rtf, .csv, .exe, .key, .pps, .ppt, .pptx, .7z, .pkg, .rar, .gz, .zip, .avi, .mov, .mp4, .mpe, .mpeg, .wmv, .mid, .midi, .mp3, .wav, .wma.
Even though these are the most popular ones, there is a slight chance that your business uses a different extension. At the time of writing this blog post, you cannot customize the list of extensions, so there is no way to mitigate against inaccuracies in the data when there are unique file extensions on your site.
This also means you can’t just track PDF downloads; you will track all file downloads on your site in the same event.
Solution? A custom setup (which is coming up).
Limitations in custom parameters/dimensions
You can’t add any additional information, e.g., if you want to capture another parameter, like content_group, with the event.
By adding the parameter into the Event settings variable and integrating it into the Google tag, you can ensure that the enhanced measurement events collect this information. But if you want to send a parameter with just the file downloads event, this is only possible with a custom setup.
The max length of link_url is 100 characters
When GA4 tracks the file links, it captures the entire URL. So, if the URL is too long, GA4 will cut off the end of the value. Usually, the end of the URL contains the actual file name and file extension (pdf, xls, etc.), which can be problematic.
Instead, I prefer tracking the file name (e.g., some-ebook.pdf) or the Path, e.g., “/download/something/some-ebook.pdf”. This would reduce the impact of the character limit.
If these limitations impact you and you need to track file downloads (in general, not just PDFs), check out this article on how to implement file download tracking with Google Analytics 4.
Now, let’s get into the custom setup for PDF downloads!
Tracking PDF downloads with GTM
If you have reached this point, then using enhanced measurement has not met your needs for tracking PDF downloads. Luckily, you’re only five steps away from a custom setup that avoids all the abovementioned issues!
Step 1: Create a variable for the file name
Go to your Google Tag Manager container, click “Variables”, and ensure the Click URL variable is enabled. If you don’t see it in the list, click “Configure” and find the Click URL under Clicks.
Then click New (in the same Variables section) > Variable Configuration > Custom JavaScript. Paste the following code:
function() { var filepath = {{Click URL}}.split("/"); var filename = filepath.pop(); var decodedFilename = decodeURI(filename); return decodedFilename.indexOf(".") > -1?decodedFilename:'n/a'; }
Name the variable “cjs – filename” and save.
Step 2: Create a Link Click trigger
Now, go to Triggers > New > Trigger Configuration > Just links. Enter the following configuration:
Name this trigger “Link click – pdf download” and save.
Step 3: Create a GA4 event tag
Go to Tags > New > Tag Configuration > Google Analytics > Google Analytics: GA4 Event. Enter the following settings:
Ensure that the event parameter names are exactly as I show in the screenshot: file_name, link_text and link_url. You can also add these parameters to the Event settings variable and add it to the tag.
Ensure that two built-in variables in GTM are enabled: Click Text and Click URL. If you don’t see them, click Variables > Configure and enable them.
Then click the Triggering and add the Link click trigger we created in the previous chapter.
Name the tag “GA4 event – pdf_download” and save.
Step 4: Test the setup
We’re almost at the end! Now that the setup has been implemented, you must check that everything works as expected using Preview and Debugview.
Click Preview in the top-right corner of the GTM interface and enter the page URL where you want to test your new event.
Once the Preview mode has launched and your site opens, click the downloadable PDF file. Back in GTM, select the Link Click event in the left-hand tab. You should see your tag here.
Did the tag fire? If yes, go to Google Analytics > Admin > Data display > Debugview. Here, the pdf_download event will be visible (if everything was set up correctly). If you don’t see the event, check out these common mistakes.
Note: if you haven’t disabled the GA4 enhanced measurement file download tracking, you will see two events. No worries, we will fix this next!
Step 5: Publish GTM and (optionally) disable file download tracking in GA4
Final step. There are two things you need to do:
- Publish the GTM container by clicking the “Submit” button in the top-right of the GTM interface. Now, the event will be live, and start collecting data on your website.
- [Optional] You may be fine with having the file_download event run in parallel to the pdf_download event to capture the download of other file types, in which case, ignore this step. If you want to disable it, go to Google Analytics > Admin > Data Streams > Select your web data stream. Click the gear icon, disable File download tracking, and save. This way, you will avoid duplicate data for PDF downloads.
Where can you find PDF download data in GA4?
You must wait at least 24-48 hours after publishing your GTM container before seeing data in Google Analytics 4. To see the data, you can use the built-in standard Events report or create a free form exploration.
Find PDF download data in standard GA4 reports
To find the built-in standard report for Events in GA4, go to Reports > Engagement > Events.
Since GA4 is customizable, your property may not look the same as mine, so the location of this report may be elsewhere. You can also add this report collection to your reports tab if you don’t have it by going to the Library and publishing the “Life cycle” collection.
Search the name of the event pdf_download.
This may not show you the granularity of data that you are looking for, so there are three options:
- Customize the report to use the Link URL as a primary dimension. Keep in mind that the Link URL will appear for all events that use it, not just pdf_download. Hopefully, GA4 will offer an option to filter a standard report based on the event name in the future.
- Add a custom dimension (like explained here
- Build a Free Form exploration report, which we will do now!
Find PDF download data in the GA4 free form exploration
To create your own report, go to Explore > Blank exploration. Learn more about free-form reports here.
In the report, you want to add:
- Dimensions: Event name, File name or Link URL (I find File Name easier to read)
- Metrics: Active users, Event count
- Filter: Event name exactly matches pdf_download
Double-click on each dimension and metric to add them to the report (you don’t need to add the Event name parameter since it will be the same for every row).
Can I track if a visitor opens a PDF directly?
“Is there a way to check when a user accesses a PDF file in their browser directly through a link?” Unfortunately, there is no convenient way to do this since you cannot embed JavaSript in PDF files, preventing the installation of GA4 and GTM.
However, you can still work with a developer to see if they can check the server logs and tell you how many times users are opening a particular PDF. They should have this data, but GA4 cannot collect it.
Tracking PDF Downloads with Google Tag Manager: Final Words
Enhanced measurement is a quick way to set up your Google Analytics 4 tracking and capture how users interact with your site without needing extra configuration in GTM or adding to your website’s code.
However, you will quickly find that there is a better way to do things than this, especially when tracking actions that can dictate where you dedicate time and resources, like the creation of downloadable content.
The best practice would be to implement file download tracking with a custom setup, which you can do in five steps using GTM without needing a developer. Give it a go!
12 COMMENTS
This is so helpful, thank you!! I really appreciate how you walked through the very very basic beginner steps as well.
You're welcome!
Any idea how to track direct links to a PDF file? Everything above makes sense from a link click perspective, but how would you handle someone taking your PDF link and posting it online and then as a website owner, knowing how many people not only see the PDF from link clicks, but also from direct traffic?
Hi, what you have described is impossible to track with GTM, GA or any other similar tool.
I was afraid of that. Thank you for confirming Julius.
Great set of instructions really clear, unfortunately...
I just cannot get either a simple ahref link, or button link to a pdf doc download to trigger at all. Can you think why this may be the case?
Other tracking tags work ok.
It can be anything. More information is needed.
Very helpful, thank you sir!
hello,
I would like to know if the person went to the bottom of the document for example. How i do?
How do i do to track a specific page on my website or landing page ? Click, scroll etc
1. You cannot track how people use your PDF files (e.g. scrolls to the bottom).
2. You use a variable Page URL in the trigger (e.g. Page URL contains xxxxx)
Hi,
I am in the position where I need to know when the invoice is actually downloaded, can this be done? Not when clicking on the URL or cliking on the text "X", this can only be done with data layers?
How would it be?
Thanks and regards,
Ainoa
You need to talk to the developer about this. Placing Javascript (including datalayer) directly in the PDF file is impossible.