#318974: Tracking Individual Users is against Google Analytics Policy
[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_legacy_version');
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_js_scope');
37 variable_del('googleanalytics_custom');
38 variable_del('googleanalytics_roles');
39 variable_del('googleanalytics_visibility');
40 variable_del('googleanalytics_pages');
41 }
42
43 /**
44 * Remove cache directory if module is disabled (or uninstalled).
45 */
46 function googleanalytics_disable() {
47 $path = file_directory_path() .'/googleanalytics';
48 if (file_exists($path)) {
49 file_delete($path .'/urchin.js');
50 file_delete($path .'/ga.js');
51 rmdir($path);
52 }
53 }
54
55
56 function googleanalytics_update_1() {
57 $ret = array();
58
59 $result = db_query("SELECT * FROM {role}");
60 while ($role = db_fetch_object($result)) {
61 // can't use empty spaces in varname
62 $role_varname = str_replace(' ', '_', $role->name);
63 variable_set('googleanalytics_track_'. $role->rid, !variable_get("googleanalytics_track_{$role_varname}", FALSE));
64 variable_del("googleanalytics_track_{$role_varname}");
65 }
66 variable_set('googleanalytics_track__user1', FALSE);
67
68 return $ret;
69 }
70
71 function googleanalytics_update_6000() {
72 $ret = array();
73
74 variable_set('googleanalytics_trackfiles_extensions', variable_get('googleanalytics_trackfiles', GA_TRACKFILES_EXTENSIONS));
75 $trackfiles = variable_get('googleanalytics_trackfiles', GA_TRACKFILES_EXTENSIONS) ? TRUE : FALSE;
76 variable_set('googleanalytics_trackfiles', $trackfiles);
77 $ret[] = array('success' => TRUE, 'query' => 'Updated download tracking settings.');
78
79 return $ret;
80 }
81
82 function googleanalytics_update_6001() {
83 $ret = array();
84
85 variable_set('googleanalytics_visibility', 0);
86
87 // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
88 $pages = array(
89 'admin*',
90 'user*',
91 'node/add*',
92 'node/*/*',
93 );
94 variable_set('googleanalytics_pages', implode("\n", $pages));
95 $ret[] = array('success' => TRUE, 'query' => 'Added page tracking to every page except the listed pages: '. implode(', ', $pages));
96
97 return $ret;
98 }
99
100 /**
101 * Upgrade role settings and per user tracking settings
102 * of "User 1" and remove outdated tracking variables.
103 */
104 function googleanalytics_update_6002() {
105 $ret = array();
106
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 foreach (user_roles() as $rid => $name) {
110 if (variable_get('googleanalytics_track_'. $rid, FALSE)) {
111 // Role ID is activated for user tracking.
112 $roles[$rid] = $rid;
113 $ret[] = array('success' => TRUE, 'query' => 'Enabled page tracking for role: '. $name);
114 }
115 else {
116 $ret[] = array('success' => TRUE, 'query' => 'Disabled page tracking for role: '. $name);
117 }
118 }
119 variable_set('googleanalytics_roles', $roles);
120
121 // Upgrade disabled tracking of "user 1" to new logic.
122 if (!$track_user1 = variable_get('googleanalytics_track__user1', 1)) {
123 variable_set('googleanalytics_custom', 1);
124
125 // Load user 1 object, set appropiate value and save new user settings back.
126 $account = user_load(array('uid' => 1));
127 $account = user_save($account, array('googleanalytics' => array('custom' => 0)), 'account');
128 $ret[] = array('success' => TRUE, 'query' => 'Disabled user specific page tracking for website administrator.');
129 }
130
131 // Delete outdated tracking settings.
132 $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'googleanalytics_track_%'");
133
134 return $ret;
135 }
136
137 /**
138 * #262468: Clear menu cache to solve stale menu data in 5.x-1.5 and 6.x-1.1
139 */
140 function googleanalytics_update_6003() {
141 $ret = array();
142 menu_rebuild();
143 return $ret;
144 }
145
146 /**
147 * Change visibility setting for path "user/*".
148 */
149 function googleanalytics_update_6004() {
150 $ret = array();
151
152 // Orginal 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 $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "user*" to "user/*/*".');
171 }
172 else {
173 $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
174 }
175
176 return $ret;
177 }
178
179 /**
180 * Change visibility setting for path "admin*".
181 */
182 function googleanalytics_update_6005() {
183 $ret = array();
184
185 // Orginal pages setting.
186 $pages = array(
187 'admin*',
188 'user/*/*',
189 'node/add*',
190 'node/*/*',
191 );
192
193 $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('googleanalytics_pages', implode("\n", $pages))));
194 if (empty($diff)) {
195 // No diff to original settings found. Update with new settings.
196 $pages = array(
197 'admin',
198 'admin/*',
199 'user/*/*',
200 'node/add*',
201 'node/*/*',
202 );
203 variable_set('googleanalytics_pages', implode("\n", $pages));
204 $ret[] = array('success' => TRUE, 'query' => 'Path visibility filter setting changed from "admin*" to "admin" and "admin/*".');
205 }
206 else {
207 $ret[] = array('success' => TRUE, 'query' => 'Custom path visibility filter setting found. Update skipped!');
208 }
209
210 return $ret;
211 }
212
213 /**
214 * Upgrade custom javascript settings.
215 */
216 function googleanalytics_update_6006() {
217 $ret = array();
218
219 variable_set('googleanalytics_codesnippet_before', variable_get('googleanalytics_codesnippet', ''));
220 variable_del('googleanalytics_codesnippet');
221 $ret[] = array('success' => TRUE, 'query' => 'Upgraded custom javascript codesnippet setting.');
222
223 return $ret;
224 }
225
226 /**
227 * Remove "User identifier" and "User name" from segmentation fields.
228 *
229 * This is a data protection and privacy law change. For more information see Google Analytics
230 * terms of use section 8.1 (http://www.google.com/analytics/en-GB/tos.html).
231 */
232 function googleanalytics_update_6007() {
233 $ret = array();
234
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 $ret[] = array('success' => TRUE, 'query' => 'Removed "User identifier" and "User name" from segmentation fields.');
240
241 return $ret;
242 }