You are here

function googleanalytics_footer in Google Analytics 6

Same name and namespace in other branches
  1. 5 googleanalytics.module \googleanalytics_footer()
  2. 6.2 googleanalytics.module \googleanalytics_footer()
  3. 6.3 googleanalytics.module \googleanalytics_footer()

Implementation of hook_footer() to insert Javascript at the end of the page.

File

./googleanalytics.module, line 94

Code

function googleanalytics_footer($main = 0) {
  global $user;
  $id = variable_get('googleanalytics_account', '');
  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {

    // Use the old version of Google Analytics?
    $legacy_version = variable_get('googleanalytics_legacy_version', FALSE);

    // Add User profile segmentation values.
    if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && $user->uid > 0) {
      $p = module_invoke('profile', 'load_profile', $user);
      $fields = array();
      foreach ($profile_fields as $field => $title) {
        $value = $user->{$field};
        if (is_array($value)) {
          $value = implode(',', $value);
        }
        $fields[$field] = $value;
      }

      // Only show segmentation variable if there are specified fields.
      $segmentation = '';
      if (count($fields) > 0) {
        if ($legacy_version) {
          $segmentation = '__utmSetVar(' . drupal_to_js(implode(':', $fields)) . ');';
        }
        else {
          $segmentation = 'pageTracker._setVar(' . drupal_to_js(implode(':', $fields)) . ');';
        }
      }
    }

    // Site search tracking support.
    $url_custom = '';
    if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && ($keys = search_get_keys())) {
      $url_custom = drupal_to_js(url('search/' . arg(1), array(
        'query' => 'search=' . $keys,
      )));
    }

    // If this node is a translation of another node, pass the original
    // node instead.
    if (module_exists('translation') && variable_get('googleanalytics_translation_set', 0)) {

      // Check we have a node object, it supports translation, and its
      // translated node ID (tnid) doesn't match its own node ID.
      $node = menu_get_object();
      if ($node && translation_supported_type($node->type) && isset($node->tnid) && $node->tnid != $node->nid) {
        $source_node = node_load($node->tnid);
        $languages = language_list();
        $url_custom = drupal_to_js(url('node/' . $source_node->nid, array(
          'language' => $languages[$source_node->language],
        )));
      }
    }

    // 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')) {
        if ($legacy_version) {

          // See http://www.google.com/support/analytics/bin/answer.py?answer=86928
          $url_custom = '"/403.html?page=" + _udl.pathname + _udl.search';
        }
        else {

          // See http://www.google.com/support/analytics/bin/answer.py?answer=86927
          $url_custom = '"/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
        }
      }
      elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
        if ($legacy_version) {
          $url_custom = '"/404.html?page=" + _udl.pathname + _udl.search';
        }
        else {
          $url_custom = '"/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
        }
      }
    }

    // Add any custom code snippets if specified.
    $codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
    $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');

    // Should the legacy code be used?
    $script = 'try{';
    if ($legacy_version) {
      $script .= '_uacct = ' . drupal_to_js($id) . ';';
      if (!empty($segmentation)) {
        $script .= $segmentation;
      }
      if (!empty($codesnippet_before)) {
        $script .= $codesnippet_before;
      }
      $script .= 'urchinTracker(' . $url_custom . ');';
      if (!empty($codesnippet_after)) {
        $script .= $codesnippet_after;
      }
    }
    else {
      $script .= 'var pageTracker = _gat._getTracker(' . drupal_to_js($id) . ');';
      if (!empty($segmentation)) {
        $script .= $segmentation;
      }
      if (!empty($codesnippet_before)) {
        $script .= $codesnippet_before;
      }
      $script .= 'pageTracker._trackPageview(' . $url_custom . ');';
      if (!empty($codesnippet_after)) {
        $script .= $codesnippet_after;
      }
    }
    $script .= '} catch(err) {}';
    drupal_add_js($script, 'inline', 'footer');
  }
}