#795294 by hass: Make account number validation more foolproof.
[project/google_analytics.git] / googleanalytics.admin.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Administrative page callbacks for the googleanalytics module.
7 */
8
9 /**
10 * Implementation of hook_admin_settings() for configuring the module
11 */
12 function googleanalytics_admin_settings_form($form_state) {
13 $form['account'] = array(
14 '#type' => 'fieldset',
15 '#title' => t('General settings'),
16 );
17
18 $form['account']['googleanalytics_account'] = array(
19 '#title' => t('Web Property ID'),
20 '#type' => 'textfield',
21 '#default_value' => variable_get('googleanalytics_account', 'UA-'),
22 '#size' => 15,
23 '#maxlength' => 20,
24 '#required' => TRUE,
25 '#description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href="@analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href="@webpropertyid">Find more information in the documentation</a>.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => 'http://code.google.com/apis/analytics/docs/concepts/gaConceptsAccounts.html#webProperty')),
26 );
27
28 // Visibility settings.
29 $form['tracking_title'] = array(
30 '#type' => 'item',
31 '#title' => t('Tracking scope'),
32 );
33 $form['tracking'] = array(
34 '#type' => 'vertical_tabs',
35 '#attached' => array(
36 'js' => array(drupal_get_path('module', 'googleanalytics') . '/googleanalytics.admin.js'),
37 ),
38 );
39
40 // Standard tracking configurations.
41 $form['tracking']['user_vis_settings'] = array(
42 '#type' => 'fieldset',
43 '#title' => t('Users'),
44 );
45 $t_permission = array('%permission' => t('opt-in or out of tracking'));
46 $form['tracking']['user_vis_settings']['googleanalytics_custom'] = array(
47 '#type' => 'radios',
48 '#title' => t('Allow users to customize tracking on their account page'),
49 '#options' => array(
50 t('No customization allowed'),
51 t('Tracking on by default, users with %permission permission can opt out', $t_permission),
52 t('Tracking off by default, users with %permission permission can opt in', $t_permission)
53 ),
54 '#default_value' => variable_get('googleanalytics_custom', 0),
55 );
56
57 // Render the role overview.
58 $form['tracking']['role_vis_settings'] = array(
59 '#type' => 'fieldset',
60 '#title' => t('Roles'),
61 );
62
63 $role_options = array_map('check_plain', user_roles());
64 $form['tracking']['role_vis_settings']['googleanalytics_roles'] = array(
65 '#type' => 'checkboxes',
66 '#title' => t('Add tracking for specific roles'),
67 '#default_value' => variable_get('googleanalytics_roles', array()),
68 '#options' => $role_options,
69 '#description' => t('If none are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked.'),
70 );
71
72 // Page specific visibility configurations.
73 $php_access = user_access('use PHP for tracking visibility');
74 $visibility = variable_get('googleanalytics_visibility', 0);
75 $pages = variable_get('googleanalytics_pages', '');
76
77 $form['tracking']['page_vis_settings'] = array(
78 '#type' => 'fieldset',
79 '#title' => t('Pages'),
80 '#collapsible' => TRUE,
81 '#collapsed' => TRUE,
82 );
83
84 if ($visibility == 2 && !$php_access) {
85 $form['tracking']['page_vis_settings'] = array();
86 $form['tracking']['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
87 $form['tracking']['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages);
88 }
89 else {
90 $options = array(
91 t('Every page except the listed pages'),
92 t('The listed pages only')
93 );
94 $description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
95
96 if (module_exists('php') && $php_access) {
97 $options[] = t('Pages on which this PHP code returns <code>TRUE</code> (experts only)');
98 $title = t('Pages or PHP code');
99 $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
100 }
101 else {
102 $title = t('Pages');
103 }
104 $form['tracking']['page_vis_settings']['googleanalytics_visibility'] = array(
105 '#type' => 'radios',
106 '#title' => t('Add tracking to specific pages'),
107 '#options' => $options,
108 '#default_value' => $visibility,
109 );
110 $form['tracking']['page_vis_settings']['googleanalytics_pages'] = array(
111 '#type' => 'textarea',
112 '#title' => '<span class="element-invisible">' . $title . '</span>',
113 '#default_value' => $pages,
114 '#description' => $description,
115 '#wysiwyg' => FALSE,
116 );
117 }
118
119 // Link specific configurations.
120 $form['tracking']['linktracking'] = array(
121 '#type' => 'fieldset',
122 '#title' => t('Links and downloads'),
123 );
124 $form['tracking']['linktracking']['googleanalytics_trackoutgoing'] = array(
125 '#type' => 'checkbox',
126 '#title' => t('Track clicks on outgoing links'),
127 '#default_value' => variable_get('googleanalytics_trackoutgoing', 1),
128 );
129 $form['tracking']['linktracking']['googleanalytics_trackmailto'] = array(
130 '#type' => 'checkbox',
131 '#title' => t('Track clicks on mailto links'),
132 '#default_value' => variable_get('googleanalytics_trackmailto', 1),
133 );
134 $form['tracking']['linktracking']['googleanalytics_trackfiles'] = array(
135 '#type' => 'checkbox',
136 '#title' => t('Track downloads (clicks on file links) for the following extensions'),
137 '#default_value' => variable_get('googleanalytics_trackfiles', 1),
138 );
139 $form['tracking']['linktracking']['googleanalytics_trackfiles_extensions'] = array(
140 '#type' => 'textfield',
141 '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS),
142 '#description' => t('A file extension list separated by the | character that will be tracked when clicked. Regular expressions are supported. For example: !extensions', array('!extensions' => GA_TRACKFILES_EXTENSIONS)),
143 '#maxlength' => 255,
144 );
145
146 // Google already have many translations, if not - they display a note to change the language.
147 global $language;
148 $form['tracking']['search_and_adsense'] = array(
149 '#type' => 'fieldset',
150 '#title' => t('Search and AdSense'),
151 );
152
153 $site_search_dependencies = '<div class="admin-dependencies">';
154 $site_search_dependencies .= t('Depends on: !dependencies', array('!dependencies' => (module_exists('search') ? t('@module (<span class="admin-enabled">enabled</span>)', array('@module' => 'Search')) : t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => 'Search')))));
155 $site_search_dependencies .= '</div>';
156
157 $form['tracking']['search_and_adsense']['googleanalytics_site_search'] = array(
158 '#type' => 'checkbox',
159 '#title' => t('Track internal search'),
160 '#description' => t('If checked, internal search keywords are tracked. You must configure your Google account to use the internal query parameter <strong>search</strong>. For more information see <a href="@url">How do I set up Site Search for my profile</a>.', array('@url' => 'http://www.google.com/support/analytics/bin/answer.py?hl='. $language->language .'&answer=75817')) . $site_search_dependencies,
161 '#default_value' => variable_get('googleanalytics_site_search', FALSE),
162 '#disabled' => (module_exists('search') ? FALSE : TRUE),
163 );
164
165 $form['tracking']['search_and_adsense']['googleanalytics_trackadsense'] = array(
166 '#type' => 'checkbox',
167 '#title' => t('Track AdSense ads'),
168 '#default_value' => variable_get('googleanalytics_trackadsense', FALSE),
169 );
170
171 $profile_enabled = module_exists('profile');
172 $form['segmentation'] = array(
173 '#type' => 'fieldset',
174 '#title' => t('Additional user information'),
175 '#collapsible' => TRUE,
176 );
177
178 // Compile a list of fields to show.
179 $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles')));
180 if ($profile_enabled) {
181 $result = db_query('SELECT name, title FROM {profile_field} ORDER BY weight');
182 foreach ($result as $record) {
183 $fields[$record->name] = $record->title;
184 }
185 }
186
187 $form['segmentation']['googleanalytics_segmentation'] = array(
188 '#type' => 'select',
189 '#title' => t('Segment users based on these selected properties'),
190 '#description' => t("You can supplement Google Analytics' basic IP address tracking of visitors by segmenting users based on these selected properties.") . (!$profile_enabled ? ' '. t('<a href="@module_list">Enable the profile module</a> to be able to use profile fields for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Section 8.1 of the <a href="@ga_tos">Google Analytics terms of use</a> requires you to make sure you will not associate (or permit any third party to associate) any data gathered from your websites (or such third parties\' websites) with any personally identifying information from any source as part of your use (or such third parties\' use) of the Google Analytics\' service.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('You can select multiple values.'),
191 '#default_value' => variable_get('googleanalytics_segmentation', array()),
192 '#options' => $fields,
193 '#size' => count($fields)>3 ? 10 : 3,
194 '#multiple' => TRUE
195 );
196
197 // Advanced feature configurations.
198 $form['advanced'] = array(
199 '#type' => 'fieldset',
200 '#title' => t('Advanced settings'),
201 '#collapsible' => TRUE,
202 '#collapsed' => TRUE,
203 );
204
205 $form['advanced']['googleanalytics_tracker_anonymizeip'] = array(
206 '#type' => 'checkbox',
207 '#title' => t('Anonymize visitors IP address'),
208 '#description' => t('Tell Google Analytics to anonymize the information sent by the tracker objects by removing the last octet of the IP address prior to its storage. Note that this will slightly reduce the accuracy of geographic reporting. In some countries it is not allowed to collect personally identifying information for privacy reasons and this setting may help you to comply with the local laws.'),
209 '#default_value' => variable_get('googleanalytics_tracker_anonymizeip', 0),
210 );
211
212 $form['advanced']['googleanalytics_cache'] = array(
213 '#type' => 'checkbox',
214 '#title' => t('Locally cache tracking code file'),
215 '#description' => t("If checked, the tracking code file is retrieved from Google Analytics and cached locally. It is updated daily from Google's servers to ensure updates to tracking code are reflected in the local copy. Do not activate this until after Google Analytics has confirmed that site tracking is working!"),
216 '#default_value' => variable_get('googleanalytics_cache', 0),
217 );
218
219 // Allow for tracking of the originating node when viewing translation sets.
220 if (module_exists('translation')) {
221 $form['advanced']['googleanalytics_translation_set'] = array(
222 '#type' => 'checkbox',
223 '#title' => t('Track translation sets as one unit'),
224 '#description' => t('When a node is part of a translation set, record statistics for the originating node instead. This allows for a translation set to be treated as a single unit.'),
225 '#default_value' => variable_get('googleanalytics_translation_set', 0),
226 );
227 }
228
229 $form['advanced']['codesnippet'] = array(
230 '#type' => 'fieldset',
231 '#title' => t('Custom JavaScript code'),
232 '#collapsible' => TRUE,
233 '#collapsed' => TRUE,
234 '#description' => t('You can add custom Google Analytics <a href="@snippets">code snippets</a> here. These will be added every time tracking is in effect. Before you add your custom code, you should read the <a href="@ga_concepts_overview">Google Analytics Tracking Code - Functional Overview</a> and the <a href="@ga_js_api">Google Analytics Tracking API</a> documentation. <strong>Do not include the &lt;script&gt; tags</strong>, and always end your code with a semicolon (;).', array('@snippets' => 'http://drupal.org/node/248699', '@ga_concepts_overview' => 'http://code.google.com/apis/analytics/docs/gaConceptsOverview.html', '@ga_js_api' => 'http://code.google.com/apis/analytics/docs/gaJSApi.html'))
235 );
236 $form['advanced']['codesnippet']['googleanalytics_codesnippet_before'] = array(
237 '#type' => 'textarea',
238 '#title' => t('Code snippet (before)'),
239 '#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
240 '#rows' => 5,
241 '#wysiwyg' => FALSE,
242 '#description' => t("Code in this textarea will be added <strong>before</strong> _gaq.push(['_trackPageview'])."),
243 );
244 $form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
245 '#type' => 'textarea',
246 '#title' => t('Code snippet (after)'),
247 '#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
248 '#rows' => 5,
249 '#wysiwyg' => FALSE,
250 '#description' => t("Code in this textarea will be added <strong>after</strong> _gaq.push(['_trackPageview']). This is useful if you'd like to track a site in two accounts."),
251 );
252
253 $form['advanced']['googleanalytics_js_scope'] = array(
254 '#type' => 'select',
255 '#title' => t('JavaScript scope'),
256 '#description' => t('Google recommends adding the external JavaScript files to the header for performance reasons.'),
257 '#options' => array(
258 'footer' => t('Footer'),
259 'header' => t('Header'),
260 ),
261 '#default_value' => variable_get('googleanalytics_js_scope', 'header'),
262 );
263
264 return system_settings_form($form);
265 }
266
267 function googleanalytics_admin_settings_form_validate($form, &$form_state) {
268 // Trim some text values.
269 $form_state['values']['googleanalytics_account'] = trim($form_state['values']['googleanalytics_account']);
270 $form_state['values']['googleanalytics_pages'] = trim($form_state['values']['googleanalytics_pages']);
271 $form_state['values']['googleanalytics_codesnippet_before'] = trim($form_state['values']['googleanalytics_codesnippet_before']);
272 $form_state['values']['googleanalytics_codesnippet_after'] = trim($form_state['values']['googleanalytics_codesnippet_after']);
273
274 if (!preg_match('/^UA-\d{4,}-\d+$/', $form_state['values']['googleanalytics_account'])) {
275 form_set_error('googleanalytics_account', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
276 }
277
278 // This is for the Newbie's who cannot read a text area description.
279 if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
280 form_set_error('googleanalytics_codesnippet_before', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
281 }
282 if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/ga.js')) {
283 form_set_error('googleanalytics_codesnippet_after', t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
284 }
285 if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_before'])) {
286 form_set_error('googleanalytics_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
287 }
288 if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_after'])) {
289 form_set_error('googleanalytics_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
290 }
291 }