forked from d6lts/google_analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleanalytics.install
More file actions
484 lines (404 loc) · 19.5 KB
/
googleanalytics.install
File metadata and controls
484 lines (404 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* @file
* Installation file for Google Analytics module.
*/
function googleanalytics_uninstall() {
variable_del('googleanalytics_account');
variable_del('googleanalytics_cache');
variable_del('googleanalytics_codesnippet_create');
variable_del('googleanalytics_codesnippet_before');
variable_del('googleanalytics_codesnippet_after');
variable_del('googleanalytics_cross_domains');
variable_del('googleanalytics_custom');
variable_del('googleanalytics_custom_dimension');
variable_del('googleanalytics_custom_metric');
variable_del('googleanalytics_debug');
variable_del('googleanalytics_domain_mode');
variable_del('googleanalytics_last_cache');
variable_del('googleanalytics_pages');
variable_del('googleanalytics_roles');
variable_del('googleanalytics_site_search');
variable_del('googleanalytics_trackadsense');
variable_del('googleanalytics_trackdoubleclick');
variable_del('googleanalytics_tracker_anonymizeip');
variable_del('googleanalytics_trackfiles');
variable_del('googleanalytics_trackfiles_extensions');
variable_del('googleanalytics_tracklinkid');
variable_del('googleanalytics_trackurlfragments');
variable_del('googleanalytics_trackuserid');
variable_del('googleanalytics_trackmailto');
variable_del('googleanalytics_trackmessages');
variable_del('googleanalytics_trackoutgoing');
variable_del('googleanalytics_translation_set');
variable_del('googleanalytics_visibility');
variable_del('googleanalytics_visibility_roles');
variable_del('googleanalytics_privacy_donottrack');
// Remove backup variables if exists. Remove this code in D8.
variable_del('googleanalytics_codesnippet_after_backup_6400');
variable_del('googleanalytics_codesnippet_before_backup_6400');
}
/**
* Remove cache directory if module is disabled (or uninstalled).
*/
function googleanalytics_disable() {
googleanalytics_clear_js_cache();
}
/**
* Implementation of hook_requirements().
*/
function googleanalytics_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
// Raise warning if Google user account has not been set yet.
if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
$requirements['googleanalytics'] = array(
'title' => t('Google Analytics module'),
'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'))),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not configured'),
);
}
// Raise warning if debugging is enabled.
if (variable_get('googleanalytics_debug', 0)) {
$requirements['google_analytics_debugging'] = array(
'title' => t('Google Analytics module'),
'description' => t('Google Analytics module has debugging enabled. Please disable debugging setting in production sites from the <a href="@url">Google Analytics settings page</a>.', array('@url' => url('admin/settings/googleanalytics'))),
'severity' => REQUIREMENT_WARNING,
'value' => t('Debugging enabled'),
);
}
}
return $requirements;
}
function googleanalytics_update_1() {
$ret = array();
$result = db_query("SELECT * FROM {role}");
while ($role = db_fetch_object($result)) {
// can't use empty spaces in varname
$role_varname = str_replace(' ', '_', $role->name);
variable_set('googleanalytics_track_'. $role->rid, !variable_get("googleanalytics_track_{$role_varname}", FALSE));
variable_del("googleanalytics_track_{$role_varname}");
}
variable_set('googleanalytics_track__user1', FALSE);
return $ret;
}
/**
* Upgrade old extension variable to new and use old name as enabled/disabled flag.
*/
function googleanalytics_update_6000() {
$ret = array();
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'));
$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;
variable_set('googleanalytics_trackfiles', $trackfiles);
$ret[] = array('success' => TRUE, 'query' => 'Updated download tracking settings.');
return $ret;
}
function googleanalytics_update_6001() {
$ret = array();
variable_set('googleanalytics_visibility', 0);
// Remove tracking from all administrative pages, see http://drupal.org/node/34970.
$pages = array(
'admin*',
'user*',
'node/add*',
'node/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
$ret[] = array('success' => TRUE, 'query' => 'Added page tracking to every page except the listed pages: '. implode(', ', $pages));
return $ret;
}
/**
* Upgrade role settings and per user tracking settings
* of "User 1" and remove outdated tracking variables.
*/
function googleanalytics_update_6002() {
$ret = array();
// Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
$roles = array();
foreach (user_roles() as $rid => $name) {
if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
// Role ID is activated for user tracking.
$roles[$rid] = $rid;
$ret[] = array('success' => TRUE, 'query' => 'Enabled page tracking for role: '. $name);
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Disabled page tracking for role: '. $name);
}
}
variable_set('googleanalytics_roles', $roles);
// Upgrade disabled tracking of "user 1" to new logic.
if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
variable_set('googleanalytics_custom', 1);
// Load user 1 object, set appropiate value and save new user settings back.
$account = user_load(array('uid' => 1));
$account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
$ret[] = array('success' => TRUE, 'query' => 'Disabled user specific page tracking for website administrator.');
}
// Delete outdated tracking settings.
$ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'googleanalytics_track_%'");
return $ret;
}
/**
* #262468: Clear menu cache to solve stale menu data in 5.x-1.5 and 6.x-1.1
*/
function googleanalytics_update_6003() {
$ret = array();
menu_rebuild();
return $ret;
}
/**
* Change visibility setting for path "user/*".
*/
function googleanalytics_update_6004() {
$ret = array();
// Orginal pages setting.
$pages = array(
'admin*',
'user*',
'node/add*',
'node/*/*',
);
$diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
if (empty($diff)) {
// No diff to original settings found. Update with new settings.
$pages = array(
'admin*',
'user/*/*',
'node/add*',
'node/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
$ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "user*" to "user/*/*".');
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
}
return $ret;
}
/**
* Change visibility setting for path "admin*".
*/
function googleanalytics_update_6005() {
$ret = array();
// Orginal pages setting.
$pages = array(
'admin*',
'user/*/*',
'node/add*',
'node/*/*',
);
$diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
if (empty($diff)) {
// No diff to original settings found. Update with new settings.
$pages = array(
'admin',
'admin/*',
'user/*/*',
'node/add*',
'node/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
$ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
}
return $ret;
}
/**
* Upgrade custom javascript settings.
*/
function googleanalytics_update_6006() {
$ret = array();
variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
variable_del('googleanalytics_codesnippet');
$ret[] = array('success' => TRUE, 'query' => 'Upgraded custom javascript codesnippet setting.');
return $ret;
}
/**
* Remove "User identifier" and "User name" from segmentation fields.
*
* This is a data protection and privacy law change. For more information see Google Analytics
* terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
*/
function googleanalytics_update_6007() {
$ret = array();
$profile_fields = variable_get('googleanalytics_segmentation', array());
unset($profile_fields['uid']);
unset($profile_fields['name']);
variable_set('googleanalytics_segmentation', $profile_fields);
$ret[] = array('success' => TRUE, 'query' => 'Removed "User identifier" and "User name" from segmentation fields.');
return $ret;
}
/**
* Remove outdated legacy support variables and files.
*/
function googleanalytics_update_6200() {
$ret = array();
$path = file_directory_path() .'/googleanalytics';
if (file_exists($path)) {
file_delete($path .'/urchin.js');
}
variable_del('googleanalytics_legacy_version');
$ret[] = array('success' => TRUE, 'query' => 'Removed legacy support.');
return $ret;
}
/**
* Update list of default file extensions.
*/
function googleanalytics_update_6201() {
$ret = array();
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') {
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');
}
$ret[] = array('success' => TRUE, 'query' => 'The default extensions for download tracking have been updated.');
return $ret;
}
/**
* Try to update Google Analytics custom code snippet to async version.
*/
function googleanalytics_update_6300() {
$ret = array();
// TODO: Backup synchronous code snippets. Remove variables in D8.
variable_set('googleanalytics_codesnippet_before_backup_6300', variable_get('googleanalytics_codesnippet_before', ''));
variable_set('googleanalytics_codesnippet_after_backup_6300', variable_get('googleanalytics_codesnippet_after', ''));
// Upgrade of BEFORE code snippet.
$code_before = variable_get('googleanalytics_codesnippet_before', '');
if (!empty($code_before)) {
// No value, e.g. _setLocalRemoteServerMode()
$code_before = preg_replace('/(.*)pageTracker\.(\w+)\(\);(.*)/i', '$1_gaq.push(["$2"]);$3', $code_before);
// One value, e.g. _setCookiePath()
$code_before = preg_replace('/(.*)pageTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_gaq.push(["$2", $3$4$5]);$6', $code_before);
// Multiple values e.g. _trackEvent()
$code_before = preg_replace('/(.*)pageTracker\.(\w+)\((.*)\);(.*)/i', '$1_gaq.push(["$2", $3]);$4', $code_before);
variable_set('googleanalytics_codesnippet_before', $code_before);
$ret[] = array('success' => TRUE, 'query' => 'Upgraded custom "before" code snippet.');
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='https://developers.google.com/analytics/devguides/collection/gajs/'>Asynchronous Tracking Usage Guide</a> if the upgrade was successfully."), 'warning');
}
// Upgrade of AFTER code snippet.
// We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
$code_after = variable_get('googleanalytics_codesnippet_after', '');
if (!empty($code_after)) {
$ret[] = array('success' => TRUE, 'query' => 'Skipped custom "after" code snippet.');
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');
}
return $ret;
}
/**
* Upgrade "User roles" tracking to custom variables.
*/
function googleanalytics_update_6301() {
$ret = array();
// Read previous segmentation settings.
$segmentation = variable_get('googleanalytics_segmentation', array());
if (module_exists('token') && in_array('roles', $segmentation)) {
// Upgrade previous segmentation settings to new custom variables settings.
$googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array());
$googleanalytics_custom_vars['slots'][1]['slot'] = 1;
$googleanalytics_custom_vars['slots'][1]['name'] = 'User roles';
$googleanalytics_custom_vars['slots'][1]['value'] = '[user-role-names]';
$googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level.
variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars);
$ret[] = array('success' => TRUE, 'query' => 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/settings/googleanalytics'))));
}
else {
$ret[] = array('success' => TRUE, 'query' => 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/settings/googleanalytics'))));
$ret[] = array('success' => TRUE, 'query' => t('<a href="@token">Token</a> module is recommended for custom variables.', array('@token' => 'http://drupal.org/project/token')));
}
return $ret;
}
/**
* Path visibility filter setting should hide "batch" path.
*/
function googleanalytics_update_6302() {
$ret = array();
// Current pages setting.
$pages = array(
'admin',
'admin/*',
'user/*/*',
'node/add*',
'node/*/*',
);
$diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
if (empty($diff)) {
// No diff to previous settings found. Update with new settings.
$pages = array(
'admin',
'admin/*',
'batch',
'node/add*',
'node/*/*',
'user/*/*',
);
variable_set('googleanalytics_pages', implode("\n", $pages));
$ret[] = array('success' => TRUE, 'query' => 'Added "batch" to path visibility filter setting.');
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
}
return $ret;
}
/**
* Delete obsolete trackOutboundAsPageview variable.
*/
function googleanalytics_update_6303() {
$ret = array();
variable_del('googleanalytics_trackoutboundaspageview');
$ret[] = array('success' => TRUE, 'query' => 'Deleted obsolete trackOutboundAsPageview variable.');
return $ret;
}
/**
* Delete obsolete googleanalytics_trackpageloadtime variable.
*/
function googleanalytics_update_6304() {
$ret = array();
variable_del('googleanalytics_trackpageloadtime');
$ret[] = array('success' => TRUE, 'query' => 'Deleted obsolete googleanalytics_trackpageloadtime variable.');
return $ret;
}
/**
* Upgrade module to 6.x-4.x
*/
function googleanalytics_update_6400() {
$messages = array();
variable_del('googleanalytics_segmentation');
variable_del('googleanalytics_codesnippet_before_backup_6300');
variable_del('googleanalytics_codesnippet_after_backup_6300');
$messages[] = array('success' => TRUE, 'query' => 'Removed backup variables from pre-ga.js days.');
variable_del('googleanalytics_js_scope');
$messages[] = array('success' => TRUE, 'query' => 'Removed obsolete JavaScript scope variable.');
variable_del('googleanalytics_custom_var');
$messages[] = array('success' => TRUE, 'query' => 'Deleted obsolete custom variables. Custom variables are now custom dimensions and metrics and you need to manually configure them!');
// ga.js code will cause the tracker to break. Remove custom code snippets.
$googleanalytics_codesnippet_before = variable_get('googleanalytics_codesnippet_before', '');
if (!empty($googleanalytics_codesnippet_before)) {
variable_set('googleanalytics_codesnippet_before_backup_6400', $googleanalytics_codesnippet_before);
variable_del('googleanalytics_codesnippet_before');
drupal_set_message(db_prefix_tables("A backup of your previous Google Analytics code snippet (ga.js) has been saved in database table '{variable}' as 'googleanalytics_codesnippet_before_backup_6400'. You need to manually upgrade the custom 'before' code snippet to analytics.js API."), 'warning');
$messages[] = array('success' => TRUE, 'query' => 'Manual upgrade of custom "before" code snippet from ja.js to analytics.js API is required.');
}
$googleanalytics_codesnippet_after = variable_get('googleanalytics_codesnippet_after', '');
if (!empty($googleanalytics_codesnippet_after)) {
variable_set('googleanalytics_codesnippet_after_backup_6400', $googleanalytics_codesnippet_after);
variable_del('googleanalytics_codesnippet_after');
drupal_set_message(db_prefix_tables("A backup of your previous Google Analytics code snippet (ga.js) has been saved in database table '{variable}' as 'googleanalytics_codesnippet_after_backup_6400'. You need to manually upgrade the custom 'after' code snippet to analytics.js API."), 'warning');
$messages[] = array('success' => TRUE, 'query' => 'Manual upgrade of custom "after" code snippet from ja.js to analytics.js API is required.');
}
return $messages;
}
/**
* Update list of default file extensions.
*/
function googleanalytics_update_6401() {
$ret = array();
if (variable_get('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') {
variable_set('googleanalytics_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc(x|m)?|dot(x|m)?|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt(x|m)?|pot(x|m)?|pps(x|m)?|ppam|sld(x|m)?|thmx|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls(x|m|b)?|xlt(x|m)|xlam|xml|z|zip');
$ret[] = array('success' => TRUE, 'query' => 'The default extensions for download tracking have been updated.');
}
else {
$ret[] = array('success' => TRUE, 'query' => 'Custom extensions for download tracking setting found. Update skipped!');
}
return $ret;
}