You are here

function googleanalytics_footer in Google Analytics 6.2

Same name and namespace in other branches
  1. 5 googleanalytics.module \googleanalytics_footer()
  2. 6 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 90

Code

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

    // Add User profile segmentation values.
    $profile_fields = variable_get('googleanalytics_segmentation', array());
    if (module_exists('profile') && !empty($profile_fields) && $user->uid > 0) {

      // Extend the $user object with profile data.
      profile_load_profile($user);
      $fields = array();
      foreach ($profile_fields as $field => $title) {
        $fields[$field] = is_array($user->{$field}) ? implode(',', $user->{$field}) : $user->{$field};
      }

      // Only show segmentation variable if there are specified fields.
      $segmentation = '';
      if (count($fields) > 0) {
        $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, '403 Forbidden')) {

        // 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, '404 Not Found')) {
        $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', '');

    // Build tracker code for footer.
    $script = 'try{';
    $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');
  }
}