--- piwik_orig.module	2008-11-21 15:46:20.875000000 -0500
+++ piwik.module	2008-11-21 14:49:07.046875000 -0500
@@ -73,8 +73,14 @@ function piwik_footer($main = 0) {
   $id = variable_get('piwik_site_id', '');
 
   if (!empty($id) && _piwik_visibility_pages() && _piwik_visibility_user($user)) {
-
-    $piwik_vars = array();
+    // 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', ''),
+    );
 
     /* TODO: For future use.
     // Add User profile segmentation values.
@@ -99,13 +105,6 @@ function piwik_footer($main = 0) {
       }
     }
 
-    // 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);
-    }
-
     // Track access denied (403) and file not found (404) pages.
     if (function_exists('drupal_get_headers')) {
       $headers = drupal_get_headers();
@@ -118,18 +117,35 @@ function piwik_footer($main = 0) {
     }
     */
 
-    // Add any custom code snippets if specified.
-    $codesnippet = variable_get('piwik_codesnippet', '');
+    /*
+     * TODO: Extract this into a hook
+     */
 
-    // 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";';
+    // Site search tracking support.
+    if (module_exists('search') && variable_get('piwik_site_search', FALSE)) {
+      if (arg(0) == 'search') {
+        $keys = search_get_keys();
+        $search_settings['search_type'] = arg(1);
+        $search_settings['search_keys'] = trim($keys);
+        $data = 'search_type: "'.$search_settings['search_type'].'", search_keys: "'.$search_settings['search_keys'].'"';
+//        $ajaxPage = base_path() . drupal_get_path('module', 'piwik') . '/piwik_search.php';
+        $ajaxPage = base_path() . 'piwik_search.php';
+
+        $settings['piwik_action_name'] .= ' + ' . drupal_to_js('/' . $search_settings['search_type']);
+        $settings['_piwik_codesnippet'] .= '$(\'dt.title a\').click(function () { var href = $(this).attr("href"); $(this).removeAttr("href"); $.post("'.$ajaxPage.'", { '.$data.', url: href }, function(data) { window.location.href = data; } );  }, \'text\');';
+      }
+      else if (isset($_SESSION['search_settings'])) {
+        $settings['_piwik_vars']['search_type'] = $_SESSION['search_settings']['search_type'];
+        $settings['_piwik_vars']['search_keys'] = $_SESSION['search_settings']['search_keys'];
+      }
+
+      //make sure this gets unset, no matter what
+      unset($_SESSION['search_settings']);
+    }
 
     // Add link tracking.
     if (!($track_outgoing = variable_get('piwik_track', 1))) {
-      $script .= 'piwik_install_tracker = 0;';
+      $settings['piwik_install_tracker'] = '0';
     }
     /* TODO: For future use.
     if ($track_outgoing = variable_get('piwik_trackoutgoing', 1)) {
@@ -141,23 +157,24 @@ function piwik_footer($main = 0) {
     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) .';';
+      $settings['piwik_download_extensions'] = drupal_to_js($trackfiles_extensions);
     }
 
-    // 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) .' };';
+    // Convert custom variable array to js string
+    if (!empty($settings['_piwik_vars'])) {
+      $settings['piwik_vars'] = drupal_to_js($settings['_piwik_vars']);
     }
     else {
-      $script .= 'piwik_vars = "";';
+      $settings['piwik_vars'] = '[]';
     }
-    if (!empty($codesnippet)) {
-      $script .= $codesnippet;
+
+    $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);';
 
     drupal_add_js($script, 'inline', 'footer');
@@ -189,7 +206,6 @@ function piwik_user($type, $edit, &$acco
         return $form;
       }
       break;
-
   }
 }
 
@@ -290,7 +306,7 @@ function _piwik_visibility_user($account
 }
 
 /**
- * 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 +334,7 @@ function _piwik_visibility_roles($accoun
 }
 
 /**
- * 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() {
