#811156 by hass: Found some people reporting that _gaq.push(["_gat._anonymizeIp"...
[project/google_analytics.git] / googleanalytics.module
CommitLineData
0efd556a 1<?php
430ff5f8 2// $Id$
60b6111f 3
430ff5f8 4/*
97110728 5 * @file
430ff5f8 6 * Drupal Module: GoogleAnalytics
d18f90cc 7 * Adds the required Javascript to the bottom of all your Drupal pages
0efd556a
MC
8 * to allow tracking by the Google Analytics statistics package.
9 *
e755cc10 10 * @author: Alexander Hass <http://drupal.org/user/85918>
0efd556a
MC
11 */
12
e1097f5d 13define('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');
430ff5f8 14
5bcdd55b 15function googleanalytics_help($path, $arg) {
16 switch ($path) {
ffbad14e 17 case 'admin/config/system/googleanalytics':
a8664f42 18 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/'));
0efd556a
MC
19 }
20}
21
34a9e54b 22function googleanalytics_permission() {
bdff3a24 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 ),
ffbad14e 36 );
60b6111f 37}
5bcdd55b 38
60b6111f 39function googleanalytics_menu() {
ffbad14e 40 $items['admin/config/system/googleanalytics'] = array(
5bcdd55b 41 'title' => 'Google Analytics',
a8664f42 42 'description' => 'Configure tracking behavior to get insights into your website traffic and marketing effectiveness.',
5bcdd55b 43 'page callback' => 'drupal_get_form',
60b6111f 44 'page arguments' => array('googleanalytics_admin_settings_form'),
45 'access arguments' => array('administer google analytics'),
5bcdd55b 46 'type' => MENU_NORMAL_ITEM,
ffbad14e 47 'file' => 'googleanalytics.admin.inc',
5bcdd55b 48 );
49
834c2715
MC
50 return $items;
51}
52
bcfa518e 53/**
01f3950e 54 * Implementation of hook_page_alter() to insert JavaScript to the appropriate scope/region of the page.
bcfa518e 55 */
56function googleanalytics_page_alter(&$page) {
2cc17535
MC
57 global $user;
58
430ff5f8 59 $id = variable_get('googleanalytics_account', '');
0efd556a 60
ea86491a 61 // Get page status code for visibility filtering.
62 $status = drupal_get_http_header('Status');
63 $trackable_status_codes = array(
64 '403 Forbidden',
65 '404 Not Found',
66 );
67
60b6111f 68 // 1. Check if the GA account number has a value.
69 // 2. Track page views based on visibility value.
70 // 3. Check if we should track the currently active user's role.
c5f72148 71 // 4. Ignore pages visibility filter for 404 or 403 status codes.
ea86491a 72 if (!empty($id) && (_googleanalytics_visibility_pages() || in_array($status, $trackable_status_codes)) && _googleanalytics_visibility_user($user)) {
60b6111f 73
bcfa518e 74 // We allow different scopes. Default to 'footer' but allow user to override if they really need to.
01f3950e 75 $scope = variable_get('googleanalytics_js_scope', 'header');
76
77 // Custom tracking. Prepend before all other JavaScript.
78 if (variable_get('googleanalytics_trackadsense', FALSE)) {
79 $page['footer']['googleanalytics']['#attached']['js']['window.google_analytics_uacct = ' . drupal_json_encode($id) . ';'] = array(
80 'type' => 'inline',
81 'scope' => 'header',
82 'weight' => JS_LIBRARY - 21,
83 );
84 }
68c107f3 85
60b6111f 86 // Add link tracking.
87 $link_settings = array();
97110728 88 if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
89 $link_settings['trackOutgoing'] = $track_outgoing;
60b6111f 90 }
97110728 91 if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
92 $link_settings['trackMailto'] = $track_mailto;
60b6111f 93 }
97110728 94 if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
95 $link_settings['trackDownload'] = $track_download;
60b6111f 96 $link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
97 }
98 if (!empty($link_settings)) {
6abb9031 99 $page['footer']['googleanalytics']['#attached']['js'][] = array(
100 'type' => 'setting',
101 'data' => array('googleanalytics' => $link_settings),
102 );
103 $page['footer']['googleanalytics']['#attached']['js'][drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js'] = array('scope' => $scope);
60b6111f 104 }
4fe1e402 105
60b6111f 106 // Add User profile segmentation values.
9f1c4438 107 $profile_fields = variable_get('googleanalytics_segmentation', array());
108 if (!empty($profile_fields) && ($user->uid > 0)) {
109 // Invoke all modules having a hook_user_load() implemented.
110 // If the invoked modules extend the $user object, the additional
111 // data can be tracked.
112 module_invoke_all('user_load', array($user->uid => $user));
d2c2fa2c
MC
113
114 $fields = array();
68c107f3 115 foreach ($profile_fields as $field => $title) {
9f1c4438 116 $fields[$field] = is_array($user->$field) ? implode(',', $user->$field) : $user->$field;
68c107f3 117 }
118
60b6111f 119 // Only show segmentation variable if there are specified fields.
5bcdd55b 120 $segmentation = '';
68c107f3 121 if (count($fields) > 0) {
01f3950e 122 $segmentation = '_gaq.push(["_setVar", ' . drupal_json_encode(implode(':', $fields)) . ']);';
d2c2fa2c 123 }
60b6111f 124 }
5215f4f7 125
60b6111f 126 // Site search tracking support.
127 $url_custom = '';
97110728 128 if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && $keys = search_get_keys()) {
505b14cf 129 $url_custom = drupal_json_encode(url('search/'. arg(1), array('query' => 'search='. $keys)));
97110728 130 }
131
132 // If this node is a translation of another node, pass the original
133 // node instead.
134 if (module_exists('translation') && variable_get('googleanalytics_translation_set', 0)) {
135 // Check we have a node object, it supports translation, and its
136 // translated node ID (tnid) doesn't match its own node ID.
137 $node = menu_get_object();
138 if ($node && translation_supported_type($node->type) && isset($node->tnid) && ($node->tnid != $node->nid)) {
139 $source_node = node_load($node->tnid);
140 $languages = language_list();
505b14cf 141 $url_custom = drupal_json_encode(url('node/'. $source_node->nid, array('language' => $languages[$source_node->language])));
97110728 142 }
60b6111f 143 }
144
145 // Track access denied (403) and file not found (404) pages.
bcfa518e 146 if ($status == '403 Forbidden') {
147 // See http://www.google.com/support/analytics/bin/answer.py?answer=86927
148 $url_custom = '"/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
149 }
150 elseif ($status == '404 Not Found') {
151 $url_custom = '"/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
68c107f3 152 }
153
60b6111f 154 // Add any custom code snippets if specified.
97110728 155 $codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
156 $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
157
bcfa518e 158 // Build tracker code.
01f3950e 159 $script = 'var _gaq = _gaq || [];';
160 $script .= '_gaq.push(["_setAccount", ' . drupal_json_encode($id) . ']);';
b44fb664 161 if (variable_get('googleanalytics_tracker_anonymizeip', 0)) {
69ee2449 162 $script .= '_gaq.push(["_gat._anonymizeIp"]);';
b44fb664 163 }
97110728 164 if (!empty($segmentation)) {
165 $script .= $segmentation;
68c107f3 166 }
97110728 167 if (!empty($codesnippet_before)) {
168 $script .= $codesnippet_before;
169 }
01f3950e 170 if (empty($url_custom)) {
171 $script .= '_gaq.push(["_trackPageview"]);';
172 }
173 else {
174 $script .= '_gaq.push(["_trackPageview", ' . $url_custom . ']);';
175 }
97110728 176 if (!empty($codesnippet_after)) {
177 $script .= $codesnippet_after;
68c107f3 178 }
01f3950e 179
180 $script .= '(function() {';
181 $script .= 'var ga = document.createElement("script");';
182 $script .= 'ga.type = "text/javascript";';
183 $script .= 'ga.async = true;';
184
185 // Should a local cached copy of ga.js be used?
186 if (variable_get('googleanalytics_cache', 0) && $source = _googleanalytics_cache('http://www.google-analytics.com/ga.js')) {
187 $script .= 'ga.src = "' . file_create_url($source) . '";';
188 }
189 else {
190 $script .= 'ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";';
191 }
192 $script .= 'var s = document.getElementsByTagName("script")[0];';
193 $script .= 's.parentNode.insertBefore(ga, s);';
194 $script .= '})();';
68c107f3 195
6abb9031 196 // Add tracker code to scope.
197 $page['footer']['googleanalytics']['#attached']['js'][$script] = array(
198 'type' => 'inline',
199 'scope' => $scope,
bcfa518e 200 );
60b6111f 201 }
202}
203
204/**
5034470c 205 * Implement hook_form_FORM_ID_alter().
60b6111f 206 *
207 * Allow users to decide if tracking code will be added to pages or not.
208 */
5034470c 209function googleanalytics_form_user_profile_form_alter(&$form, &$form_state) {
210 $account = $form['#user'];
211 $category = $form['#user_category'];
212
bdff3a24 213 if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
214 $form['googleanalytics'] = array(
215 '#type' => 'fieldset',
216 '#title' => t('Google Analytics configuration'),
217 '#weight' => 3,
218 '#collapsible' => TRUE,
219 '#tree' => TRUE
220 );
d1396871 221
222 switch ($custom) {
223 case 1:
d1396871 224 $description = t('Users are tracked by default, but you are able to opt out.');
225 break;
226
227 case 2:
d1396871 228 $description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
229 break;
230 }
231
bdff3a24 232 $form['googleanalytics']['custom'] = array(
233 '#type' => 'checkbox',
9ec4587a 234 '#title' => t('Enable user tracking'),
d1396871 235 '#description' => $description,
bdff3a24 236 '#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : ($custom == 1)
237 );
238
239 return $form;
0efd556a
MC
240 }
241}
242
0efd556a 243/**
68c107f3 244 * Implementation of hook_cron().
245 */
246function googleanalytics_cron() {
97110728 247 // Regenerate the google analytics ga.js every day.
60b6111f 248 if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
60b6111f 249 // New google analytics version.
ffbad14e 250 file_unmanaged_delete('public://googleanalytics/ga.js');
60b6111f 251
252 // Clear aggregated JS files.
253 if (variable_get('preprocess_js', 0)) {
254 drupal_clear_js_cache();
255 }
256
257 variable_set('googleanalytics_last_cache', time());
68c107f3 258 }
259}
260
261/**
97110728 262 * Download and cache the ga.js file locally.
68c107f3 263 * @param $location
264 * The full URL to the external javascript file.
265 * @return mixed
266 * The path to the local javascript file on success, boolean FALSE on failure.
267 */
60b6111f 268function _googleanalytics_cache($location) {
ffbad14e 269 $path = 'public://googleanalytics';
270 $file_destination = $path .'/'. basename($location);
68c107f3 271 if (!file_exists($file_destination)) {
272 $result = drupal_http_request($location);
273 if ($result->code == 200) {
274 // Check that the files directory is writable
ffbad14e 275 if (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
276 return file_unmanaged_save_data($result->data, $path .'/'. basename($location), FILE_EXISTS_REPLACE);
68c107f3 277 }
278 }
279 }
280 else {
281 return $file_destination;
282 }
283}
60b6111f 284
285/**
286 * Tracking visibility check for an user object.
287 *
288 * @param $account
289 * A user object containing an array of roles to check.
290 * @return boolean
291 * A decision on if the current user is being tracked by Google Analytics.
292 */
293function _googleanalytics_visibility_user($account) {
294
295 $enabled = FALSE;
296
297 // Is current user a member of a role that should be tracked?
298 if (_googleanalytics_visibility_roles($account)) {
299
300 // Use the user's block visibility setting, if necessary.
301 if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
302 if ($account->uid && isset($account->googleanalytics['custom'])) {
303 $enabled = $account->googleanalytics['custom'];
304 }
305 else {
306 $enabled = ($custom == 1);
307 }
308 }
309 else {
310 $enabled = TRUE;
311 }
312
313 }
314
315 return $enabled;
316}
317
318/**
319 * Based on visibility setting this function returns TRUE if GA code should
320 * be added for the current role and otherwise FALSE.
321 */
322function _googleanalytics_visibility_roles($account) {
323
324 $enabled = FALSE;
325 $roles = variable_get('googleanalytics_roles', array());
326
327 if (array_sum($roles) > 0) {
328 // One or more roles are selected for tracking.
329 foreach (array_keys($account->roles) as $rid) {
330 // Is the current user a member of one role enabled for tracking?
331 if (isset($roles[$rid]) && $rid == $roles[$rid]) {
332 // Current user is a member of a role that should be tracked.
333 $enabled = TRUE;
334 break;
335 }
336 }
337 }
338 else {
97110728 339 // No role is selected for tracking, therefor all roles should be tracked.
60b6111f 340 $enabled = TRUE;
341 }
342
343 return $enabled;
344}
345
346/**
347 * Based on visibility setting this function returns TRUE if GA code should
348 * be added to the current page and otherwise FALSE.
349 */
350function _googleanalytics_visibility_pages() {
351 static $page_match;
352
353 // Cache visibility setting in hook_init for hook_footer.
354 if (!isset($page_match)) {
355
356 $visibility = variable_get('googleanalytics_visibility', 0);
357 $pages = variable_get('googleanalytics_pages', '');
358
359 // Match path if necessary.
360 if (!empty($pages)) {
361 if ($visibility < 2) {
362 $path = drupal_get_path_alias($_GET['q']);
363 // Compare with the internal and path alias (if any).
364 $page_match = drupal_match_path($path, $pages);
365 if ($path != $_GET['q']) {
366 $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
367 }
368 // When $visibility has a value of 0, the block is displayed on
369 // all pages except those listed in $pages. When set to 1, it
370 // is displayed only on those pages listed in $pages.
371 $page_match = !($visibility xor $page_match);
372 }
373 else {
374 $page_match = drupal_eval($pages);
375 }
376 }
377 else {
378 $page_match = TRUE;
379 }
380
381 }
382 return $page_match;
383}