How do I track a publicly downloaded file? I am creating a custom module to track the file download time and user id with a file link or path. I have tried multiple modules but the download the file time tracking option is not available.
How I can programmatically track the file download time?
This is the code I am using.
function mymodule_file_download($uri) {
$scheme = \Drupal::service('file_system')-&gt;uriScheme($uri);
  if (in_array($scheme, ['private', 'temporary', 'session'])) {
    $permission = &quot;read $scheme files&quot;;
    $current_user = \Drupal::currentUser();
    $account = $current_user-&gt;getAccount();
    if ($account-&gt;hasPermission($permission)) {
      return [
        'Content-Type: text/plain',
      ];
    }
  

    if (\Drupal::currentUser()-&gt;isAnonymous()) {
      throw new NotFoundHttpException();
   }

  }
  elseif (in_array($scheme, ['public'])){
    $current_user = \Drupal::currentUser();
    $account = $current_user-&gt;getAccount();
   \Drupal::logger('downloads_tracker1')-&gt;notice($account); 
  }

}
hook_file_download doesn't run for public file downloads.

Control access to private file downloads and specify HTTP headers.

If you want to track public file downloads, you will need a different approach.
For example, track downloads with Google Analytics event tracking