Tracking file downloads from a website
By Flavio
Published · Updated
Tagged: Tutorials
Learn how to track file downloads on a static website with Plausible: turn on file downloads tracking, see the clicks per file, and track other file types like epub.
I had the need to track file downloads on a website.
I initially looked into using URL shorteners that provided stats, as I assumed that was my only option without a backend (was building a static site) but nothing really excited me.
Then I saw Plausible can track file downloads.
Turn it on
The setup is simpler now. File downloads tracking is on by default for new sites. To check it, open the site’s Plausible settings, go to General, and look for the File downloads toggle under Default tracking in the Tracking area. You do not need to swap the tracking script by hand. The change applies to the snippet already on your pages.
A click on a file link is recorded as a File Download event. Each download click also counts towards your billable monthly pageviews, so keep that in mind on a site with lots of downloads.
Which files are tracked
Plausible looks at the extension at the end of the link’s path, like /files/report.pdf. These extensions are tracked by default:
pdf, xlsx, docx, txt, rtf, csv, exe, key, pps, ppt, pptx, 7z, pkg, rar, gz, zip, avi, mov, mp4, mpeg, wmv, midi, mp3, wav, wma, dmg
Some common ones are missing from that list. epub, json and svg aren’t in it, for example, so links to those files aren’t counted until you add them (see below).
Where to see the downloads
Open your Plausible dashboard and scroll to the Goals section. The File Download goal appears there as soon as the first download is tracked, so don’t worry if it’s missing right after you turn the feature on.
Click it to filter the whole dashboard to download clicks. You get:
- Events: total clicks, including the same person clicking twice
- Visitors: unique visitors who clicked
- the pages that drove the clicks, referral sources, countries and devices
The file URL is stored in a property called url. Click a URL in the list to see the stats of that one file. To group files, for example all the files in your /ebooks/ folder, click Filter in the top right, choose Property, select url, and use the “contains” option.
Track other file types
To track your own list of extensions, pass fileExtensions to plausible.init in the Plausible snippet on your pages:
plausible.init({
fileDownloads: {
fileExtensions: ['pdf', 'epub']
}
})
Setting fileExtensions replaces the default list, so include every extension you want to track. With the code above, a .zip link is no longer counted.
If you install Plausible through its npm package, it doesn’t read the site settings, so you enable the feature in code. init also needs your domain, and fileDownloads: true turns the feature on with the default list:
import { init } from '@plausible-analytics/tracker'
init({
domain: 'example.com',
fileDownloads: true
})
Pass the same { fileExtensions: [...] } object instead of true to use your own list.
The cases it misses
There are a few limitations worth knowing:
- The visitor must click the file link from a tracked page. A direct file URL opened from an email is not counted.
- A file on another domain, a CDN or a subdomain you don’t track is recorded as an outbound link click instead, if you have outbound link tracking turned on.
- A link with no file extension, like
/download/1234, isn’t detected as a download. - Links inside an
<svg>element aren’t tracked.
For the links Plausible can’t detect, add a CSS class to the link and track it as a custom event:
<a href="/download/1234" class="plausible-event-name=Ebook+Download">
Download the ebook
</a>
The + becomes a space, so the event name is Ebook Download. Custom events don’t show up on their own like the file downloads goal. Go to your site settings, open Goals, click + Add goal, choose Custom event and type Ebook Download exactly. For links, Plausible attaches the href as the url property automatically, so you can still tell files apart.
If the class doesn’t fit, you can send the event from JavaScript:
plausible('Ebook Download', { props: { url: '/download/1234' } })
Check that it works
Open your browser’s developer tools, go to the Network tab, and click a file link. You should see a request to plausible.io/api/event. If you see no request, the feature is off or the script didn’t load on that page.
If the request is there but the response has an x-plausible-dropped: 1 header, Plausible received the event and rejected it. That happens when you test from localhost or a staging domain that isn’t added to your Plausible account, so test on the live site.
If you’d rather run the analytics on your own server, see how to self-host Plausible.
Want me to talk about your product? You can sponsor this site.
Related posts about tutorial: