0

I'm probably not fully awake this morning because it seems so trivial but I cannot figure it out.

I need a hook to track when user access a certain page (path = 'export-csv'). This page is not a node neither a View and neither a form... It is just a "path". And I need a hook that I could use in my custom module's file, not in templates.

I thought of something like hook_preprocess_page or but it doesn't seem to work.

I need a hook called before the page is fully loaded because the page is never truly displayed (it is a page for downloading a file and it just shows the download popup).

If you need precisions about my code or something, just ask. But I don't think that it is necessary.

Thanks in advance of course.

edit: I found a part of solution here but I still have trouble because of the nature of the page. I never truly land on the page and I think the hook is not called before the popup happens.

The callback function of this page looks like this:

function grap_contact_export_csv_page() {
  header("Cache-Control: public");
  header("Content-Type: application/octet-stream");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0");
  header("Content-Disposition: attachment; filename=\"grape-export.csv\";" );
  header("Content-Transfer-Encoding: binary");
  $fp = fopen('export.csv', 'r') or die('Impossible d\'accéder au fichier');
  echo fread($fp, filesize('export.csv'));
  fclose($fp);
  drupal_exit();
}
  • Try with hook_menu_alter. Install your module's callback function, do whatever you need in your callback, and when done do a module_load_include for the above callback and call it directly. – J. Reynolds Oct 8 '14 at 10:23
0

That's how I achieved that:

function grap_contact_export_csv_page() {
  grap_log_export(); // Calling another function to record tracking in DB
  header("Cache-Control: public");
  header("Content-Type: application/octet-stream");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0");
  header("Content-Disposition: attachment; filename=\"grape-export.csv\";" );
  header("Content-Transfer-Encoding: binary");
  $fp = fopen('export.csv', 'r') or die('Impossible d\'accéder au fichier');
  echo fread($fp, filesize('export.csv'));
  fclose($fp);
  drupal_exit();
}
| improve this answer | |
  • Why not to use Google Analytics and track it as an event, for example? Not reliable enough? – Mołot Nov 4 '14 at 8:45
  • Actually, I had to implement a custom tracking system for my company's internal application. Because it is linked to "government's data", I cannot use a third service like Google. :-) – Djouuuuh Nov 4 '14 at 8:50
  • Oh, makes sense. – Mołot Nov 4 '14 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.