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