You are here

function google_analytics_et_page_alter in Google Analytics Event Tracking 7

Implements hook_page_alter().

File

./google_analytics_et.module, line 14
Google Analytics Event Tracking Module.

Code

function google_analytics_et_page_alter(&$page) {
  global $user;
  $id = variable_get('googleanalytics_account', '');
  $cache_on = variable_get('google_analytics_et_cache_on', FALSE);

  // Get page status code for visibility filtering.
  $status = drupal_get_http_header('Status');
  $trackable_status_codes = array(
    '403 Forbidden',
    '404 Not Found',
  );
  $settings = module_invoke_all('google_analytics_et_settings_info');

  // Get all selectors defined by google_analytics_et_api hook.
  $selectors =& drupal_static(__FUNCTION__);
  if (!isset($selectors)) {
    if ($cache = cache_get('google_analytics_et_selectors_cache')) {
      $selectors = $cache->data;
    }
    else {
      $selectors = module_invoke_all('google_analytics_et_api');

      // If the override selectors toggle is TRUE then only load the selectors
      // from the variable table.
      $override_hooked_selectors = variable_get('google_analytics_et_selectors_override', FALSE);
      if ($override_hooked_selectors) {
        $selectors = variable_get('google_analytics_et_selectors', '');
      }

      // Sanitize event tracking input to limit the threat of xss.
      $selectors = _google_analytics_et_sanitize_event_trackers($selectors);
      if ($cache_on) {
        cache_set('google_analytics_et_selectors_cache', $selectors, 'cache');
      }
    }
  }

  // Token replacements. @TODO this should be in an alter hook
  if (!empty($selectors)) {
    foreach ($selectors as $out_key => $selector) {
      if (!empty($selector) && $out_key != 'debug') {
        foreach ($selector as $key => $value) {
          switch ($key) {
            case 'label':
            case 'category':
            case 'action':
              $selectors[$out_key][$key] = token_replace($value);
              break;
          }
        }
      }
    }
  }
  $js = array(
    'googleAnalyticsETSettings' => array(
      'selectors' => $selectors,
      'settings' => $settings,
    ),
  );
  if (!empty($id) && (_googleanalytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _googleanalytics_visibility_user($user)) {

    // Get the page scope setting from googleanalytics:
    $scope = variable_get('googleanalytics_js_scope', 'header');
    drupal_add_js($js, 'setting');
    drupal_add_js(drupal_get_path('module', 'google_analytics_et') . '/js/google_analytics_et.js', array(
      'scope' => $scope,
    ));
  }
}