analytics.js apache google-analytics javascript php

Send file download event from server to Google Analytics

Very related and helpful: Send event to Google Analytics using API server sided

A download link is sent to a customer via email:


Please find your product here:

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 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?