Category Archives: analytics.js

Send file download event from server to Google Analytics [duplicate]

This question already has an answer here:

A download link is sent to a customer via email:

Hello,
Please find your product here: https://www.example.com/files/yourfile.zip

I'd like to track this download in Google Analytics, as a Goal conversion.

Unfortunately, when the user clicks on the link, the file is directly delivered by the web server, without going trough a .html page.

How to track such a direct file download inside Analytics?

  1. Should I add a dummy HTML page "in the middle", that would use the analytics.js tracking snippet and send a download event to GA with ga.send(...), and then redirect to the actual file after 500 milliseconds with setTimeout(redirect, 500)? Is it really a clean and safe solution? I see many little potential issues: is 500 ms ok? what kind of redirection should be used? Also a user with JS disabled will never get his file... or if using <noscript> no Goal conversion can be recorded.

  2. Is there a way to ask Apache (who serves the yourfile.zip to the client) or PHP to send a tracking event to GoogleAnalytics when this file is served?

  3. Another solution?

It seems that solution 2. would have the advantage of being 100% reliable, no matter the client has JS enabled or not.

But on the other hand, I don't want to use a very-few used hack. What is the usual solution for this very common situation?