I'm trying to create a module that will fire send an event via google analytics' Measurement Protocol API when a file is downloaded. These files are all in the private file system outside of the document root.
hook_file_download
looks like a somewhat likely candidate for triggering an http request to google analytics, but since the hook is responsible for access, it seems possible that another module might deny access after this version of the hook is run, and google analytics would get an event even in cases where the download didn't go through. If there a better hook that would be run after all the access check have finished? I should probably mention that I am using the media module.
Supplementarily, the existence of both hook_file_download
and hook_file_download_access
is confusing since they both claim to control access to files. Is it a correct understanding that hook_file_download_access
basically controls whether a user has access in relation to the entity the file is attached to (so likely would control whether the download link is displayed at all or not), and hook_file_download
only applies to private files, and checks on access after a request for the file has been made?
Follow Up
I ended up implementing two hooks: hook_file_download
and hood_file_transfer
which as far as I could tell is undocumented in the drupal api site as a hook. Both of those hooks calls a function passing the $uri parameter which in turn builds the google analytics request and then sends an http request. That seems to cover the two different cases when a file download passes through drupal for my setup.