googleanalytics.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/google_analytics/googleanalytics.module
    2. 6 contributions/google_analytics/googleanalytics.module
    3. 7 contributions/google_analytics/googleanalytics.module

Constants

NameDescription
GA_TRACKFILES_EXTENSIONS

Functions & methods

NameDescription
googleanalytics_admin_settings_formImplementation of hook_admin_settings() for configuring the module
googleanalytics_admin_settings_form_submit
googleanalytics_admin_settings_form_validate
googleanalytics_cronImplementation of hook_cron().
googleanalytics_footerImplementation of hook_footer() to insert Javascript at the end of the page
googleanalytics_help
googleanalytics_menu
googleanalytics_perm
googleanalytics_requirementsImplementation of hook_requirements().
googleanalytics_userImplementation of hook_user().
_googleanalytics_cacheDownload and cache the urchin.js file locally.
_googleanalytics_match_pathD6 backport of drupal_match_path().
_googleanalytics_user_rolesD6 backport orders core standard roles on top and translate core roles.
_googleanalytics_visibility_pagesBased on visibility setting this function returns TRUE if GA code should be added to the current page and otherwise FALSE.
_googleanalytics_visibility_rolesBased on visibility setting this function returns TRUE if GA code should be added for the current role and otherwise FALSE.
_googleanalytics_visibility_userTracking visibility check for an user object.

File

View source
  1. <?php
  2. /*
  3. * @file
  4. * Drupal Module: GoogleAnalytics
  5. * Adds the required Javascript to the bottom of all your Drupal pages
  6. * to allow tracking by the Google Analytics statistics package.
  7. *
  8. * @author: Alexander Hass <http://drupal.org/user/85918>
  9. */
  10. 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');
  11. function googleanalytics_help($section) {
  12. switch ($section) {
  13. case 'admin/settings/googleanalytics':
  14. 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/'));
  15. }
  16. }
  17. function googleanalytics_perm() {
  18. return array('administer google analytics', 'opt-in or out of tracking', 'use PHP for tracking visibility');
  19. }
  20. function googleanalytics_menu($maycache) {
  21. global $user;
  22. $items = array();
  23. if ($maycache) {
  24. $items[] = array(
  25. 'path' => 'admin/settings/googleanalytics',
  26. 'title' => t('Google Analytics'),
  27. 'description' => t('Configure the settings used to generate your Google Analytics tracking code.'),
  28. 'callback' => 'drupal_get_form',
  29. 'callback arguments' => 'googleanalytics_admin_settings_form',
  30. 'access' => user_access('administer google analytics'),
  31. 'type' => MENU_NORMAL_ITEM,
  32. );
  33. }
  34. else {
  35. $id = variable_get('googleanalytics_account', '');
  36. // 1. Check if the GA account number has a value.
  37. // 2. Track page views based on visibility value.
  38. // 3. Check if we should track the currently active user's role.
  39. if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
  40. // Use the old version of Google Analytics?
  41. $legacy_version = variable_get('googleanalytics_legacy_version', 0);
  42. $scope = variable_get('googleanalytics_js_scope', 'footer');
  43. // Should a local cached copy of urchin.js or ga.js be used?
  44. $js_file = ($legacy_version) ? 'urchin.js' : 'ga.js';
  45. $url = 'http://www.google-analytics.com/'. $js_file;
  46. if (variable_get('googleanalytics_cache', 0) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) && $source = _googleanalytics_cache($url)) {
  47. drupal_add_js($source, 'module', $scope);
  48. }
  49. else {
  50. $script = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
  51. $script .= 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/'. $js_file .'\' type=\'text/javascript\'%3E%3C/script%3E"));';
  52. drupal_add_js($script, 'inline', $scope);
  53. }
  54. // Add link tracking.
  55. $link_settings = array();
  56. if ($track_outgoing = variable_get('googleanalytics_trackoutgoing', 1)) {
  57. $link_settings['trackOutgoing'] = $track_outgoing;
  58. }
  59. if ($track_mailto = variable_get('googleanalytics_trackmailto', 1)) {
  60. $link_settings['trackMailto'] = $track_mailto;
  61. }
  62. if (($track_download = variable_get('googleanalytics_trackfiles', 1)) && ($trackfiles_extensions = variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS))) {
  63. $link_settings['trackDownload'] = $track_download;
  64. $link_settings['trackDownloadExtensions'] = $trackfiles_extensions;
  65. }
  66. if (!empty($link_settings)) {
  67. // TODO: Why is this legacy_version added as data type string and not integer to settings?
  68. $link_settings['LegacyVersion'] = $legacy_version ? 1 : 0;
  69. drupal_add_js(array('googleanalytics' => $link_settings), 'setting', 'header');
  70. drupal_add_js(drupal_get_path('module', 'googleanalytics') .'/googleanalytics.js', 'module', $scope);
  71. }
  72. // Custom tracking.
  73. if (variable_get('googleanalytics_trackadsense', FALSE)) {
  74. drupal_add_js('window.google_analytics_uacct = ' . drupal_to_js($id) . ';', 'inline', 'header');
  75. }
  76. }
  77. }
  78. return $items;
  79. }
  80. /**
  81. * Implementation of hook_footer() to insert Javascript at the end of the page
  82. */
  83. function googleanalytics_footer($main = 0) {
  84. global $user;
  85. $id = variable_get('googleanalytics_account', '');
  86. // 1. Check if the GA account number has a value.
  87. // 2. Track page views based on visibility value.
  88. // 3. Check if we should track the currently active user's role.
  89. if (!empty($id) && _googleanalytics_visibility_pages() && _googleanalytics_visibility_user($user)) {
  90. // Use the old version of Google Analytics?
  91. $legacy_version = variable_get('googleanalytics_legacy_version', 0);
  92. // Add User profile segmentation values
  93. if (is_array($profile_fields = variable_get('googleanalytics_segmentation', '')) && ($user->uid > 0)) {
  94. $p = module_invoke('profile', 'load_profile', $user);
  95. $fields = array();
  96. foreach ($profile_fields as $field => $title) {
  97. $value = $user->$field;
  98. if (is_array($value)) {
  99. $value = implode(',', $value);
  100. }
  101. $fields[$field] = $value;
  102. }
  103. // Only show segmentation variable if there are specified fields.
  104. $segmentation = '';
  105. if (count($fields) > 0) {
  106. if ($legacy_version) {
  107. $segmentation = '__utmSetVar('. drupal_to_js(implode(':', $fields)) .');';
  108. } else {
  109. $segmentation = 'pageTracker._setVar('. drupal_to_js(implode(':', $fields)) .');';
  110. }
  111. }
  112. }
  113. // Site search tracking support.
  114. $url_custom = '';
  115. if (module_exists('search') && variable_get('googleanalytics_site_search', FALSE) && arg(0) == 'search' && $keys = search_get_keys()) {
  116. $url_custom = drupal_to_js(url('search/'. arg(1), 'search='. $keys));
  117. }
  118. // Track access denied (403) and file not found (404) pages.
  119. if (function_exists('drupal_get_headers')) {
  120. $headers = drupal_get_headers();
  121. if (strstr($headers, '403 Forbidden')) {
  122. if ($legacy_version) {
  123. // See http://www.google.com/support/analytics/bin/answer.py?answer=86928
  124. $url_custom = '"/403.html?page=" + _udl.pathname + _udl.search';
  125. }
  126. else {
  127. // See http://www.google.com/support/analytics/bin/answer.py?answer=86927
  128. $url_custom = '"/403.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
  129. }
  130. }
  131. elseif (strstr($headers, '404 Not Found')) {
  132. if ($legacy_version) {
  133. $url_custom = '"/404.html?page=" + _udl.pathname + _udl.search';
  134. }
  135. else {
  136. $url_custom = '"/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer';
  137. }
  138. }
  139. }
  140. // Add any custom code snippets if specified
  141. $codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
  142. $codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
  143. // Should the legacy code be used?
  144. $script = 'try{';
  145. if ($legacy_version) {
  146. $script .= '_uacct = '. drupal_to_js($id) .';';
  147. if (!empty($segmentation)) {
  148. $script .= $segmentation;
  149. }
  150. if (!empty($codesnippet_before)) {
  151. $script .= $codesnippet_before;
  152. }
  153. $script .= 'urchinTracker('. $url_custom .');';
  154. if (!empty($codesnippet_after)) {
  155. $script .= $codesnippet_after;
  156. }
  157. }
  158. else {
  159. $script .= 'var pageTracker = _gat._getTracker('. drupal_to_js($id) .');';
  160. if (!empty($segmentation)) {
  161. $script .= $segmentation;
  162. }
  163. if (!empty($codesnippet_before)) {
  164. $script .= $codesnippet_before;
  165. }
  166. $script .= 'pageTracker._trackPageview('. $url_custom .');';
  167. if (!empty($codesnippet_after)) {
  168. $script .= $codesnippet_after;
  169. }
  170. }
  171. $script .= '} catch(err) {}';
  172. drupal_add_js($script, 'inline', 'footer');
  173. }
  174. }
  175. /**
  176. * Implementation of hook_user().
  177. *
  178. * Allow users to decide if tracking code will be added to pages or not.
  179. */
  180. function googleanalytics_user($type, $edit, &$account, $category = NULL) {
  181. switch ($type) {
  182. case 'form':
  183. if ($category == 'account' && user_access('opt-in or out of tracking') && ($custom = variable_get('googleanalytics_custom', 0)) != 0 && _googleanalytics_visibility_roles($account)) {
  184. $form['googleanalytics'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Google Analytics configuration'),
  187. '#weight' => 3,
  188. '#collapsible' => TRUE,
  189. '#tree' => TRUE
  190. );
  191. switch ($custom) {
  192. case 1:
  193. $description = t('Users are tracked by default, but you are able to opt out.');
  194. break;
  195. case 2:
  196. $description = t('Users are <em>not</em> tracked by default, but you are able to opt in.');
  197. break;
  198. }
  199. $form['googleanalytics']['custom'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Enable user tracking'),
  202. '#description' => $description,
  203. '#default_value' => isset($account->googleanalytics['custom']) ? $account->googleanalytics['custom'] : ($custom == 1)
  204. );
  205. return $form;
  206. }
  207. break;
  208. }
  209. }
  210. /**
  211. * Implementation of hook_admin_settings() for configuring the module
  212. */
  213. function googleanalytics_admin_settings_form() {
  214. $form['account'] = array(
  215. '#type' => 'fieldset',
  216. '#title' => t('General settings'),
  217. '#collapsible' => FALSE,
  218. );
  219. $form['account']['googleanalytics_account'] = array(
  220. '#type' => 'textfield',
  221. '#title' => t('Google Analytics account number'),
  222. '#default_value' => variable_get('googleanalytics_account', 'UA-'),
  223. '#size' => 15,
  224. '#maxlength' => 20,
  225. '#required' => TRUE,
  226. '#description' => t('The account number is unique to the websites domain. Click the <strong>Edit</strong> link in your Google Analytics account next to the appropriate profile on the <strong>Analytics Settings</strong> page, then select <strong>Check Status</strong> at the top-right of the table to find the account number (UA-xxxx-x) of your site. You can obtain a user account from the <a href="@url">Google Analytics</a> website.', array('@url' => 'http://www.google.com/analytics/')),
  227. );
  228. // Standard tracking configurations.
  229. $form['user_vis_settings'] = array(
  230. '#type' => 'fieldset',
  231. '#title' => t('User specific tracking settings'),
  232. '#collapsible' => TRUE,
  233. );
  234. $form['user_vis_settings']['googleanalytics_custom'] = array(
  235. '#type' => 'radios',
  236. '#title' => t('Custom tracking settings'),
  237. '#options' => array(
  238. t('Users cannot control whether they are tracked or not.'),
  239. t('Track users by default, but let individual users to opt out.'),
  240. t('Do not track users by default, but let individual users to opt in.')
  241. ),
  242. '#description' => t('Allow individual users to customize the visibility of tracking in their account settings. Only users with %permission permission are allowed to set their own preference.', array('%permission' => t('opt-in or out of tracking'))),
  243. '#default_value' => variable_get('googleanalytics_custom', 0),
  244. );
  245. // Render the role overview.
  246. $form['role_vis_settings'] = array(
  247. '#type' => 'fieldset',
  248. '#title' => t('Role specific tracking settings'),
  249. '#collapsible' => TRUE,
  250. );
  251. $default_role_options = array();
  252. $default_roles = variable_get('googleanalytics_roles', array());
  253. foreach ($default_roles as $default_rid => $checked) {
  254. if ($checked) {
  255. $default_role_options[] = $default_rid;
  256. }
  257. }
  258. $roles = _googleanalytics_user_roles();
  259. $role_options = array();
  260. foreach ($roles as $rid => $name) {
  261. $role_options[$rid] = $name;
  262. }
  263. $form['role_vis_settings']['googleanalytics_roles'] = array(
  264. '#type' => 'checkboxes',
  265. '#title' => t('Add tracking for specific roles'),
  266. '#default_value' => $default_role_options,
  267. '#options' => $role_options,
  268. '#description' => t('Add tracking only for the selected role(s). If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked.'),
  269. );
  270. // Page specific visibility configurations.
  271. $form['page_vis_settings'] = array(
  272. '#type' => 'fieldset',
  273. '#title' => t('Page specific tracking settings'),
  274. '#collapsible' => TRUE,
  275. '#collapsed' => TRUE,
  276. );
  277. $access = user_access('use PHP for tracking visibility');
  278. $visibility = variable_get('googleanalytics_visibility', 0);
  279. $pages = variable_get('googleanalytics_pages', '');
  280. if ($visibility == 2 && !$access) {
  281. $form['page_vis_settings'] = array();
  282. $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
  283. $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages);
  284. }
  285. else {
  286. $options = array(t('Add to every page except the listed pages.'), t('Add to the listed pages only.'));
  287. $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
  288. if ($access) {
  289. $options[] = t('Add if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
  290. $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
  291. }
  292. $form['page_vis_settings']['googleanalytics_visibility'] = array(
  293. '#type' => 'radios',
  294. '#title' => t('Add tracking to specific pages'),
  295. '#options' => $options,
  296. '#default_value' => $visibility,
  297. );
  298. $form['page_vis_settings']['googleanalytics_pages'] = array(
  299. '#type' => 'textarea',
  300. '#title' => t('Pages'),
  301. '#default_value' => $pages,
  302. '#description' => $description,
  303. '#wysiwyg' => FALSE,
  304. );
  305. }
  306. $profile_enabled = module_exists('profile');
  307. $form['segmentation'] = array(
  308. '#type' => 'fieldset',
  309. '#title' => t('User segmentation settings'),
  310. '#collapsible' => TRUE,
  311. );
  312. // Compile a list of fields to show.
  313. $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles')));
  314. if ($profile_enabled) {
  315. $result = db_query('SELECT name, title, type, weight FROM {profile_fields} ORDER BY weight');
  316. while ($record = db_fetch_object($result)) {
  317. $fields[$record->name] = $record->title;
  318. }
  319. }
  320. $form['segmentation']['googleanalytics_segmentation'] = array(
  321. '#type' => 'select',
  322. '#title' => t('Add segmentation information to tracking code'),
  323. '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . (!$profile_enabled ? ' '. t('<a href="@module_list">Enable the profile module</a> to be able to use profile fields for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Make sure you will not associate (or permit any third party to associate) any data gathered from Your Website(s) (or such third parties\' website(s)) with any personally identifying information from any source as part of Your use (or such third parties\' use) of the Google Analytics service. For more information see section 8.1 in the <a href="@ga_tos">Google Analytics terms of use</a>.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('Selecting one or more values is supported. To select multiple items, hold down CTRL (PC) or &#8984; (Mac) while selecting fields.'),
  324. '#default_value' => variable_get('googleanalytics_segmentation', ''),
  325. '#options' => $fields,
  326. '#size' => count($fields)>3 ? 10 : 3,
  327. '#multiple' => TRUE
  328. );
  329. // Link specific configurations.
  330. $form['linktracking'] = array(
  331. '#type' => 'fieldset',
  332. '#title' => t('Link tracking settings'),
  333. '#collapsible' => TRUE,
  334. '#collapsed' => FALSE,
  335. );
  336. $form['linktracking']['googleanalytics_trackoutgoing'] = array(
  337. '#type' => 'checkbox',
  338. '#title' => t('Track outgoing links'),
  339. '#default_value' => variable_get('googleanalytics_trackoutgoing', 1),
  340. '#description' => t('Enables tracking of clicks on outgoing links.')
  341. );
  342. $form['linktracking']['googleanalytics_trackmailto'] = array(
  343. '#type' => 'checkbox',
  344. '#title' => t('Track mailto links'),
  345. '#default_value' => variable_get('googleanalytics_trackmailto', 1),
  346. '#description' => t('Enables tracking of clicks on mailto links.')
  347. );
  348. $form['linktracking']['googleanalytics_trackfiles'] = array(
  349. '#type' => 'checkbox',
  350. '#title' => t('Track download links'),
  351. '#default_value' => variable_get('googleanalytics_trackfiles', 1),
  352. '#description' => t('Enables tracking of clicks on links to files based on the file extensions list below.')
  353. );
  354. $form['linktracking']['googleanalytics_trackfiles_extensions'] = array(
  355. '#type' => 'textfield',
  356. '#title' => t('File extensions to track'),
  357. '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS),
  358. '#description' => t('A pipe separated list of file extensions that should be tracked when clicked with regular expression support. Example: !extensions', array('!extensions' => GA_TRACKFILES_EXTENSIONS)),
  359. '#maxlength' => 255,
  360. );
  361. // Advanced feature configurations.
  362. $form['advanced'] = array(
  363. '#type' => 'fieldset',
  364. '#title' => t('Advanced settings'),
  365. '#collapsible' => TRUE,
  366. '#collapsed' => TRUE,
  367. );
  368. $form['advanced']['googleanalytics_legacy_version'] = array(
  369. '#type' => 'radios',
  370. '#title' => t('Google Analytics version used'),
  371. '#default_value' => variable_get('googleanalytics_legacy_version', 0),
  372. '#options' => array(
  373. 0 => t('Latest (ga.js) tracking code'),
  374. 1 => t('Legacy (urchin.js) tracking code')
  375. ),
  376. '#description' => t('<a href="@ga_js_url">On December 13, 2007, Google rolled out a new API</a> for its tracking code, and suggests all new sites to use this code. You should only use the older legacy code, if you have custom tracking code tied to that API. Otherwise it is suggested you use the latest API, as the legacy code will not receive feature updates and is not compatible with new features.', array('@ga_js_url' => 'http://analytics.blogspot.com/2007/12/announcing-new-graphing-tools-gajs.html')),
  377. );
  378. $form['advanced']['googleanalytics_cache'] = array(
  379. '#type' => 'checkbox',
  380. '#title' => t('Cache tracking code file locally'),
  381. '#description' => t("If checked, the tracking code file is retrieved 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!"),
  382. '#default_value' => variable_get('googleanalytics_cache', 0),
  383. );
  384. if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE) {
  385. $form['advanced']['googleanalytics_cache']['#disabled'] = TRUE;
  386. $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())));
  387. }
  388. $site_search_dependencies = '<div class="admin-dependencies">';
  389. $site_search_dependencies .= t('Depends on: !dependencies', array('!dependencies' => (module_exists('search') ? t('@module (<span class="admin-enabled">enabled</span>)', array('@module' => 'Search')) : t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => 'Search')))));
  390. $site_search_dependencies .= '</div>';
  391. // Google already have many translations, if not - they display a note to change the language.
  392. global $locale;
  393. $form['advanced']['googleanalytics_site_search'] = array(
  394. '#type' => 'checkbox',
  395. '#title' => t('Track internal search'),
  396. '#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <strong>search</strong>. 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 .'&answer=75817')) . $site_search_dependencies,
  397. '#default_value' => variable_get('googleanalytics_site_search', FALSE),
  398. '#disabled' => (module_exists('search') ? FALSE : TRUE),
  399. );
  400. $form['advanced']['googleanalytics_trackadsense'] = array(
  401. '#type' => 'checkbox',
  402. '#title' => t('Track AdSense ads'),
  403. '#description' => t('If checked, your AdSense ads will be tracked in your Google Analytics account.'),
  404. '#default_value' => variable_get('googleanalytics_trackadsense', FALSE),
  405. );
  406. $form['advanced']['codesnippet'] = array(
  407. '#type' => 'fieldset',
  408. '#title' => t('Custom JavaScript code'),
  409. '#collapsible' => TRUE,
  410. '#collapsed' => TRUE,
  411. '#description' => t('You can add custom Google Analytics <a href="@snippets">code snippets</a> here. These will be added to every page that Google Analytics appears on. Before you add custom code to the below textarea\'s you should read <a href="@ga_concepts_overview">Google Analytics Tracking Code - Functional Overview</a> and the <a href="@ga_js_api">Google Analytics Tracking API</a> documentation. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/248699', '@ga_concepts_overview' => 'http://code.google.com/apis/analytics/docs/gaConceptsOverview.html', '@ga_js_api' => 'http://code.google.com/apis/analytics/docs/gaJSApi.html'))
  412. );
  413. $form['advanced']['codesnippet']['googleanalytics_codesnippet_before'] = array(
  414. '#type' => 'textarea',
  415. '#title' => t('Code snippet (before)'),
  416. '#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
  417. '#rows' => 5,
  418. '#wysiwyg' => FALSE,
  419. '#description' => t('Code in this textarea will be added <strong>before</strong> urchinTracker() or pageTracker._trackPageview().')
  420. );
  421. $form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
  422. '#type' => 'textarea',
  423. '#title' => t('Code snippet (after)'),
  424. '#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
  425. '#rows' => 5,
  426. '#wysiwyg' => FALSE,
  427. '#description' => t("Code in this textarea will be added <strong>after</strong> urchinTracker() or pageTracker._trackPageview(). This is useful if you'd like to track a site in two accounts.")
  428. );
  429. $form['advanced']['googleanalytics_js_scope'] = array(
  430. '#type' => 'select',
  431. '#title' => t('JavaScript scope'),
  432. '#description' => t("<strong>Warning:</strong> Google recommends adding the external JavaScript files to footer for performance reasons."),
  433. '#options' => array(
  434. 'footer' => t('Footer'),
  435. 'header' => t('Header'),
  436. ),
  437. '#default_value' => variable_get('googleanalytics_js_scope', 'footer'),
  438. );
  439. return system_settings_form($form);
  440. }
  441. function googleanalytics_admin_settings_form_validate($form_id, $form_values) {
  442. if (!preg_match('/^UA-\d{4,}-\d+$/', $form_values['googleanalytics_account'])) {
  443. form_set_error('googleanalytics_account', t('A valid Google Analytics account number is case sensitive and formatted like UA-xxxxxx-x.'));
  444. }
  445. // This is for the Newbie's who cannot read a text area description.
  446. if (stristr($form_values['googleanalytics_codesnippet_before'], 'http://www.google-analytics.com/urchin.js') || stristr($form_values['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
  447. form_set_error('googleanalytics_codesnippet_before', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  448. }
  449. if (stristr($form_values['googleanalytics_codesnippet_after'], 'http://www.google-analytics.com/urchin.js') || stristr($form_values['googleanalytics_codesnippet_after'], 'google-analytics.com/ga.js')) {
  450. form_set_error('googleanalytics_codesnippet_after', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  451. }
  452. if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_values['googleanalytics_codesnippet_before'])) {
  453. form_set_error('googleanalytics_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  454. }
  455. if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_values['googleanalytics_codesnippet_after'])) {
  456. form_set_error('googleanalytics_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  457. }
  458. }
  459. function googleanalytics_admin_settings_form_submit($form_id, $form_values) {
  460. // Trim some text area values.
  461. $form_values['googleanalytics_pages'] = trim($form_values['googleanalytics_pages']);
  462. $form_values['googleanalytics_codesnippet_before'] = trim($form_values['googleanalytics_codesnippet_before']);
  463. $form_values['googleanalytics_codesnippet_after'] = trim($form_values['googleanalytics_codesnippet_after']);
  464. system_settings_form_submit($form_id, $form_values);
  465. }
  466. /**
  467. * Implementation of hook_requirements().
  468. */
  469. function googleanalytics_requirements($phase) {
  470. $requirements = array();
  471. if ($phase == 'runtime') {
  472. // Raise warning if Google user account has not been set yet.
  473. if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
  474. $requirements['googleanalytics'] = array(
  475. 'title' => t('Google Analytics module'),
  476. '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'))),
  477. 'severity' => REQUIREMENT_ERROR,
  478. 'value' => t('Not configured'),
  479. );
  480. }
  481. }
  482. return $requirements;
  483. }
  484. /**
  485. * Implementation of hook_cron().
  486. */
  487. function googleanalytics_cron() {
  488. // Regenerate the google analytics urchin.js or ga.js every day.
  489. if (time() - variable_get('googleanalytics_last_cache', 0) >= 86400) {
  490. // Legacy google analytics version.
  491. file_delete(file_directory_path() .'/googleanalytics/urchin.js');
  492. // New google analytics version.
  493. file_delete(file_directory_path() .'/googleanalytics/ga.js');
  494. variable_set('googleanalytics_last_cache', time());
  495. }
  496. }
  497. /**
  498. * Download and cache the urchin.js file locally.
  499. * @param $location
  500. * The full URL to the external javascript file.
  501. * @return mixed
  502. * The path to the local javascript file on success, boolean FALSE on failure.
  503. */
  504. function _googleanalytics_cache($location) {
  505. $directory = file_directory_path() .'/googleanalytics';
  506. $file_destination = $directory .'/'. basename($location);
  507. if (!file_exists($file_destination)) {
  508. $result = drupal_http_request($location);
  509. if ($result->code == 200) {
  510. // Check that the files directory is writable
  511. if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
  512. return file_save_data($result->data, $directory .'/'. basename($location), FILE_EXISTS_REPLACE);
  513. }
  514. }
  515. }
  516. else {
  517. return $file_destination;
  518. }
  519. }
  520. /**
  521. * Tracking visibility check for an user object.
  522. *
  523. * @param $account
  524. * A user object containing an array of roles to check.
  525. * @return boolean
  526. * A decision on if the current user is being tracked by Google Analytics.
  527. */
  528. function _googleanalytics_visibility_user($account) {
  529. $enabled = FALSE;
  530. // Is current user a member of a role that should be tracked?
  531. if (_googleanalytics_visibility_roles($account)) {
  532. // Use the user's block visibility setting, if necessary.
  533. if (($custom = variable_get('googleanalytics_custom', 0)) != 0) {
  534. if ($account->uid && isset($account->googleanalytics['custom'])) {
  535. $enabled = $account->googleanalytics['custom'];
  536. }
  537. else {
  538. $enabled = ($custom == 1);
  539. }
  540. }
  541. else {
  542. $enabled = TRUE;
  543. }
  544. }
  545. return $enabled;
  546. }
  547. /**
  548. * Based on visibility setting this function returns TRUE if GA code should
  549. * be added for the current role and otherwise FALSE.
  550. */
  551. function _googleanalytics_visibility_roles($account) {
  552. $enabled = FALSE;
  553. $roles = variable_get('googleanalytics_roles', array());
  554. if (array_sum($roles) > 0) {
  555. // One or more roles are selected for tracking.
  556. foreach (array_keys($account->roles) as $rid) {
  557. // Is the current user a member of one role enabled for tracking?
  558. if (isset($roles[$rid]) && $rid == $roles[$rid]) {
  559. // Current user is a member of a role that should be tracked.
  560. $enabled = TRUE;
  561. break;
  562. }
  563. }
  564. }
  565. else {
  566. // No role is selected for tracking, therefor all roles should be tracked.
  567. $enabled = TRUE;
  568. }
  569. return $enabled;
  570. }
  571. /**
  572. * Based on visibility setting this function returns TRUE if GA code should
  573. * be added to the current page and otherwise FALSE.
  574. */
  575. function _googleanalytics_visibility_pages() {
  576. static $page_match;
  577. // Cache visibility setting in hook_init for hook_footer.
  578. if (!isset($page_match)) {
  579. $visibility = variable_get('googleanalytics_visibility', 0);
  580. $pages = variable_get('googleanalytics_pages', '');
  581. // Match path if necessary.
  582. if (!empty($pages)) {
  583. if ($visibility < 2) {
  584. $path = drupal_get_path_alias($_GET['q']);
  585. // Compare with the internal and path alias (if any).
  586. $page_match = _googleanalytics_match_path($path, $pages);
  587. if ($path != $_GET['q']) {
  588. $page_match = $page_match || _googleanalytics_match_path($_GET['q'], $pages);
  589. }
  590. // When $visibility has a value of 0, the block is displayed on
  591. // all pages except those listed in $pages. When set to 1, it
  592. // is displayed only on those pages listed in $pages.
  593. $page_match = !($visibility xor $page_match);
  594. }
  595. else {
  596. $page_match = drupal_eval($pages);
  597. }
  598. }
  599. else {
  600. $page_match = TRUE;
  601. }
  602. }
  603. return $page_match;
  604. }
  605. /**
  606. * D6 backport of drupal_match_path().
  607. */
  608. function _googleanalytics_match_path($path, $patterns) {
  609. static $regexps;
  610. if (!isset($regexps[$patterns])) {
  611. $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
  612. }
  613. return preg_match($regexps[$patterns], $path);
  614. }
  615. /**
  616. * D6 backport orders core standard roles on top and translate core roles.
  617. */
  618. function _googleanalytics_user_roles() {
  619. // System roles take the first two positions.
  620. $roles = array(
  621. DRUPAL_ANONYMOUS_RID => NULL,
  622. DRUPAL_AUTHENTICATED_RID => NULL,
  623. );
  624. $result = db_query('SELECT * FROM {role} ORDER BY name');
  625. while ($role = db_fetch_object($result)) {
  626. switch ($role->rid) {
  627. // We only translate the built in role names
  628. case DRUPAL_ANONYMOUS_RID:
  629. $roles[$role->rid] = t($role->name);
  630. break;
  631. case DRUPAL_AUTHENTICATED_RID:
  632. $roles[$role->rid] = t($role->name);
  633. break;
  634. default:
  635. $roles[$role->rid] = $role->name;
  636. }
  637. }
  638. // Filter to remove unmatched system roles.
  639. return array_filter($roles);
  640. }