You are here

function sbp_ga_preprocess_page in Search by Page 6

Implementation of hook_preprocess_page().

If we are on a search page, translate the URL for Google Analytics. We have to override the current call to the _gaq_push() function that the GA module already put into the footer, with a new call having a different URL.

File

./sbp_ga.module, line 33
Module file for Search by Page Google Analtics Integration.

Code

function sbp_ga_preprocess_page(&$variables) {
  $url_custom = '';
  if (variable_get('googleanalytics_site_search', FALSE) && ($keys = sbp_ga_get_custom_keys())) {
    $envs = search_by_page_list_environments();
    foreach ($envs as $envid) {
      $path = search_by_page_setting_get('page_path', $envid, 'search_pages');
      if (arg(0) == $path) {
        $url_custom = drupal_to_js(url('search/' . arg(0), array(
          'query' => 'search=' . $keys,
        )));
      }
      break;
    }
  }
  if ($url_custom) {
    $newscript = '_gaq.push(["_trackPageview", ' . $url_custom . ']);';
    $oldscript = '_gaq.push(["_trackPageview"]);';
    $variables['closure'] = str_replace($oldscript, $newscript, $variables['closure']);
  }
}