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