#609892 by joetsuihk, hass, bec, jmesam: Added Multiple custom variables.
[project/google_analytics.git] / googleanalytics.install
CommitLineData
8afc0408 1<?php
2// $Id$
3
4/**
5 * @file
6 * Installation file for Google Analytics module.
7 */
8
9function googleanalytics_install() {
10 variable_set('googleanalytics_visibility', 0);
11
12 // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
13 $pages = array(
14 'admin',
15 'admin/*',
16 'user/*/*',
17 'node/add*',
18 'node/*/*',
19 );
20 variable_set('googleanalytics_pages', implode("\n", $pages));
21}
22
23function googleanalytics_uninstall() {
24 variable_del('googleanalytics_account');
ff05e032 25 variable_del('googleanalytics_custom_vars');
8afc0408 26 variable_del('googleanalytics_codesnippet_before');
27 variable_del('googleanalytics_codesnippet_after');
28 variable_del('googleanalytics_segmentation');
29 variable_del('googleanalytics_trackoutgoing');
30 variable_del('googleanalytics_trackmailto');
31 variable_del('googleanalytics_trackfiles');
32 variable_del('googleanalytics_trackfiles_extensions');
33 variable_del('googleanalytics_cache');
34 variable_del('googleanalytics_last_cache');
35 variable_del('googleanalytics_site_search');
36 variable_del('googleanalytics_trackadsense');
37 variable_del('googleanalytics_js_scope');
38 variable_del('googleanalytics_custom');
39 variable_del('googleanalytics_roles');
40 variable_del('googleanalytics_visibility');
41 variable_del('googleanalytics_pages');
c4bb0ba3 42 variable_del('googleanalytics_tracker_anonymizeip');
8afc0408 43 variable_del('googleanalytics_translation_set');
9b98e3a2 44
45 // Remove backup variables if exits. Remove this code in D8.
46 variable_del('googleanalytics_codesnippet_before_backup_6300');
47 variable_del('googleanalytics_codesnippet_after_backup_6300');
8afc0408 48}
49
50/**
51 * Remove cache directory if module is disabled (or uninstalled).
52 */
53function googleanalytics_disable() {
db78cc00 54 googleanalytics_clear_js_cache();
8afc0408 55}
56
2e588f1a 57/**
58 * Implementation of hook_requirements().
59 */
60function googleanalytics_requirements($phase) {
61 $requirements = array();
62
63 if ($phase == 'runtime') {
64 // Raise warning if Google user account has not been set yet.
65 if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
66 $requirements['googleanalytics'] = array(
67 'title' => t('Google Analytics module'),
3a8fec41 68 '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'))),
2e588f1a 69 'severity' => REQUIREMENT_ERROR,
70 'value' => t('Not configured'),
71 );
72 }
73 }
74
75 return $requirements;
76}
77
8afc0408 78
79function googleanalytics_update_1() {
80 $ret = array();
81
82 $result = db_query("SELECT * FROM {role}");
83 while ($role = db_fetch_object($result)) {
84 // can't use empty spaces in varname
85 $role_varname = str_replace(' ', '_', $role->name);
86 variable_set('googleanalytics_track_'. $role->rid, !variable_get("googleanalytics_track_{$role_varname}", FALSE));
87 variable_del("googleanalytics_track_{$role_varname}");
88 }
89 variable_set('googleanalytics_track__user1', FALSE);
90
91 return $ret;
92}
93
94/**
95 * Upgrade old extension variable to new and use old name as enabled/disabled flag.
96 */
97function googleanalytics_update_6000() {
98 $ret = array();
99
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 $ret[] = array('success' => TRUE, 'query' => 'Updated download tracking settings.');
104
105 return $ret;
106}
107
108function googleanalytics_update_6001() {
109 $ret = array();
110
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 $ret[] = array('success' => TRUE, 'query' => 'Added page tracking to every page except the listed pages: '. implode(', ', $pages));
122
123 return $ret;
124}
125
126/**
127 * Upgrade role settings and per user tracking settings
128 * of "User 1" and remove outdated tracking variables.
129 */
130function googleanalytics_update_6002() {
131 $ret = array();
132
133 // Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
134 $roles = array();
135 foreach (user_roles() as $rid => $name) {
136 if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
137 // Role ID is activated for user tracking.
138 $roles[$rid] = $rid;
139 $ret[] = array('success' => TRUE, 'query' => 'Enabled page tracking for role: '. $name);
140 }
141 else {
142 $ret[] = array('success' => TRUE, 'query' => 'Disabled page tracking for role: '. $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
151 // Load user 1 object, set appropiate value and save new user settings back.
152 $account = user_load(array('uid' => 1));
153 $account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
154 $ret[] = array('success' => TRUE, 'query' => 'Disabled user specific page tracking for website administrator.');
155 }
156
157 // Delete outdated tracking settings.
158 $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'googleanalytics_track_%'");
159
160 return $ret;
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 $ret = array();
168 menu_rebuild();
169 return $ret;
170}
171
172/**
173 * Change visibility setting for path "user/*".
174 */
175function googleanalytics_update_6004() {
176 $ret = array();
177
178 // Orginal pages setting.
179 $pages = array(
180 'admin*',
181 'user*',
182 'node/add*',
183 'node/*/*',
184 );
185
186 $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
187 if (empty($diff)) {
188 // No diff to original settings found. Update with new settings.
189 $pages = array(
190 'admin*',
191 'user/*/*',
192 'node/add*',
193 'node/*/*',
194 );
195 variable_set('googleanalytics_pages', implode("\n", $pages));
196 $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "user*" to "user/*/*".');
197 }
198 else {
199 $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
200 }
201
202 return $ret;
203}
204
205/**
206 * Change visibility setting for path "admin*".
207 */
208function googleanalytics_update_6005() {
209 $ret = array();
210
211 // Orginal pages setting.
212 $pages = array(
213 'admin*',
214 'user/*/*',
215 'node/add*',
216 'node/*/*',
217 );
218
219 $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
220 if (empty($diff)) {
221 // No diff to original settings found. Update with new settings.
222 $pages = array(
223 'admin',
224 'admin/*',
225 'user/*/*',
226 'node/add*',
227 'node/*/*',
228 );
229 variable_set('googleanalytics_pages', implode("\n", $pages));
230 $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
231 }
232 else {
233 $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
234 }
235
236 return $ret;
237}
238
239/**
240 * Upgrade custom javascript settings.
241 */
242function googleanalytics_update_6006() {
243 $ret = array();
244
245 variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
246 variable_del('googleanalytics_codesnippet');
247 $ret[] = array('success' => TRUE, 'query' => 'Upgraded custom javascript codesnippet setting.');
248
249 return $ret;
250}
251
252/**
253 * Remove "User identifier" and "User name" from segmentation fields.
2e588f1a 254 *
8afc0408 255 * This is a data protection and privacy law change. For more information see Google Analytics
256 * terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
257 */
258function googleanalytics_update_6007() {
259 $ret = array();
260
261 $profile_fields = variable_get('googleanalytics_segmentation', array());
262 unset($profile_fields['uid']);
263 unset($profile_fields['name']);
264 variable_set('googleanalytics_segmentation', $profile_fields);
265 $ret[] = array('success' => TRUE, 'query' => 'Removed "User identifier" and "User name" from segmentation fields.');
266
267 return $ret;
268}
269
270/**
271 * Remove outdated legacy support variables and files.
272 */
273function googleanalytics_update_6200() {
274 $ret = array();
275
276 $path = file_directory_path() .'/googleanalytics';
277 if (file_exists($path)) {
278 file_delete($path .'/urchin.js');
279 }
280 variable_del('googleanalytics_legacy_version');
281
282 $ret[] = array('success' => TRUE, 'query' => 'Removed legacy support.');
283
284 return $ret;
285}
286
287/**
288 * Update list of default file extensions.
289 */
290function googleanalytics_update_6201() {
291 $ret = array();
292
293 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') {
294 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');
295 }
296 $ret[] = array('success' => TRUE, 'query' => 'The default extensions for download tracking have been updated.');
297
298 return $ret;
299}
9b98e3a2 300
301/**
302 * Try to update Google Analytics custom code snippet to async version.
303 */
304function googleanalytics_update_6300() {
305 $ret = array();
306
307 // TODO: Backup synchronous code snippets. Remove variables in D8.
308 variable_set('googleanalytics_codesnippet_before_backup_6300', variable_get('googleanalytics_codesnippet_before', ''));
309 variable_set('googleanalytics_codesnippet_after_backup_6300', variable_get('googleanalytics_codesnippet_after', ''));
310
311 // Upgrade of BEFORE code snippet.
312 $code_before = variable_get('googleanalytics_codesnippet_before', '');
313 if (!empty($code_before)) {
314 // No value, e.g. _setLocalRemoteServerMode()
315 $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(\);(.*)/i', '$1_gaq.push(["$2"]);$3', $code_before);
316 // One value, e.g. _setCookiePath()
317 $code_before = preg_replace('/(.*)pageTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_gaq.push(["$2", $3$4$5]);$6', $code_before);
318 // Multiple values e.g. _trackEvent()
319 $code_before = preg_replace('/(.*)pageTracker\.(\w+)\((.*)\);(.*)/i', '$1_gaq.push(["$2", $3]);$4', $code_before);
320
321 variable_set('googleanalytics_codesnippet_before', $code_before);
322
323 $ret[] = array('success' => TRUE, 'query' => 'Upgraded custom "before" code snippet.');
324 drupal_set_message(db_prefix_tables("<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');
325 }
326
327 // Upgrade of AFTER code snippet.
328 // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
329 $code_after = variable_get('googleanalytics_codesnippet_after', '');
330 if (!empty($code_after)) {
331 $ret[] = array('success' => TRUE, 'query' => 'Skipped custom "after" code snippet.');
332 drupal_set_message(db_prefix_tables("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');
333 }
334
335 return $ret;
336}
ff05e032 337
338/**
339 * Upgrade "User roles" tracking to custom variables.
340 */
341function googleanalytics_update_6301() {
342 $ret = array();
343
344 // Read previous segmentation settings.
345 $segmentation = variable_get('googleanalytics_segmentation', array());
346
347 if (module_exists('token') && in_array('roles', $segmentation)) {
348 // Upgrade previous segmentation settings to new custom variables settings.
349 $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
350
351 $googleanalytics_custom_vars['slots'][1]['slot'] = 1;
352 $googleanalytics_custom_vars['slots'][1]['name'] = 'User roles';
353 $googleanalytics_custom_vars['slots'][1]['value'] = '[roles]';
354 $googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level.
355
356 variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars);
357 $ret[] = array('success' => TRUE, 'query' => '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!');
358 }
359 else {
360 $ret[] = array('success' => TRUE, 'query' => 'You need to upgrade the deprecated profile segmentation settings to custom variables manually or you may loose tracking data in future!');
361 $ret[] = array('success' => TRUE, 'query' => 'Token module is recommended for custom variables.');
362 }
363
364 return $ret;
365}