Have Google Analytics code still run on force download When you click on a download link (my_download/download/agent/%/week/%) that I have set up dynamically for each user it runs some logic and downloads a file.

I was thinking that I could just check Google Analytics to see how many people downloaded the file but the data is not there.  Does anyone have any idea to have it force download like I have set up and run Google Analtyics?

/**
 * Implements hook_menu().
 */
function my_module_menu() {
  $items['my_download/download/agent/%/week/%'] = array(
    'title' =&gt; 'Download',
    'description' =&gt; 'Download File.',
    'page arguments' =&gt; array(3, 5),
    'page callback' =&gt; 'my_module_file_download',
  );

  return $items;
}

/**
 * Download file.
 *
 * @param string $agent_id
 *   Agent number.
 * @param string $date
 *   Date string.
 *
 * @return string
 *   Force file to download or give error.
 */
function ea_email_requote_agent_file_download($agent_id, $date) {
  $file_name = my_module_agent_file_name($agent_id, $date);
  $file = "/mypath/$date/$file_name";

  if (file_exists($file)) {
    // Transfer to browser download.
    drupal_add_http_header('Pragma', 'public');
    drupal_add_http_header('Expires', '0');
    drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
    drupal_add_http_header('Cache-Control', 'private');
    drupal_add_http_header('Content-Type', 'text/csv');
    drupal_add_http_header('Content-Disposition', 'attachment; filename="' . $file_name . '";');

    readfile($file);
    drupal_exit();
  }
  else {
    drupal_set_message('File has not been loaded yet.', 'error');
    return '';
  }
}
For that kind of thing, you can use the GA "event tracking" to track the click event for the file download. 


  Events are user interactions with content that can be tracked independently from a web page or a screen load. Downloads, mobile ad clicks, gadgets, Flash elements, AJAX embedded elements, and video plays are all examples of actions you might want to track as Events.


The GA module provides some click event tracking for files by default in the config, else you can check here for more info on adding such a feature:

https://developers.google.com/analytics/devguides/collection/analyticsjs/events