googleanalytics.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for Google Analytics module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function googleanalytics_install() {
  10. // By German laws it's always best to enable the anonymizing of IP addresses.
  11. // NOTE: If this is also an important default setting in other countries, please let us know!
  12. $countries = array(
  13. 'DE',
  14. );
  15. if (in_array(variable_get('site_default_country', ''), $countries)) {
  16. variable_set('googleanalytics_tracker_anonymizeip', 1);
  17. }
  18. }
  19. /**
  20. * Implements hook_uninstall().
  21. */
  22. function googleanalytics_uninstall() {
  23. variable_del('googleanalytics_account');
  24. variable_del('googleanalytics_cache');
  25. variable_del('googleanalytics_codesnippet_before');
  26. variable_del('googleanalytics_codesnippet_after');
  27. variable_del('googleanalytics_cross_domains');
  28. variable_del('googleanalytics_custom');
  29. variable_del('googleanalytics_custom_var');
  30. variable_del('googleanalytics_domain_mode');
  31. variable_del('googleanalytics_js_scope');
  32. variable_del('googleanalytics_last_cache');
  33. variable_del('googleanalytics_pages');
  34. variable_del('googleanalytics_roles');
  35. variable_del('googleanalytics_site_search');
  36. variable_del('googleanalytics_trackadsense');
  37. variable_del('googleanalytics_trackdoubleclick');
  38. variable_del('googleanalytics_tracker_anonymizeip');
  39. variable_del('googleanalytics_trackfiles');
  40. variable_del('googleanalytics_trackfiles_extensions');
  41. variable_del('googleanalytics_trackmailto');
  42. variable_del('googleanalytics_trackoutbound');
  43. variable_del('googleanalytics_translation_set');
  44. variable_del('googleanalytics_visibility_pages');
  45. variable_del('googleanalytics_visibility_roles');
  46. // Remove backup variables if exist. Remove this code in D8.
  47. variable_del('googleanalytics_codesnippet_after_backup_6300');
  48. variable_del('googleanalytics_codesnippet_before_backup_6300');
  49. variable_del('googleanalytics_segmentation');
  50. }
  51. /**
  52. * Implements hook_disable().
  53. *
  54. * Remove cache directory if module is disabled (or uninstalled).
  55. */
  56. function googleanalytics_disable() {
  57. googleanalytics_clear_js_cache();
  58. }
  59. /**
  60. * Implements hook_requirements().
  61. */
  62. function googleanalytics_requirements($phase) {
  63. $requirements = array();
  64. $t = get_t();
  65. if ($phase == 'runtime') {
  66. // Raise warning if Google user account has not been set yet.
  67. if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
  68. $requirements['googleanalytics'] = array(
  69. 'title' => $t('Google Analytics module'),
  70. '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/config/system/googleanalytics'))),
  71. 'severity' => REQUIREMENT_WARNING,
  72. 'value' => $t('Not configured'),
  73. );
  74. }
  75. }
  76. return $requirements;
  77. }
  78. /**
  79. * Upgrade old extension variable to new and use old name as enabled/disabled flag.
  80. */
  81. function googleanalytics_update_6000() {
  82. variable_set('googleanalytics_trackfiles_extensions', variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip'));
  83. $trackfiles = variable_get('googleanalytics_trackfiles', '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') ? TRUE : FALSE;
  84. variable_set('googleanalytics_trackfiles', $trackfiles);
  85. return t('Updated download tracking file extensions.');
  86. }
  87. function googleanalytics_update_6001() {
  88. variable_set('googleanalytics_visibility', 0);
  89. // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
  90. $pages = array(
  91. 'admin*',
  92. 'user*',
  93. 'node/add*',
  94. 'node/*/*',
  95. );
  96. variable_set('googleanalytics_pages', implode("\n", $pages));
  97. return t('Added page tracking to every page except the listed pages: @pages.', array('@pages' => implode(', ', $pages)));
  98. }
  99. /**
  100. * Upgrade role settings and per user tracking settings
  101. * of "User 1" and remove outdated tracking variables.
  102. */
  103. function googleanalytics_update_6002() {
  104. // Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
  105. $roles = array();
  106. $messages = array();
  107. foreach (user_roles() as $rid => $name) {
  108. if (variable_get('googleanalytics_track_' . $rid, FALSE)) {
  109. // Role ID is activated for user tracking.
  110. $roles[$rid] = $rid;
  111. $messages[] = t('Enabled page tracking for role: @name.', array('@name' => $name));
  112. }
  113. else {
  114. $messages[] = t('Disabled page tracking for role: @name.', array('@name' => $name));
  115. }
  116. }
  117. variable_set('googleanalytics_roles', $roles);
  118. // Upgrade disabled tracking of "user 1" to new logic.
  119. if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
  120. variable_set('googleanalytics_custom', 1);
  121. // Load user 1 object, set appropriate value and save new user settings back.
  122. $account = user_load(1);
  123. $account = user_save($account, array('data' => array('googleanalytics' => array('custom' => 0))), 'account');
  124. $messages[] = t('Disabled user specific page tracking for site administrator.');
  125. }
  126. // Delete outdated tracking settings.
  127. db_delete('variable')
  128. ->condition('name', db_like('googleanalytics_track_') . '%', 'LIKE')
  129. ->execute();
  130. return implode(', ', $messages);
  131. }
  132. /**
  133. * #262468: Clear menu cache to solve stale menu data in 5.x-1.5 and 6.x-1.1
  134. */
  135. function googleanalytics_update_6003() {
  136. menu_rebuild();
  137. return t('Menu has been rebuild.');
  138. }
  139. /**
  140. * Change visibility setting for path "user/*".
  141. */
  142. function googleanalytics_update_6004() {
  143. // Original pages setting.
  144. $pages = array(
  145. 'admin*',
  146. 'user*',
  147. 'node/add*',
  148. 'node/*/*',
  149. );
  150. $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
  151. if (empty($diff)) {
  152. // No diff to original settings found. Update with new settings.
  153. $pages = array(
  154. 'admin*',
  155. 'user/*/*',
  156. 'node/add*',
  157. 'node/*/*',
  158. );
  159. variable_set('googleanalytics_pages', implode("\n", $pages));
  160. return t('Path visibility filter setting changed from "user*" to "user/*/*".');
  161. }
  162. else {
  163. return t('Custom path visibility filter setting found. Update skipped!');
  164. }
  165. }
  166. /**
  167. * Change visibility setting for path "admin*".
  168. */
  169. function googleanalytics_update_6005() {
  170. // Original pages setting.
  171. $pages = array(
  172. 'admin*',
  173. 'user/*/*',
  174. 'node/add*',
  175. 'node/*/*',
  176. );
  177. $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
  178. if (empty($diff)) {
  179. // No diff to original settings found. Update with new settings.
  180. $pages = array(
  181. 'admin',
  182. 'admin/*',
  183. 'user/*/*',
  184. 'node/add*',
  185. 'node/*/*',
  186. );
  187. variable_set('googleanalytics_pages', implode("\n", $pages));
  188. return t('Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
  189. }
  190. else {
  191. return t('Custom path visibility filter setting found. Update skipped!');
  192. }
  193. }
  194. /**
  195. * Upgrade custom javascript settings.
  196. */
  197. function googleanalytics_update_6006() {
  198. variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
  199. variable_del('googleanalytics_codesnippet');
  200. return t('Upgraded custom javascript codesnippet setting.');
  201. }
  202. /**
  203. * Remove "User identifier" and "User name" from segmentation fields.
  204. *
  205. * This is a data protection and privacy law change. For more information see Google Analytics
  206. * terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
  207. */
  208. function googleanalytics_update_6007() {
  209. $profile_fields = variable_get('googleanalytics_segmentation', array());
  210. unset($profile_fields['uid']);
  211. unset($profile_fields['name']);
  212. variable_set('googleanalytics_segmentation', $profile_fields);
  213. return t('Removed "User identifier" and "User name" from segmentation fields.');
  214. }
  215. /**
  216. * Remove outdated legacy support variables and files.
  217. */
  218. function googleanalytics_update_6200() {
  219. $path = 'public://googleanalytics';
  220. if (file_exists($path)) {
  221. file_unmanaged_delete($path . '/urchin.js');
  222. }
  223. variable_del('googleanalytics_legacy_version');
  224. return t('Removed outdated legacy tracker stuff.');
  225. }
  226. /**
  227. * Update list of default file extensions.
  228. */
  229. function googleanalytics_update_6201() {
  230. if (variable_get('googleanalytics_trackfiles_extensions', '') == '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') {
  231. variable_set('googleanalytics_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');
  232. }
  233. return t('The default extensions for download tracking have been updated.');
  234. }
  235. /**
  236. * Try to update Google Analytics custom code snippet to async version.
  237. */
  238. function googleanalytics_update_6300() {
  239. $messages = array();
  240. // TODO: Backup synchronous code snippets. Remove variables in D8.
  241. variable_set('googleanalytics_codesnippet_before_backup_6300', variable_get('googleanalytics_codesnippet_before', ''));
  242. variable_set('googleanalytics_codesnippet_after_backup_6300', variable_get('googleanalytics_codesnippet_after', ''));
  243. // Upgrade of BEFORE code snippet.
  244. $code_before = variable_get('googleanalytics_codesnippet_before', '');
  245. if (!empty($code_before)) {
  246. // No value, e.g. _setLocalRemoteServerMode()
  247. $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(\);(.*)/i', '$1_gaq.push(["$2"]);$3', $code_before);
  248. // One value, e.g. _setCookiePath()
  249. $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_gaq.push(["$2", $3$4$5]);$6', $code_before);
  250. // Multiple values e.g. _trackEvent()
  251. $code_before = preg_replace('/(.*)pageTracker\.(\w+)\((.*)\);(.*)/i', '$1_gaq.push(["$2", $3]);$4', $code_before);
  252. variable_set('googleanalytics_codesnippet_before', $code_before);
  253. drupal_set_message(Database::getConnection()->prefixTables("<strong>Attempted</strong> to upgrade Google Analytics custom 'before' code snippet. Backup of previous code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_before_backup_6300'. Please consult Google's <a href='https://developers.google.com/analytics/devguides/collection/gajs/'>Asynchronous Tracking Usage Guide</a> if the upgrade was successfully."), 'warning');
  254. $messages[] = t('Upgraded custom "before" code snippet.');
  255. }
  256. // Upgrade of AFTER code snippet.
  257. // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
  258. $code_after = variable_get('googleanalytics_codesnippet_after', '');
  259. if (!empty($code_after)) {
  260. drupal_set_message(Database::getConnection()->prefixTables("Automatic upgrade of Google Analytics custom 'after' code snippet has been skipped. Backup of previous code snippet has been saved in database table '{variable}' as 'googleanalytics_codesnippet_after_backup_6300'. You need to manually upgrade the custom 'after' code snippet."), 'error');
  261. $messages[] = t('Skipped custom "after" code snippet.');
  262. }
  263. return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
  264. }
  265. /**
  266. * Run D6 -> D7 upgrades.
  267. */
  268. function googleanalytics_update_7000() {
  269. // Update JavaScript scope to 'header'.
  270. variable_set('googleanalytics_js_scope', 'header');
  271. $messages[] = t('Google tracking code has been moved to header.');
  272. // Upgrade D6 token placeholder to D7. update_6301 is not required.
  273. $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
  274. if (!empty($googleanalytics_custom_vars['slots'][1]) && $googleanalytics_custom_vars['slots'][1]['name'] == 'User roles' && $googleanalytics_custom_vars['slots'][1]['value'] = '[user-role-names]') {
  275. $googleanalytics_custom_vars['slots'][1]['value'] = '[current-user:role-names]';
  276. variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars);
  277. $messages[] = t("The D6 token placeholder [user-role-names] used in the custom variable 'User roles' has been replaced with [current-user:role-names].");
  278. }
  279. return implode(' ', $messages);
  280. }
  281. /**
  282. * Automatically enable anonymizing of IP addresses for Germany.
  283. */
  284. function googleanalytics_update_7001() {
  285. // By German law it's always best to enable the anonymizing of IP addresses.
  286. $countries = array(
  287. 'DE',
  288. );
  289. if (in_array(variable_get('site_default_country', ''), $countries)) {
  290. variable_set('googleanalytics_tracker_anonymizeip', 1);
  291. return t('The default country in your regional settings is Germany. Anonymizing of IP addresses has been enabled for privacy reasons.');
  292. }
  293. else {
  294. return t('The default country in your regional settings is <em>not</em> Germany. The anonymizing of IP addresses setting has not been changed. Make sure your site settings comply with the local privacy rules.');
  295. }
  296. }
  297. /**
  298. * Upgrade "User roles" tracking to custom variables.
  299. */
  300. function googleanalytics_update_7002() {
  301. // Read previous segmentation settings.
  302. $segmentation = variable_get('googleanalytics_segmentation', array());
  303. // If this is an upgrade from D6 the slot 1 may not empty.
  304. if (empty($googleanalytics_custom_vars['slots'][1]) && in_array('roles', $segmentation)) {
  305. // Upgrade previous segmentation settings to new custom variables settings.
  306. $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
  307. $googleanalytics_custom_vars['slots'][1]['slot'] = 1;
  308. $googleanalytics_custom_vars['slots'][1]['name'] = 'User roles';
  309. $googleanalytics_custom_vars['slots'][1]['value'] = '[current-user:role-names]';
  310. $googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level.
  311. variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars);
  312. return t('The deprecated profile segmentation setting for "User roles" has been added to custom variables. You need to deselect all selected profile fields in <a href="@admin">Google Analytics settings</a> and upgrade other profile fields manually or you may loose tracking data in future! See Google Analytics <a href="@customvar">Custom Variables</a> for more information.', array('@customvar' => 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables', '@admin' => url('admin/config/system/googleanalytics')));
  313. }
  314. else {
  315. return t('You need to deselect all selected profile fields in <a href="@admin">Google Analytics settings</a> and upgrade other profile fields manually or you may loose tracking data in future! See Google Analytics <a href="@customvar">Custom Variables</a> for more information.', array('@customvar' => 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables', '@admin' => url('admin/config/system/googleanalytics')));
  316. }
  317. }
  318. /**
  319. * Rename googleanalytics_trackoutgoing variable to googleanalytics_trackoutbound.
  320. */
  321. function googleanalytics_update_7003() {
  322. variable_set('googleanalytics_trackoutbound', variable_get('googleanalytics_trackoutgoing', 1));
  323. variable_del('googleanalytics_trackoutgoing');
  324. return t('Renamed "googleanalytics_trackoutgoing" settings variable to googleanalytics_trackoutbound.');
  325. }
  326. /**
  327. * Rename googleanalytics_visibility variable to googleanalytics_visibility_pages for consistency.
  328. */
  329. function googleanalytics_update_7004() {
  330. variable_set('googleanalytics_visibility_pages', variable_get('googleanalytics_visibility', 1));
  331. variable_del('googleanalytics_visibility');
  332. return t('Renamed "googleanalytics_visibility" settings variable to googleanalytics_visibility_pages.');
  333. }
  334. /**
  335. * Path visibility filter setting should hide "batch" path.
  336. */
  337. function googleanalytics_update_7005() {
  338. // Current pages setting.
  339. $pages = array(
  340. 'admin',
  341. 'admin/*',
  342. 'user/*/*',
  343. 'node/add*',
  344. 'node/*/*',
  345. );
  346. $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
  347. if (empty($diff)) {
  348. // No diff to previous settings found. Update with new settings.
  349. $pages = array(
  350. 'admin',
  351. 'admin/*',
  352. 'batch',
  353. 'node/add*',
  354. 'node/*/*',
  355. 'user/*/*',
  356. );
  357. variable_set('googleanalytics_pages', implode("\n", $pages));
  358. return t('Added "batch" to path visibility filter setting.');
  359. }
  360. else {
  361. return t('Custom path visibility filter setting found. Update skipped!');
  362. }
  363. }
  364. /**
  365. * Delete obsolete trackOutboundAsPageview variable.
  366. */
  367. function googleanalytics_update_7006() {
  368. variable_del('googleanalytics_trackoutboundaspageview');
  369. return t('Deleted obsolete trackOutboundAsPageview variable.');
  370. }
  371. /**
  372. * Delete obsolete googleanalytics_trackpageloadtime variable.
  373. */
  374. function googleanalytics_update_7007() {
  375. variable_del('googleanalytics_trackpageloadtime');
  376. return t('Deleted obsolete googleanalytics_trackpageloadtime variable.');
  377. }