home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (googleanalytics.module)

This example Drupal source code file (googleanalytics.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, empty, file, function, if, page_match, path, php, return, script, string, type, user

The googleanalytics.module Drupal example source code

<?php
// $Id: googleanalytics.module,v 1.36.2.34 2011/02/07 14:16:20 hass Exp $

/*
 * @file
 * Drupal Module: GoogleAnalytics
 * Adds the required Javascript to the bottom of all your Drupal pages
 * to allow tracking by the Google Analytics statistics package.
 *
 * @author: Alexander Hass <http://drupal.org/user/85918>
 */

define('GA_TRACKFILES_EXTENSIONS', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');

/**
 * Implementation of hook_help().
 */
function googleanalytics_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/googleanalytics':
      return t('<a href="@ga_url">Google Analytics</a> is a free (registration required) website traffic and marketing effectiveness service.', array('@ga_url' => 'http://www.google.com/analytics/'));
  }
}

/**
 * Implementation of hook_theme().
 */
function googleanalytics_theme() {
  return array(
    'googleanalytics_admin_custom_var_table' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

/**
 * Implementation of hook_perm().
 */
function googleanalytics_perm() {
  return array('administer google analytics', 'opt-in or out of tracking', 'use PHP for tracking visibility');
}

/**
 * Implementation of hook_menu().
 */
function googleanalytics_menu() {
  $items['admin/settings/googleanalytics'] = array(
    'title' => 'Google Analytics',
    'description' => 'Configure the settings used to generate your Google Analytics tracking code.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('googleanalytics_admin_settings_form'),
    'access arguments' => array('administer google analytics'),
    'file' => 'googleanalytics.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implementation of hook_init().
 */
function googleanalytics_init() {
  global $user;

  $id = variable_get('googleanalytics_account', '');

  // 1. Check if the GA account number has a value.
  // 2. Track page views based on visibility value.
  // 3. Check if we should track the currently active user's role.
  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {

    // Custom tracking.
    if (variable_get('googleanalytics_trackadsense', FALSE)) {
      drupal_add_js('window.google_analytics_uacct = ' . drupal_to_js($id) . ';', 'inline', 'header');
    }

    // Add link tracking.
    $link_settings = array();
    if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
      $link_settings['trackOutgoing'] = $track_outgoing;
    }
    if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
      $link_settings['trackMailto'] = $track_mailto;
    }
    if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
      $link_settings['trackDownload'] = $track_download;
      $link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
    }
    if ($track_outbound_as_pageview = variable_get('googleanalytics_trackoutboundaspageview', 0)) {
      $link_settings['trackOutboundAsPageview'] = $track_outbound_as_pageview;
    }
    if (!empty($link_settings)) {
      drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header');
      drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', 'header');
    }
  }
}

/**
 * Implementation of hook_footer() to insert JavaScript at the end of the page.
 */
function googleanalytics_footer($main = 0) {
  global $user;

  $id = variable_get('googleanalytics_account', '');

  // 1. Check if the GA account number has a value.
  // 2. Track page views based on visibility value.
  // 3. Check if we should track the currently active user's role.
  if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {

    // Add User profile segmentation values.
    $profile_fields = variable_get('googleanalytics_segmentation', array());
    if (!empty($profile_fields)) {

      if (module_exists('profile')) {
        // Extend the $user object with profile data. Otherwise only 'User roles' can be tracked.
        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 = '_gaq.push(["_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.
    $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', '');

    // Add custom variables.
    $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
    $custom_var = '';
    for ($i = 1; $i < 6; $i++) {
      $custom_var_name = !empty($googleanalytics_custom_vars['slots'][$i]['name']) ? $googleanalytics_custom_vars['slots'][$i]['name'] : '';
      if (!empty($custom_var_name)) {
        $custom_var_value = !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '';
        $custom_var_scope = !empty($googleanalytics_custom_vars['slots'][$i]['scope']) ? $googleanalytics_custom_vars['slots'][$i]['scope'] : 3;

        if (module_exists('token')) {
          $node = menu_get_object();
          $custom_var_value = token_replace_multiple($custom_var_value, array('global' => NULL, 'user' => $user, 'node' => $node), '[', ']', array('clear' => TRUE));
        }

        // Suppress empty custom variables.
        if (!is_numeric($custom_var_value) && empty($custom_var_value)) {
          continue;
        }

        // The length of the string used for the 'name' and the length of the
        // string used for the 'value' must not exceed 64 bytes after url encoding.
        $name_length = drupal_strlen(rawurlencode($custom_var_name));
        $tmp_value = rawurlencode($custom_var_value);
        $value_length = drupal_strlen($tmp_value);
        if ($name_length + $value_length > 64) {
          // Trim value and remove fragments of url encoding.
          $tmp_value = rtrim(substr($tmp_value, 0, 63 - $name_length), '%0..9A..F');
          $custom_var_value = urldecode($tmp_value);
        }

        $custom_var_name = drupal_to_js($custom_var_name);
        $custom_var_value = drupal_to_js($custom_var_value);
        $custom_var .= "_gaq.push(['_setCustomVar', $i, $custom_var_name, $custom_var_value, $custom_var_scope]);";
      }
    }

    // Build tracker code.
    $script = 'var _gaq = _gaq || [];';
    $script .= '_gaq.push(["_setAccount", ' . drupal_to_js($id) . ']);';
    if (variable_get('googleanalytics_tracker_anonymizeip', 0)) {
      // FIXME: The Google API is currently broken and "_gat._anonymizeIp" is only
      // a workaround until "_anonymizeIp" has been implemented/fixed.
      $script .= '_gaq.push(["_gat._anonymizeIp"]);';
    }
    if (!empty($segmentation)) {
      $script .= $segmentation;
    }
    if (!empty($codesnippet_before)) {
      $script .= $codesnippet_before;
    }
    if (!empty($custom_var)) {
      $script .= $custom_var;
    }
    if (empty($url_custom)) {
      $script .= '_gaq.push(["_trackPageview"]);';
    }
    else {
      $script .= '_gaq.push(["_trackPageview", ' . $url_custom . ']);';
    }
    if (!empty($codesnippet_after)) {
      $script .= $codesnippet_after;
    }

    $script .= '(function() {';
    $script .= 'var ga = document.createElement("script");';
    $script .= 'ga.type = "text/javascript";';
    $script .= 'ga.async = true;';

    // Should a local cached copy of ga.js be used?
    if (variable_get('googleanalytics_cache', 0) && $url = _googleanalytics_cache('http://www.google-analytics.com/ga.js')) {
      // A dummy query-string is added to filenames, to gain control over
      // browser-caching. The string changes on every update or full cache
      // flush, forcing browsers to load a new copy of the files, as the
      // URL changed.
      $query_string = '?'. substr(variable_get('css_js_query_string', '0'), 0, 1);

      $script .= 'ga.src = "' . $url . $query_string . '";';
    }
    else {
      $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";';
    }
    $script .= 'var s = document.getElementsByTagName("script")[0];';
    $script .= 's.parentNode.insertBefore(ga, s);';
    $script .= '})();';

    drupal_add_js($script, 'inline', 'footer');
  }
}

/**
 * Implementation of hook_user().
 *
 * Allow users to decide if tracking code will be added to pages or not.
 */
function googleanalytics_user($type, $edit, &$account, $category = NULL) {
  switch ($type) {
    case 'form':
      if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
        $form['googleanalytics'] = array(
          '#type' => 'fieldset',
          '#title' => t('Google Analytics configuration'),
          '#weight' => 3,
          '#collapsible' => TRUE,
          '#tree' => TRUE
        );

        switch ($custom) {
          case 1:
            $description = t('Users are tracked by default, but you are able to opt out.');
            break;

          case 2:
            $description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
            break;
        }

        $form['googleanalytics']['custom'] = array(
          '#type' => 'checkbox',
          '#title' => t('Enable user tracking'),
          '#description' => $description,
          '#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : ($custom == 1)
        );

        return $form;
      }
      break;

  }
}

/**
 * Implementation of hook_cron().
 */
function googleanalytics_cron() {
  // Regenerate the tracking code file every day.
  if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400 && variable_get('googleanalytics_cache', 0)) {
    _googleanalytics_cache('http://www.google-analytics.com/ga.js', TRUE);
    variable_set('googleanalytics_last_cache', time());
  }
}

/**
 * Download/Synchronize/Cache tracking code file locally.
 *
 * @param $location
 *   The full URL to the external javascript file.
 * @param $sync_cached_file
 *   Synchronize tracking code and update if remote file have changed.
 * @return mixed
 *   The path to the local javascript file on success, boolean FALSE on failure.
 */
function _googleanalytics_cache($location, $sync_cached_file = FALSE) {

  $path = file_create_path('googleanalytics');
  $file_destination = $path . '/' . basename($location);

  if (!file_exists($file_destination) || $sync_cached_file) {
    // Download the latest tracking code.
    $result = drupal_http_request($location);

    if ($result->code == 200) {
      if (file_exists($file_destination)) {
        // Synchronize tracking code and and replace local file if outdated.
        $data_hash_local = md5(file_get_contents($file_destination));
        $data_hash_remote = md5($result->data);
        // Check that the files directory is writable.
        if ($data_hash_local != $data_hash_remote && file_check_directory($path)) {
          // Save updated tracking code file to disk.
          file_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
          watchdog('googleanalytics', 'Locally cached tracking code file has been updated.', array(), WATCHDOG_INFO);

          // Change query-strings on css/js files to enforce reload for all users.
          _drupal_flush_css_js();
        }
      }
      else {
        // Check that the files directory is writable.
        if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
          // There is no need to flush JS here as core refreshes JS caches
          // automatically, if new files are added.
          file_save_data($result->data, $file_destination, FILE_EXISTS_REPLACE);
          watchdog('googleanalytics', 'Locally cached tracking code file has been saved.', array(), WATCHDOG_INFO);

          // Return the local JS file path.
          return base_path() . $file_destination;
        }
      }
    }
  }
  else {
    // Return the local JS file path.
    return base_path() . $file_destination;
  }
}

/**
 * Delete cached files and directory.
 */
function googleanalytics_clear_js_cache() {
  $path = file_create_path('googleanalytics');
  if (file_check_directory($path)) {
    file_scan_directory($path, '.*', array('.', '..', 'CVS'), 'file_delete', TRUE);
    rmdir($path);

    // Change query-strings on css/js files to enforce reload for all users.
    _drupal_flush_css_js();

    watchdog('googleanalytics', 'Local cache has been purged.', array(), WATCHDOG_INFO);
  }
}

/**
 * Tracking visibility check for an user object.
 *
 * @param $account
 *   A user object containing an array of roles to check.
 * @return boolean
 *   A decision on if the current user is being tracked by Google Analytics.
 */
function _googleanalytics_visibility_user($account) {

  $enabled = FALSE;

  // Is current user a member of a role that should be tracked?
  if (_googleanalytics_visibility_roles($account)) {

    // Use the user's block visibility setting, if necessary.
    if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
      if ($account->uid && isset($account->googleanalytics['custom'])) {
        $enabled = $account->googleanalytics['custom'];
      }
      else {
        $enabled = ($custom == 1);
      }
    }
    else {
      $enabled = TRUE;
    }

  }

  return $enabled;
}

/**
 * Based on visibility setting this function returns TRUE if GA code should
 * be added for the current role and otherwise FALSE.
 */
function _googleanalytics_visibility_roles($account) {

  $visibility = variable_get('googleanalytics_visibility_roles', 0);
  $enabled = $visibility;
  $roles = variable_get('googleanalytics_roles', array());

  if (array_sum($roles) > 0) {
    // One or more roles are selected.
    foreach (array_keys($account->roles) as $rid) {
      // Is the current user a member of one of these roles?
      if (isset($roles[$rid]) && $rid == $roles[$rid]) {
        // Current user is a member of a role that should be tracked/excluded from tracking.
        $enabled = !$visibility;
        break;
      }
    }
  }
  else {
    // No role is selected for tracking, therefore all roles should be tracked.
    $enabled = TRUE;
  }

  return $enabled;
}

/**
 * Based on visibility setting this function returns TRUE if GA code should
 * be added to the current page and otherwise FALSE.
 */
function _googleanalytics_visibility_pages() {
  static $page_match;

  // Cache visibility setting in hook_init for hook_footer.
  if (!isset($page_match)) {

    $visibility = variable_get('googleanalytics_visibility', 0);
    $setting_pages = variable_get('googleanalytics_pages', '');

    // Match path if necessary.
    if (!empty($setting_pages)) {
      // Convert path to lowercase. This allows comparison of the same path
      // with different case. Ex: /Page, /page, /PAGE.
      $pages = drupal_strtolower($setting_pages);
      if ($visibility < 2) {
        // Convert the Drupal path to lowercase
        $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
        // Compare the lowercase internal and lowercase path alias (if any).
        $page_match = drupal_match_path($path, $pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
        }
        // When $visibility has a value of 0, the tracking code is displayed on
        // all pages except those listed in $pages. When set to 1, it
        // is displayed only on those pages listed in $pages.
        $page_match = !($visibility xor $page_match);
      }
      else {
        $page_match = drupal_eval($setting_pages);
      }
    }
    else {
      $page_match = TRUE;
    }

  }
  return $page_match;
}

/**
 * Implementation of hook_token_values().
 */
function googleanalytics_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'user') {
    if (!empty($object->roles)) {
      $account = $object;
    }
    else {
      $account = user_load(array('uid' => $GLOBALS['user']->uid));
    }

    $tokens['user-role-names'] = implode(',', $account->roles);
    $tokens['user-role-ids'] = implode(',', array_keys($account->roles));

    return $tokens;
  }
}

/**
 * Implementation of hook_token_list().
 */
function googleanalytics_token_list($type = 'all') {
  if ($type == 'user' || $type == 'all') {
    $tokens['user']['user-role-names'] = t('The role names the user account is a member of as comma separated list.');
    $tokens['user']['user-role-ids'] = t('The role ids the user account is a member of as comma separated list.');
    return $tokens;
  }
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal googleanalytics.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.