| 1 |
<?php
|
| 2 |
// $Id: googleanalytics.module,v 1.33 2009/10/17 14:55:50 hass Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* @file
|
| 6 |
* Drupal Module: GoogleAnalytics
|
| 7 |
* Adds the required Javascript to the bottom of all your Drupal pages
|
| 8 |
* to allow tracking by the Google Analytics statistics package.
|
| 9 |
*
|
| 10 |
* @author: Mike Carter <www.ixis.co.uk/contact>
|
| 11 |
*/
|
| 12 |
|
| 13 |
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');
|
| 14 |
|
| 15 |
function googleanalytics_help($path, $arg) {
|
| 16 |
switch ($path) {
|
| 17 |
case 'admin/config/system/googleanalytics':
|
| 18 |
return t('<a href="@ga_url">Google Analytics</a> is a free statistics package based on the excellent Urchin system. This module provides services to better integrate Drupal with Google Analytics.', array('@ga_url' => 'http://www.google.com/analytics/'));
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
function googleanalytics_perm() {
|
| 23 |
return array(
|
| 24 |
'administer google analytics' => array(
|
| 25 |
'title' => t('Administer Google Analytics'),
|
| 26 |
'description' => t('Perform maintenance tasks for Google Analytics.'),
|
| 27 |
),
|
| 28 |
'opt-in or out of tracking' => array(
|
| 29 |
'title' => t('Opt-in or out of tracking'),
|
| 30 |
'description' => t('Allow users to decide if tracking code will be added to pages or not.'),
|
| 31 |
),
|
| 32 |
'use PHP for tracking visibility' => array(
|
| 33 |
'title' => t('Use PHP for tracking visibility'),
|
| 34 |
'description' => t('Enter PHP code in the field for tracking visibility settings. %warning', array('%warning' => t('Warning: Give to trusted roles only; this permission has security implications.'))),
|
| 35 |
),
|
| 36 |
);
|
| 37 |
}
|
| 38 |
|
| 39 |
function googleanalytics_menu() {
|
| 40 |
$items['admin/config/system/googleanalytics'] = array(
|
| 41 |
'title' => 'Google Analytics',
|
| 42 |
'description' => 'Configure the settings used to generate your Google Analytics tracking code.',
|
| 43 |
'page callback' => 'drupal_get_form',
|
| 44 |
'page arguments' => array('googleanalytics_admin_settings_form'),
|
| 45 |
'access arguments' => array('administer google analytics'),
|
| 46 |
'type' => MENU_NORMAL_ITEM,
|
| 47 |
'file' => 'googleanalytics.admin.inc',
|
| 48 |
);
|
| 49 |
|
| 50 |
return $items;
|
| 51 |
}
|
| 52 |
|
| 53 |
function googleanalytics_init() {
|
| 54 |
global $user;
|
| 55 |
|
| 56 |
$id = variable_get('googleanalytics_account', '');
|
| 57 |
|
| 58 |
// 1. Check if the GA account number has a value.
|
| 59 |
// 2. Track page views based on visibility value.
|
| 60 |
// 3. Check if we should track the currently active user's role.
|
| 61 |
if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
|
| 62 |
$scope = variable_get('googleanalytics_js_scope', 'footer');
|
| 63 |
|
| 64 |
// Should a local cached copy of ga.js be used?
|
| 65 |
$js_file = 'ga.js';
|
| 66 |
$url = 'http://www.google-analytics.com/'. $js_file;
|
| 67 |
|
| 68 |
if (variable_get('googleanalytics_cache', 0) && $source = _googleanalytics_cache($url)) {
|
| 69 |
drupal_add_js($source, array('type' => 'file', 'scope' => $scope));
|
| 70 |
}
|
| 71 |
else {
|
| 72 |
$script = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
|
| 73 |
$script .= 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/'. $js_file .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
|
| 74 |
drupal_add_js($script, array('type' => 'inline', 'scope' => $scope, 'weight' => JS_DEFAULT - 1));
|
| 75 |
}
|
| 76 |
|
| 77 |
// Add link tracking.
|
| 78 |
$link_settings = array();
|
| 79 |
if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
|
| 80 |
$link_settings['trackOutgoing'] = $track_outgoing;
|
| 81 |
}
|
| 82 |
if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
|
| 83 |
$link_settings['trackMailto'] = $track_mailto;
|
| 84 |
}
|
| 85 |
if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
|
| 86 |
$link_settings['trackDownload'] = $track_download;
|
| 87 |
$link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
|
| 88 |
}
|
| 89 |
if (!empty($link_settings)) {
|
| 90 |
drupal_add_js(array('googleanalytics' => $link_settings), array('type' => 'setting', 'scope' => 'header'));
|
| 91 |
drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', array('type' => 'file', 'scope' => $scope));
|
| 92 |
}
|
| 93 |
|
| 94 |
// Custom tracking.
|
| 95 |
if (variable_get('googleanalytics_trackadsense', FALSE)) {
|
| 96 |
drupal_add_js('window.google_analytics_uacct = ' . drupal_json_encode($id) . ';', array('type' => 'inline', 'scope' => 'header'));
|
| 97 |
}
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Implementation of hook_page_alter() to insert Javascript at the bottom of the page.
|
| 103 |
*/
|
| 104 |
function googleanalytics_page_alter(&$page) {
|
| 105 |
global $user;
|
| 106 |
|
| 107 |
$id = variable_get('googleanalytics_account', '');
|
| 108 |
|
| 109 |
if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
|
| 110 |
|
| 111 |
// Add User profile segmentation values.
|
| 112 |
if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
|
| 113 |
|
| 114 |
$p = module_invoke('profile', 'load_profile', $user);
|
| 115 |
|
| 116 |
$fields = array();
|
| 117 |
foreach ($profile_fields as $field => $title) {
|
| 118 |
$value = $user->$field;
|
| 119 |
|
| 120 |
if (is_array($value)) {
|
| 121 |
$value = implode(',', $value);
|
| 122 |
}
|
| 123 |
|
| 124 |
$fields[$field] = $value;
|
| 125 |
}
|
| 126 |
|
| 127 |
// Only show segmentation variable if there are specified fields.
|
| 128 |
$segmentation = '';
|
| 129 |
if (count($fields) > 0) {
|
| 130 |
$segmentation = 'pageTracker._setVar('. drupal_json_encode(implode(':', $fields)) .');';
|
| 131 |
}
|
| 132 |
}
|
| 133 |
|
| 134 |
// Site search tracking support.
|
| 135 |
$url_custom = '';
|
| 136 |
if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && $keys = search_get_keys()) {
|
| 137 |
$url_custom = drupal_json_encode(url('search/'. arg(1), array('query' => 'search='. $keys)));
|
| 138 |
}
|
| 139 |
|
| 140 |
// If this node is a translation of another node, pass the original
|
| 141 |
// node instead.
|
| 142 |
if (module_exists('translation') && variable_get('googleanalytics_translation_set', 0)) {
|
| 143 |
// Check we have a node object, it supports translation, and its
|
| 144 |
// translated node ID (tnid) doesn't match its own node ID.
|
| 145 |
$node = menu_get_object();
|
| 146 |
if ($node && translation_supported_type($node->type) && isset($node->tnid) && ($node->tnid != $node->nid)) {
|
| 147 |
$source_node = node_load($node->tnid);
|
| 148 |
$languages = language_list();
|
| 149 |
$url_custom = drupal_json_encode(url('node/'. $source_node->nid, array('language' => $languages[$source_node->language])));
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
// Track access denied (403) and file not found (404) pages.
|
| 154 |
if (function_exists('drupal_get_headers')) {
|
| 155 |
$headers = drupal_get_headers();
|
| 156 |
if (strstr($headers, 'HTTP/1.1 403 Forbidden')) {
|
| 157 |
// See http://www.google.com/support/analytics/bin/answer.py?answer=86927
|
| 158 |
$url_custom = '"/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
|
| 159 |
}
|
| 160 |
elseif (strstr($headers, 'HTTP/1.1 404 Not Found')) {
|
| 161 |
$url_custom = '"/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
// Add any custom code snippets if specified.
|
| 166 |
$codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
|
| 167 |
$codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
|
| 168 |
|
| 169 |
// Build tracker code for footer.
|
| 170 |
$script = 'try{';
|
| 171 |
$script .= 'var pageTracker = _gat._getTracker('. drupal_json_encode($id) .');';
|
| 172 |
if (!empty($segmentation)) {
|
| 173 |
$script .= $segmentation;
|
| 174 |
}
|
| 175 |
if (!empty($codesnippet_before)) {
|
| 176 |
$script .= $codesnippet_before;
|
| 177 |
}
|
| 178 |
$script .= 'pageTracker._trackPageview('. $url_custom .');';
|
| 179 |
if (!empty($codesnippet_after)) {
|
| 180 |
$script .= $codesnippet_after;
|
| 181 |
}
|
| 182 |
$script .= '} catch(err) {}';
|
| 183 |
|
| 184 |
drupal_add_js($script, array('type' => 'inline', 'scope' => 'footer'));
|
| 185 |
}
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Implement hook_form_FORM_ID_alter().
|
| 190 |
*
|
| 191 |
* Allow users to decide if tracking code will be added to pages or not.
|
| 192 |
*/
|
| 193 |
function googleanalytics_form_user_profile_form_alter(&$form, &$form_state) {
|
| 194 |
$account = $form['#user'];
|
| 195 |
$category = $form['#user_category'];
|
| 196 |
|
| 197 |
if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
|
| 198 |
$form['googleanalytics'] = array(
|
| 199 |
'#type' => 'fieldset',
|
| 200 |
'#title' => t('Google Analytics configuration'),
|
| 201 |
'#weight' => 3,
|
| 202 |
'#collapsible' => TRUE,
|
| 203 |
'#tree' => TRUE
|
| 204 |
);
|
| 205 |
|
| 206 |
switch ($custom) {
|
| 207 |
case 1:
|
| 208 |
$description = t('Users are tracked by default, but you are able to opt out.');
|
| 209 |
break;
|
| 210 |
|
| 211 |
case 2:
|
| 212 |
$description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
|
| 213 |
break;
|
| 214 |
}
|
| 215 |
|
| 216 |
$form['googleanalytics']['custom'] = array(
|
| 217 |
'#type' => 'checkbox',
|
| 218 |
'#title' => t('Enable user tracking'),
|
| 219 |
'#description' => $description,
|
| 220 |
'#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : ($custom == 1)
|
| 221 |
);
|
| 222 |
|
| 223 |
return $form;
|
| 224 |
}
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* Implementation of hook_requirements().
|
| 229 |
*/
|
| 230 |
function googleanalytics_requirements($phase) {
|
| 231 |
$requirements = array();
|
| 232 |
|
| 233 |
if ($phase == 'runtime') {
|
| 234 |
// Raise warning if Google user account has not been set yet.
|
| 235 |
if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
|
| 236 |
$requirements['googleanalytics'] = array(
|
| 237 |
'title' => t('Google Analytics module'),
|
| 238 |
'description' => t('Google Analytics module has not been configured yet. Please configure its settings from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/settings/googleanalytics'))),
|
| 239 |
'severity' => REQUIREMENT_ERROR,
|
| 240 |
'value' => t('Not configured'),
|
| 241 |
);
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
return $requirements;
|
| 246 |
}
|
| 247 |
|
| 248 |
/**
|
| 249 |
* Implementation of hook_cron().
|
| 250 |
*/
|
| 251 |
function googleanalytics_cron() {
|
| 252 |
// Regenerate the google analytics ga.js every day.
|
| 253 |
if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
|
| 254 |
// New google analytics version.
|
| 255 |
file_unmanaged_delete('public://googleanalytics/ga.js');
|
| 256 |
|
| 257 |
// Clear aggregated JS files.
|
| 258 |
if (variable_get('preprocess_js', 0)) {
|
| 259 |
drupal_clear_js_cache();
|
| 260 |
}
|
| 261 |
|
| 262 |
variable_set('googleanalytics_last_cache', time());
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
/**
|
| 267 |
* Download and cache the ga.js file locally.
|
| 268 |
* @param $location
|
| 269 |
* The full URL to the external javascript file.
|
| 270 |
* @return mixed
|
| 271 |
* The path to the local javascript file on success, boolean FALSE on failure.
|
| 272 |
*/
|
| 273 |
function _googleanalytics_cache($location) {
|
| 274 |
$path = 'public://googleanalytics';
|
| 275 |
$file_destination = $path .'/'. basename($location);
|
| 276 |
if (!file_exists($file_destination)) {
|
| 277 |
$result = drupal_http_request($location);
|
| 278 |
if ($result->code == 200) {
|
| 279 |
// Check that the files directory is writable
|
| 280 |
if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
|
| 281 |
return file_unmanaged_save_data($result->data, $path .'/'. basename($location), FILE_EXISTS_REPLACE);
|
| 282 |
}
|
| 283 |
}
|
| 284 |
}
|
| 285 |
else {
|
| 286 |
return $file_destination;
|
| 287 |
}
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Tracking visibility check for an user object.
|
| 292 |
*
|
| 293 |
* @param $account
|
| 294 |
* A user object containing an array of roles to check.
|
| 295 |
* @return boolean
|
| 296 |
* A decision on if the current user is being tracked by Google Analytics.
|
| 297 |
*/
|
| 298 |
function _googleanalytics_visibility_user($account) {
|
| 299 |
|
| 300 |
$enabled = FALSE;
|
| 301 |
|
| 302 |
// Is current user a member of a role that should be tracked?
|
| 303 |
if (_googleanalytics_visibility_roles($account)) {
|
| 304 |
|
| 305 |
// Use the user's block visibility setting, if necessary.
|
| 306 |
if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
|
| 307 |
if ($account->uid && isset($account->googleanalytics['custom'])) {
|
| 308 |
$enabled = $account->googleanalytics['custom'];
|
| 309 |
}
|
| 310 |
else {
|
| 311 |
$enabled = ($custom == 1);
|
| 312 |
}
|
| 313 |
}
|
| 314 |
else {
|
| 315 |
$enabled = TRUE;
|
| 316 |
}
|
| 317 |
|
| 318 |
}
|
| 319 |
|
| 320 |
return $enabled;
|
| 321 |
}
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Based on visibility setting this function returns TRUE if GA code should
|
| 325 |
* be added for the current role and otherwise FALSE.
|
| 326 |
*/
|
| 327 |
function _googleanalytics_visibility_roles($account) {
|
| 328 |
|
| 329 |
$enabled = FALSE;
|
| 330 |
$roles = variable_get('googleanalytics_roles', array());
|
| 331 |
|
| 332 |
if (array_sum($roles) > 0) {
|
| 333 |
// One or more roles are selected for tracking.
|
| 334 |
foreach (array_keys($account->roles) as $rid) {
|
| 335 |
// Is the current user a member of one role enabled for tracking?
|
| 336 |
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
|
| 337 |
// Current user is a member of a role that should be tracked.
|
| 338 |
$enabled = TRUE;
|
| 339 |
break;
|
| 340 |
}
|
| 341 |
}
|
| 342 |
}
|
| 343 |
else {
|
| 344 |
// No role is selected for tracking, therefor all roles should be tracked.
|
| 345 |
$enabled = TRUE;
|
| 346 |
}
|
| 347 |
|
| 348 |
return $enabled;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Based on visibility setting this function returns TRUE if GA code should
|
| 353 |
* be added to the current page and otherwise FALSE.
|
| 354 |
*/
|
| 355 |
function _googleanalytics_visibility_pages() {
|
| 356 |
static $page_match;
|
| 357 |
|
| 358 |
// Cache visibility setting in hook_init for hook_footer.
|
| 359 |
if (!isset($page_match)) {
|
| 360 |
|
| 361 |
$visibility = variable_get('googleanalytics_visibility', 0);
|
| 362 |
$pages = variable_get('googleanalytics_pages', '');
|
| 363 |
|
| 364 |
// Match path if necessary.
|
| 365 |
if (!empty($pages)) {
|
| 366 |
if ($visibility < 2) {
|
| 367 |
$path = drupal_get_path_alias($_GET['q']);
|
| 368 |
// Compare with the internal and path alias (if any).
|
| 369 |
$page_match = drupal_match_path($path, $pages);
|
| 370 |
if ($path != $_GET['q']) {
|
| 371 |
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
|
| 372 |
}
|
| 373 |
// When $visibility has a value of 0, the block is displayed on
|
| 374 |
// all pages except those listed in $pages. When set to 1, it
|
| 375 |
// is displayed only on those pages listed in $pages.
|
| 376 |
$page_match = !($visibility xor $page_match);
|
| 377 |
}
|
| 378 |
else {
|
| 379 |
$page_match = drupal_eval($pages);
|
| 380 |
}
|
| 381 |
}
|
| 382 |
else {
|
| 383 |
$page_match = TRUE;
|
| 384 |
}
|
| 385 |
|
| 386 |
}
|
| 387 |
return $page_match;
|
| 388 |
}
|