*/ define('GA_TRACKFILES', 'pdf|zip|mp3'); function googleanalytics_help($path, $arg) { switch ($path) { case 'admin/settings/googleanalytics': return t('Google Analytics is a free statistics package based on the excellent Urchin system.'); } } function googleanalytics_perm() { return array('administer google analytics'); } function googleanalytics_menu() { $items['admin/settings/googleanalytics'] = array( 'title' => 'Google Analytics', 'description' => 'Configure the settings used to generate your Google Analytics site map.', '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; } function googleanalytics_init() { global $user; $id = variable_get('googleanalytics_account', ''); // Check if we should track the currently active user's role. $track = _googleanalytics_track($user); // Don't track page views in the admin sections if ($id && (arg(0) != 'admin') && $track == TRUE) { // Use the old version of Google Analytics? $legacy_version = variable_get('googleanalytics_legacy_version', FALSE); $scope = variable_get('googleanalytics_js_scope', 'footer'); // Are we on a secure page? $prefix = '://www'; if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) { $prefix = 's://ssl'; } // Should a local cached copy of urchin.js or ga.js be used? $js_file = ($legacy_version) ? 'urchin.js' : 'ga.js'; $url = 'http'. $prefix .'.google-analytics.com/'. $js_file; if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _googleanalytics_cache($url)) { drupal_add_js($source, 'module', $scope); } else { $script = 'document.write(unescape("%3Cscript src=\''. $url .'\' type=\'text/javascript\'%3E%3C/script%3E"));'; drupal_add_js($script, 'inline', $scope); } // Add download tracking. $path = drupal_get_path('module', 'googleanalytics'); if (variable_get('googleanalytics_trackfiles', TRUE) && $trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES)) { drupal_add_js(array('googleanalytics' => array('trackDownload' => $trackfiles_extensions, 'LegacyVersion' => $legacy_version)), 'setting', 'header'); drupal_add_js($path .'/downloadtracker.js', 'module', $scope); } } } /** * 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', ''); // Check if we should track the currently active user's role. $track = _googleanalytics_track($user); // Don't track page views in the admin sections if ($id && (arg(0) != 'admin') && $track == TRUE) { // 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='. trim($keys)))); } // 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 = variable_get('googleanalytics_codesnippet', ''); // Should the legacy code be used? $script = ''; if ($legacy_version) { $script .= '_uacct = '. drupal_to_js($id) .';'; if (!empty($segmentation)) { $script .= $segmentation; } if (!empty($codesnippet)) { $script .= $codesnippet; } $script .= 'urchinTracker('. $url_custom .');'; } else { $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');'; $script .= 'pageTracker._initData();'; if (!empty($segmentation)) { $script .= $segmentation; } if (!empty($codesnippet)) { $script .= $codesnippet; } $script .= 'pageTracker._trackPageview('. $url_custom .');'; } drupal_add_js($script, 'inline', 'footer'); } } /** * Implementation of hook_requirements(). */ function googleanalytics_requirements($phase) { $requirements = array(); if ($phase == 'runtime') { // Raise warning if Google user account has not been set yet. if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) { $requirements['googleanalytics'] = array( 'title' => t('Google Analytics module'), 'description' => t('Google Analytics module has not been configured yet. Please configure its settings from the Google Analytics settings page.', array('@url' => url('admin/settings/googleanalytics'))), 'severity' => REQUIREMENT_ERROR, 'value' => t('Not configured'), ); } } return $requirements; } /** * Implementation of hook_cron(). */ function googleanalytics_cron() { // Regenerate the google analytics urchin.js or ga.js every day. if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) { // Legacy google analytics version. file_delete(file_directory_path() .'/googleanalytics/urchin.js'); // New google analytics version. file_delete(file_directory_path() .'/googleanalytics/ga.js'); // Clear aggregated JS files. if (variable_get('preprocess_js', 0)) { drupal_clear_js_cache(); } variable_set('googleanalytics_last_cache', time()); } } /** * Download and cache the urchin.js file locally. * @param $location * The full URL to the external javascript file. * @return mixed * The path to the local javascript file on success, boolean FALSE on failure. */ function _googleanalytics_cache($location = 'http://www.google-analytics.com/urchin.js') { $directory = file_directory_path() .'/googleanalytics'; $file_destination = $directory .'/'. basename($location); if (!file_exists($file_destination)) { $result = drupal_http_request($location); if ($result->code == 200) { // Check that the files directory is writable if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) { return file_save_data($result->data, $directory .'/'. basename($location), FILE_EXISTS_REPLACE); } } } else { return $file_destination; } } /** * * @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 GAnalytics. */ function _googleanalytics_track($account) { // By default we don't track users. $track = FALSE; foreach (array_keys($account->roles) as $role) { // Add the tracking code if user is member of one role that should be tracked. if (variable_get('googleanalytics_track_'. $role, FALSE)) { $track = TRUE; } } // Handle behavior for administrative user 1. if ($account->uid == 1 && variable_get('googleanalytics_track__user1', FALSE)) { // Enable tracking of user 1 if tracking for "authenticated user" is disabled. $track = TRUE; } elseif ($account->uid == 1 && !variable_get('googleanalytics_track__user1', FALSE)) { // User 1 is a member of "authenticated user". Disable user tracking // if user 1 shouldn't be tracked, but "authenticated user" should. $track = FALSE; } return $track; }