Update hooks now return strings or throw exceptions, and update_sql() is no more
[project/google_analytics.git] / googleanalytics.install
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Installation file for Google Analytics module.
7 */
8
9 function 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
23 function googleanalytics_uninstall() {
24 variable_del('googleanalytics_account');
25 variable_del('googleanalytics_codesnippet_before');
26 variable_del('googleanalytics_codesnippet_after');
27 variable_del('googleanalytics_segmentation');
28 variable_del('googleanalytics_trackoutgoing');
29 variable_del('googleanalytics_trackmailto');
30 variable_del('googleanalytics_trackfiles');
31 variable_del('googleanalytics_trackfiles_extensions');
32 variable_del('googleanalytics_cache');
33 variable_del('googleanalytics_last_cache');
34 variable_del('googleanalytics_site_search');
35 variable_del('googleanalytics_trackadsense');
36 variable_del('googleanalytics_js_scope');
37 variable_del('googleanalytics_custom');
38 variable_del('googleanalytics_roles');
39 variable_del('googleanalytics_visibility');
40 variable_del('googleanalytics_pages');
41 variable_del('googleanalytics_translation_set');
42 }
43
44 /**
45 * Remove cache directory if module is disabled (or uninstalled).
46 */
47 function googleanalytics_disable() {
48 $path = 'public://googleanalytics';
49 if (file_exists($path)) {
50 file_unmanaged_delete($path . '/ga.js');
51 rmdir($path);
52 }
53 }
54
55 /**
56 * Implementation of hook_requirements().
57 */
58 function googleanalytics_requirements($phase) {
59 $requirements = array();
60
61 if ($phase == 'runtime') {
62 // Raise warning if Google user account has not been set yet.
63 if (!preg_match('/^UA-\d{4,}-\d+$/', variable_get('googleanalytics_account', 'UA-'))) {
64 $requirements['googleanalytics'] = array(
65 'title' => t('Google Analytics module'),
66 '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'))),
67 'severity' => REQUIREMENT_ERROR,
68 'value' => t('Not configured'),
69 );
70 }
71 }
72
73 return $requirements;
74 }
75
76 /**
77 * Upgrade old extension variable to new and use old name as enabled/disabled flag.
78 */
79 function googleanalytics_update_6000() {
80 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'));
81 $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;
82 variable_set('googleanalytics_trackfiles', $trackfiles);
83
84 return t('Updated download tracking file extensions.');
85 }
86
87 function googleanalytics_update_6001() {
88 variable_set('googleanalytics_visibility', 0);
89
90 // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
91 $pages = array(
92 'admin*',
93 'user*',
94 'node/add*',
95 'node/*/*',
96 );
97 variable_set('googleanalytics_pages', implode("\n", $pages));
98
99 return t('Added page tracking to every page except the listed pages: @pages.', array('@pages' => implode(', ', $pages)));
100 }
101
102 /**
103 * Upgrade role settings and per user tracking settings
104 * of "User 1" and remove outdated tracking variables.
105 */
106 function googleanalytics_update_6002() {
107 // Upgrade enabled/disabled roles to new logic (correct for upgrades from 5.x-1.4 and 6.x-1.0).
108 $roles = array();
109 $messages = array();
110 foreach (user_roles() as $rid => $name) {
111 if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
112 // Role ID is activated for user tracking.
113 $roles[$rid] = $rid;
114 $messages[] = t('Enabled page tracking for role: @name.', array('@name' => $name));
115 }
116 else {
117 $messages[] = t('Disabled page tracking for role: @name.', array('@name' => $name));
118 }
119 }
120 variable_set('googleanalytics_roles', $roles);
121
122 // Upgrade disabled tracking of "user 1" to new logic.
123 if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
124 variable_set('googleanalytics_custom', 1);
125
126 // Load user 1 object, set appropiate value and save new user settings back.
127 $account = user_load(array('uid' => 1));
128 $account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
129 $messages[] = t('Disabled user specific page tracking for site administrator.');
130 }
131
132 // Delete outdated tracking settings.
133 db_delete('variable')
134 ->condition('name', db_like('googleanalytics_track_') .'%', 'LIKE')
135 ->execute();
136
137 return implode(', ', $messages);
138 }
139
140 /**
141 * #262468: Clear menu cache to solve stale menu data in 5.x-1.5 and 6.x-1.1
142 */
143 function googleanalytics_update_6003() {
144 menu_rebuild();
145 return t('Menu has been rebuild.');
146 }
147
148 /**
149 * Change visibility setting for path "user/*".
150 */
151 function googleanalytics_update_6004() {
152 // Original pages setting.
153 $pages = array(
154 'admin*',
155 'user*',
156 'node/add*',
157 'node/*/*',
158 );
159
160 $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
161 if (empty($diff)) {
162 // No diff to original settings found. Update with new settings.
163 $pages = array(
164 'admin*',
165 'user/*/*',
166 'node/add*',
167 'node/*/*',
168 );
169 variable_set('googleanalytics_pages', implode("\n", $pages));
170 return t('Path visibility filter setting changed from "user*" to "user/*/*".');
171 }
172 else {
173 return t('Custom path visibility filter setting found. Update skipped!');
174 }
175 }
176
177 /**
178 * Change visibility setting for path "admin*".
179 */
180 function googleanalytics_update_6005() {
181 // Original pages setting.
182 $pages = array(
183 'admin*',
184 'user/*/*',
185 'node/add*',
186 'node/*/*',
187 );
188
189 $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
190 if (empty($diff)) {
191 // No diff to original settings found. Update with new settings.
192 $pages = array(
193 'admin',
194 'admin/*',
195 'user/*/*',
196 'node/add*',
197 'node/*/*',
198 );
199 variable_set('googleanalytics_pages', implode("\n", $pages));
200 return t('Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
201 }
202 else {
203 return t('Custom path visibility filter setting found. Update skipped!');
204 }
205 }
206
207 /**
208 * Upgrade custom javascript settings.
209 */
210 function googleanalytics_update_6006() {
211 variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
212 variable_del('googleanalytics_codesnippet');
213
214 return t('Upgraded custom javascript codesnippet setting.');
215 }
216
217 /**
218 * Remove "User identifier" and "User name" from segmentation fields.
219 *
220 * This is a data protection and privacy law change. For more information see Google Analytics
221 * terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
222 */
223 function googleanalytics_update_6007() {
224 $profile_fields = variable_get('googleanalytics_segmentation', array());
225 unset($profile_fields['uid']);
226 unset($profile_fields['name']);
227 variable_set('googleanalytics_segmentation', $profile_fields);
228
229 return t('Removed "User identifier" and "User name" from segmentation fields.');
230 }
231
232 /**
233 * Remove outdated legacy support variables and files.
234 */
235 function googleanalytics_update_6200() {
236 $path = 'public://googleanalytics';
237 if (file_exists($path)) {
238 file_unmanaged_delete($path .'/urchin.js');
239 }
240 variable_del('googleanalytics_legacy_version');
241
242 return t('Removed outdated legacy tracker stuff.');
243 }
244
245 /**
246 * Update list of default file extensions.
247 */
248 function googleanalytics_update_6201() {
249 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') {
250 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');
251 }
252
253 return t('The default extensions for download tracking have been updated.');
254 }
255
256 /**
257 * Update JavaScript scope to 'header'.
258 */
259 function googleanalytics_update_7000() {
260 variable_set('googleanalytics_js_scope', 'header');
261
262 return t('Google tracking code has been moved to header.');
263 }