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