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