Index: piwik.module
===================================================================
--- piwik.module	(revision 777)
+++ piwik.module	(revision 1291)
@@ -73,95 +73,106 @@
   $id = variable_get('piwik_site_id', '');
 
   if (!empty($id) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
+    // Build settings code. See http://piwik.org/javascript-tag-documentation/
+    $settings = array(
+      'piwik_action_name' => drupal_to_js(str_replace('/', '_', strip_tags(drupal_get_title()))),
+      'piwik_site_id' => drupal_to_js(variable_get('piwik_site_id', '')),
+      'piwik_url' => 'pkBaseURL + "/piwik.php"',
+      '_piwik_vars' => array(),
+      '_piwik_codesnippet' => variable_get('piwik_codesnippet', ''),
+    );
 
-    $piwik_vars = array();
-
+    // Add link tracking.
+    if (!($track_outgoing = variable_get('piwik_track', 1))) {
+      $settings['piwik_install_tracker'] = '0';
+    }
     /* TODO: For future use.
-    // Add User profile segmentation values.
-    if (is_array($profile_fields = variable_get('piwik_segmentation', '')) && ($user->uid > 0)) {
+     if ($track_outgoing = variable_get('piwik_trackoutgoing', 1)) {
+     $script .= '';
+     }
+     if ($track_mailto = variable_get('piwik_trackmailto', 1)) {
+     $script .= '';
+     }
+     if (($track_download = variable_get('piwik_trackfiles', 1)) && ($trackfiles_extensions = variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS))) {
+     */
+    if (($track_download = variable_get('piwik_track', 1)) && ($trackfiles_extensions = variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS))) {
+      $settings['piwik_download_extensions'] = drupal_to_js($trackfiles_extensions);
+    }
 
-      $p = module_invoke('profile', 'load_profile', $user);
+    /*
+     * Invoke the hook
+     */
+    foreach (module_implements('piwik_footer') as $module) {
+      $function = $module .'_piwik_footer';
+      $function($settings);
+    }
 
-      $fields = array();
-      foreach ($profile_fields as $field => $title) {
-        $value = $user->$field;
+    // Convert custom variable array to js string
+    if (!empty($settings['_piwik_vars'])) {
+      $settings['piwik_vars'] = drupal_to_js($settings['_piwik_vars']);
+      unset($settings['_piwik_vars']);
+    }
+    else {
+      $settings['piwik_vars'] = '[]';
+    }
 
-        if (is_array($value)) {
-          $value = implode(',', $value);
-        }
-
-        $fields[$field] = $value;
-      }
-
-      // Only show segmentation variable if there are specified fields.
-      if (count($fields) > 0) {
-        $piwik_vars['user_segmentation'] = implode(':', $fields);
+    $script = '';
+    foreach ($settings as $name => $value) {
+      if ($name{0} != '_') {
+        $script .= $name . ' = ' . $value . ';';
       }
     }
+    $script .= $settings['_piwik_codesnippet'];
+    $script .= 'piwik_log(piwik_action_name, piwik_site_id, piwik_url, piwik_vars);';
 
-    // Site search tracking support.
-    if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search') {
-      $keys = search_get_keys();
-      $piwik_vars['search_type'] = arg(1);
-      $piwik_vars['search_keys'] = trim($keys);
-    }
+    drupal_add_js($script, 'inline', 'footer');
+  }
+}
 
-    // Track access denied (403) and file not found (404) pages.
-    if (function_exists('drupal_get_headers')) {
-      $headers = drupal_get_headers();
-      if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
-        $piwik_vars['status_code'] = '403';
-      }
-      elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
-        $piwik_vars['status_code'] = '404';
-      }
-    }
-    */
+/**
+ * Implementation of hook_piwik_footer().
+ */
+function piwik_piwik_footer(&$settings) {
+  /* TODO: For future use.
+  // Add User profile segmentation values.
+  if (is_array($profile_fields = variable_get('piwik_segmentation', '')) && ($user->uid > 0)) {
+    $p = module_invoke('profile', 'load_profile', $user);
 
-    // Add any custom code snippets if specified.
-    $codesnippet = variable_get('piwik_codesnippet', '');
+    $fields = array();
+    foreach ($profile_fields as $field => $title) {
+      $value = $user->$field;
 
-    // Build settings code. See http://piwik.org/javascript-tag-documentation/
-    $script = '';
-    $script .= 'piwik_action_name = document.title;';
-    $script .= 'piwik_site_id = '. drupal_to_js(variable_get('piwik_site_id', '')) .';';
-    $script .= 'piwik_url = pkBaseURL + "/piwik.php";';
+      if (is_array($value)) {
+        $value = implode(',', $value);
+      }
 
-    // Add link tracking.
-    if (!($track_outgoing = variable_get('piwik_track', 1))) {
-      $script .= 'piwik_install_tracker = 0;';
-    }
-    /* TODO: For future use.
-    if ($track_outgoing = variable_get('piwik_trackoutgoing', 1)) {
-      $script .= '';
+      $fields[$field] = $value;
     }
-    if ($track_mailto = variable_get('piwik_trackmailto', 1)) {
-      $script .= '';
-    }
-    if (($track_download = variable_get('piwik_trackfiles', 1)) && ($trackfiles_extensions = variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS))) {
-    */
-    if (($track_download = variable_get('piwik_track', 1)) && ($trackfiles_extensions = variable_get('piwik_trackfiles_extensions', PK_TRACKFILES_EXTENSIONS))) {
-      $script .= 'piwik_download_extensions = '. drupal_to_js($trackfiles_extensions) .';';
+
+    // Only show segmentation variable if there are specified fields.
+    if (count($fields) > 0) {
+      $settings['_piwik_vars']['user_segmentation'] = implode(':', $fields);
     }
+  }
 
-    // Add custom variables.
-    if (!empty($piwik_vars)) {
-      $piwik_vars_fields = array();
-      foreach ($piwik_vars as $name => $value) {
-        $piwik_vars_fields[] = drupal_to_js($name) .':'. drupal_to_js($value);
-      }
-      $script .= 'piwik_vars = { '. implode(', ', $piwik_vars_fields) .' };';
-    }
-    else {
-      $script .= 'piwik_vars = "";';
+  // Site search tracking support.
+  if (module_exists('search') && variable_get('piwik_site_search', FALSE) && arg(0) == 'search') {
+    $keys = search_get_keys();
+    $settings['_piwik_vars']['search_type'] = arg(1);
+    $settings['_piwik_vars']['search_keys'] = trim($keys);
+  }
+
+  // Track access denied (403) and file not found (404) pages.
+  if (function_exists('drupal_get_headers')) {
+    $headers = drupal_get_headers();
+    if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
+      $settings['_piwik_vars']['status_code'] = '403';
     }
-    if (!empty($codesnippet)) {
-      $script .= $codesnippet;
+    elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
+      $settings['_piwik_vars']['status_code'] = '404';
     }
-    $script .= 'piwik_log(piwik_action_name, piwik_site_id, piwik_url, piwik_vars);';
-
-    drupal_add_js($script, 'inline', 'footer');
   }
+  */
 }
 
 /**
@@ -290,7 +300,7 @@
 }
 
 /**
- * Based on visibility setting this function returns TRUE if GA code should
+ * Based on visibility setting this function returns TRUE if piwik code should
  * be added for the current role and otherwise FALSE.
  */
 function _piwik_visibility_roles($account) {
@@ -318,7 +328,7 @@
 }
 
 /**
- * Based on visibility setting this function returns TRUE if GA code should
+ * Based on visibility setting this function returns TRUE if piwik code should
  * be added to the current page and otherwise FALSE.
  */
 function _piwik_visibility_pages() {
