summaryrefslogtreecommitdiffstats
path: root/googleanalytics.module
blob: 3d76a20f7fe900796056eee95ea8eb0e2dea4605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
// $Id$
/*
 * 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: Mike Carter <www.ixis.co.uk/contact>
 */

define('GA_TRACKFILES', 'pdf|zip|mp3');

function googleanalytics_help($section) {
  switch ($section) {
    case 'admin/settings/googleanalytics':
      return t('Google Analytics is a free statistics package based on the excellent Urchin system.');
  }
}

function googleanalytics_menu($maycache) {
  $items = array();
  if ($maycache) {
    $items[] = array(
      'path' => 'admin/settings/googleanalytics',
      'title' => t('Google Analytics'),
      'description' => t('Configure the settings used to generate your Google Analytics tracking code.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'googleanalytics_admin_settings_form',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * 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) {

    // Are we on a secure page?
    $prefix = '://www';
    if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) {
      $prefix = 's://ssl';
    }

    // Use the old version of Google Analytics?
    $legacy_version = variable_get('googleanalytics_legacy_version', TRUE);
    $scope = variable_get('googleanalytics_js_scope', 'footer');

    // 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);
    }

    // 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);
    }

    // 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), '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_admin_settings() for configuring the module
 */
function googleanalytics_admin_settings_form() {
  $form['account'] = array(
    '#type' => 'fieldset',
    '#title' => t('Analytics Account Settings'),
    '#collapsible' => FALSE,
  );

  $form['account']['googleanalytics_account'] = array(
    '#type' => 'textfield',
    '#title' => t('Google Analytics account number'),
    '#default_value' => variable_get('googleanalytics_account', 'UA-'),
    '#size' => 15,
    '#maxlength' => 20,
    '#required' => TRUE,
    '#description' => t('The user account number (UA-xxxxxx-x) is unique to the websites domain. You can obtain a user account from the <a href="@url">Google Analytics</a> website.', array('@url' => 'http://www.google.com/analytics/'))
  );

  $form['account']['googleanalytics_legacy_version'] = array(
    '#type' => 'checkbox',
    '#title' => t('Legacy Google Analytics'),
    '#default_value' => variable_get("googleanalytics_legacy_version", TRUE),
    '#description' => t('This will enable Legacy Google Analytics instead of most recent.<br /> Please note that Legacy Google Analytics will not receive feature updates and is not compatible with new features.'),
   );

  // Render the role overview.
  $result = db_query('SELECT * FROM {role} ORDER BY name');

  $form['roles'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Role Tracking'),
    '#collapsible' => TRUE,
    '#description' => t('Define what user roles should be tracked by Google Analytics. <strong>Note:</strong> Drupal Admin pages are never tracked.'),
  );

  $form['roles']['googleanalytics_track__user1'] = array(
    '#type' => 'checkbox',
    '#title' => t('Admin (user 1)'),
    '#default_value' => variable_get('googleanalytics_track__user1', FALSE),
  );

  while ($role = db_fetch_object($result)) {
    $form['roles']['googleanalytics_track_'. $role->rid] = array(
      '#type' => 'checkbox',
      '#title' => check_plain($role->name),
      '#default_value' => variable_get('googleanalytics_track_'. $role->rid, FALSE),
    );
  }

  $form['segmentation'] = array(
    '#type' => 'fieldset',
    '#title' => t('User Segmentation'),
    '#collapsible' => TRUE,
    '#description' => t('If your users have profile fields completed, you can track your logged in users based on a defined profile field.')
  );

  if (!module_exists('profile')) {
    $form['segmentation']['profile'] = array(
      '#type' => 'markup',
      '#value' => t('You need to activate the !profile to use this feature.', array('!profile' => l(t('Profile module'), 'admin/build/modules'))),
      '#prefix' => '<p>',
      '#suffix' => '</p>'
      );
  }
  else {
    // Compile a list of fields to show.
    $fields = array(
      'uid' => t('User ID'),
      'name' => t('Username'),
      'roles' => t('User Roles')
    );
    $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight');
    while ($record = db_fetch_object($result)) {
      $fields[$record->name] = $record->title;
    }

    $form['segmentation']['googleanalytics_segmentation'] = array(
      '#type' => 'select',
      '#title' => t('Track'),
      '#description' => t('Selecting one or more values allows you to track users by profile values rather than simply an IP address. To select multiple items, hold down CTRL whilst selecting fields.'),
      '#default_value' => variable_get('googleanalytics_segmentation', ''),
      '#options' => $fields,
      '#size' => 10,
      '#multiple' => TRUE
    );
  }

  $form['linktracking'] = array(
    '#type' => 'fieldset',
    '#title' => t('Link tracking'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['linktracking']['googleanalytics_trackfiles'] = array(
    '#type' => 'checkbox',
    '#title' => t('Track files'),
    '#default_value' => variable_get('googleanalytics_trackfiles', TRUE),
    '#description' => t('Enables tracking of files based on below file extensions list.')
  );
  $form['linktracking']['googleanalytics_trackfiles_extensions'] = array(
    '#type' => 'textfield',
    '#title' => t('File Extensions To Track'),
    '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES),
    '#description' => t('A pipe seperated list of file extensions that should be tracked when clicked. Example: !extensions', array('!extensions' => GA_TRACKFILES))
  );

  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('You can add custom Google Analytics code here.')
  );

  $form['advanced']['googleanalytics_cache'] = array(
    '#type' => 'checkbox',
    '#title' => t('Cache tracking code file locally'),
    '#description' => t("If checked, the tracking code file is received from Google Analytics and cached locally. It is updated daily from Google's servers to ensure updates to tracking code are reflected in the local copy. Do not activate this until after Google Analytics has confirmed your tracker."),
    '#default_value' => variable_get('googleanalytics_cache', 0),
  );
  if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
    $form['advanced']['googleanalytics_cache']['#disabled'] = TRUE;
    $form['advanced']['googleanalytics_cache']['#description'] .= ' '. t('<a href="!url">Public file transfers</a> must be enabled to allow local caching.', array('!url' => url('admin/settings/file-system', drupal_get_destination())));
  }

  $site_search_dependencies = '<div class="admin-dependencies">';
  $site_search_dependencies .= t('Depends on: !dependencies', array('!dependencies' => (module_exists('search') ? 'Search'. t(' (<span class="admin-enabled">enabled</span>)') : 'Search'. t(' (<span class="admin-disabled">disabled</span>)'))));
  $site_search_dependencies .= '</div>';

  // Google already have many translations, if not - they display a note to change the language.
  global $locale;
  $form['advanced']['googleanalytics_site_search'] = array(
    '#type' => 'checkbox',
    '#title' => t('Do Track Internal Search'),
    '#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <em>search</em>. For more information see <a href="!url">How do I set up Site Search for my profile</a>.', array('!url' => 'http://www.google.com/support/analytics/bin/answer.py?hl='. $locale .'&amp;answer=75817')) . $site_search_dependencies,
    '#default_value' => variable_get('googleanalytics_site_search', FALSE),
    '#disabled' => (module_exists('search') ? FALSE : TRUE),
  );

  $form['advanced']['googleanalytics_codesnippet'] = array(
    '#type' => 'textarea',
    '#title' => t('Custom JavaScript Code'),
    '#default_value' => variable_get('googleanalytics_codesnippet', ''),
    '#rows' => 15,
    '#description' => t('Paste <a href="@snippets">custom code snippets here</a>. These will be added to every page that Google Analytics appears on. For help with this feature see the <a href="@blog">cutroni.com blog</a>. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/39282', '@blog' => 'http://cutroni.com/blog/'))
  );

  $form['advanced']['googleanalytics_js_scope'] = array(
    '#type' => 'select',
    '#title' => t('JavaScript scope'),
    '#description' => t("<strong>Warning:</strong> Google recommends adding the external JavaScript files to footer for performance reasons."),
    '#options' => array(
      'footer' => t('Footer'),
      'header' => t('Header'),
    ),
    '#default_value' => variable_get('googleanalytics_js_scope', 'footer'),
  );

  return system_settings_form($form);
}

function googleanalytics_admin_settings_form_validate($form_id, $form_values) {
  if (!preg_match('/^UA-\d{4,}-\d+$/', $form_values['googleanalytics_account'])) {
    form_set_error('googleanalytics_account', t('A valid Google Analytics account number is case sensitive and formated like UA-xxxxxx-x.'));
  }
}

/**
 * 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 <a href="@url">Google Analytics settings page</a>.', 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');
    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, but "authenticated user" should be tracked.
    $track = FALSE;
  }

  return $track;
}