/[drupal]/contributions/modules/google_analytics/googleanalytics.admin.inc
ViewVC logotype

Contents of /contributions/modules/google_analytics/googleanalytics.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.13 - (show annotations) (download) (as text)
Sat Oct 17 14:02:51 2009 UTC (5 weeks, 5 days ago) by hass
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +0 -4 lines
File MIME type: text/x-php
* Add stream wrapper / public folder support
* Add 'file' back to menu after the roll back
1 <?php
2 // $Id: googleanalytics.admin.inc,v 1.11 2009/08/22 11:57:25 hass Exp $
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 '#collapsible' => FALSE,
17 );
18
19 $form['account']['googleanalytics_account'] = array(
20 '#type' => 'textfield',
21 '#title' => t('Google Analytics account number'),
22 '#default_value' => variable_get('googleanalytics_account', 'UA-'),
23 '#size' => 15,
24 '#maxlength' => 20,
25 '#required' => TRUE,
26 '#description' => t('The account number is unique to the websites domain. Click the <strong>Edit</strong> link in your Google Analytics account next to the appropriate profile on the <strong>Analytics Settings</strong> page, then select <strong>Check Status</strong> at the top-right of the table to find the account number (UA-xxxx-x) of your site. You can obtain a user account from the <a href="@url">Google Analytics</a> website.', array('@url' => 'http://www.google.com/analytics/')),
27 );
28
29 // Standard tracking configurations.
30 $form['user_vis_settings'] = array(
31 '#type' => 'fieldset',
32 '#title' => t('User specific tracking settings'),
33 '#collapsible' => TRUE,
34 );
35 $form['user_vis_settings']['googleanalytics_custom'] = array(
36 '#type' => 'radios',
37 '#title' => t('Custom tracking settings'),
38 '#options' => array(
39 t('Users cannot control whether they are tracked or not.'),
40 t('Track users by default, but let individual users to opt out.'),
41 t('Do not track users by default, but let individual users to opt in.')
42 ),
43 '#description' => t('Allow individual users to customize the visibility of tracking in their account settings. Only users with %permission permission are allowed to set their own preference.', array('%permission' => t('opt-in or out of tracking'))),
44 '#default_value' => variable_get('googleanalytics_custom', 0),
45 );
46
47 // Render the role overview.
48 $form['role_vis_settings'] = array(
49 '#type' => 'fieldset',
50 '#title' => t('Role specific tracking settings'),
51 '#collapsible' => TRUE,
52 );
53
54 $roles = user_roles();
55 $role_options = array();
56 foreach ($roles as $rid => $name) {
57 $role_options[$rid] = $name;
58 }
59 $form['role_vis_settings']['googleanalytics_roles'] = array(
60 '#type' => 'checkboxes',
61 '#title' => t('Add tracking for specific roles'),
62 '#default_value' => variable_get('googleanalytics_roles', array()),
63 '#options' => $role_options,
64 '#description' => t('Add tracking only for the selected role(s). If none of the roles are selected, all users will be tracked. If a user has any of the roles checked, that user will be tracked.'),
65 );
66
67 // Page specific visibility configurations.
68 $form['page_vis_settings'] = array(
69 '#type' => 'fieldset',
70 '#title' => t('Page specific tracking settings'),
71 '#collapsible' => TRUE,
72 '#collapsed' => TRUE,
73 );
74
75 $access = user_access('use PHP for tracking visibility');
76 $visibility = variable_get('googleanalytics_visibility', 0);
77 $pages = variable_get('googleanalytics_pages', '');
78
79 if ($visibility == 2 && !$access) {
80 $form['page_vis_settings'] = array();
81 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
82 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $pages);
83 }
84 else {
85 $options = array(t('Add to every page except the listed pages.'), t('Add to the listed pages only.'));
86 $description = t("Enter one page per line as Drupal paths. 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>'));
87
88 if ($access) {
89 $options[] = t('Add if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
90 $description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
91 }
92 $form['page_vis_settings']['googleanalytics_visibility'] = array(
93 '#type' => 'radios',
94 '#title' => t('Add tracking to specific pages'),
95 '#options' => $options,
96 '#default_value' => $visibility,
97 );
98 $form['page_vis_settings']['googleanalytics_pages'] = array(
99 '#type' => 'textarea',
100 '#title' => t('Pages'),
101 '#default_value' => $pages,
102 '#description' => $description,
103 '#wysiwyg' => FALSE,
104 );
105 }
106
107 $profile_enabled = module_exists('profile');
108 $form['segmentation'] = array(
109 '#type' => 'fieldset',
110 '#title' => t('User segmentation settings'),
111 '#collapsible' => TRUE,
112 );
113
114 // Compile a list of fields to show.
115 $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles')));
116 if ($profile_enabled) {
117 $result = db_query('SELECT name, title, type, weight FROM {profile_field} ORDER BY weight');
118 foreach ($result as $record) {
119 $fields[$record->name] = $record->title;
120 }
121 }
122
123 $form['segmentation']['googleanalytics_segmentation'] = array(
124 '#type' => 'select',
125 '#title' => t('Add segmentation information to tracking code'),
126 '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . (!$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('Make sure you will not associate (or permit any third party to associate) any data gathered from Your Website(s) (or such third parties\' website(s)) with any personally identifying information from any source as part of Your use (or such third parties\' use) of the Google Analytics service. For more information see section 8.1 in the <a href="@ga_tos">Google Analytics terms of use</a>.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('Selecting one or more values is supported. To select multiple items, hold down CTRL (PC) or &#8984; (Mac) while selecting fields.'),
127 '#default_value' => variable_get('googleanalytics_segmentation', ''),
128 '#options' => $fields,
129 '#size' => count($fields)>3 ? 10 : 3,
130 '#multiple' => TRUE
131 );
132
133 // Link specific configurations.
134 $form['linktracking'] = array(
135 '#type' => 'fieldset',
136 '#title' => t('Link tracking settings'),
137 '#collapsible' => TRUE,
138 '#collapsed' => FALSE,
139 );
140 $form['linktracking']['googleanalytics_trackoutgoing'] = array(
141 '#type' => 'checkbox',
142 '#title' => t('Track outgoing links'),
143 '#default_value' => variable_get('googleanalytics_trackoutgoing', 1),
144 '#description' => t('Enables tracking of clicks on outgoing links.')
145 );
146 $form['linktracking']['googleanalytics_trackmailto'] = array(
147 '#type' => 'checkbox',
148 '#title' => t('Track mailto links'),
149 '#default_value' => variable_get('googleanalytics_trackmailto', 1),
150 '#description' => t('Enables tracking of clicks on mailto links.')
151 );
152 $form['linktracking']['googleanalytics_trackfiles'] = array(
153 '#type' => 'checkbox',
154 '#title' => t('Track download links'),
155 '#default_value' => variable_get('googleanalytics_trackfiles', 1),
156 '#description' => t('Enables tracking of clicks on links to files based on the file extensions list below.')
157 );
158 $form['linktracking']['googleanalytics_trackfiles_extensions'] = array(
159 '#type' => 'textfield',
160 '#title' => t('File extensions to track'),
161 '#default_value' => variable_get('googleanalytics_trackfiles_extensions', GA_TRACKFILES_EXTENSIONS),
162 '#description' => t('A pipe separated list of file extensions that should be tracked when clicked with regular expression support. Example: !extensions', array('!extensions' => GA_TRACKFILES_EXTENSIONS)),
163 '#maxlength' => 255,
164 );
165
166 // Advanced feature configurations.
167 $form['advanced'] = array(
168 '#type' => 'fieldset',
169 '#title' => t('Advanced settings'),
170 '#collapsible' => TRUE,
171 '#collapsed' => TRUE,
172 );
173
174 $form['advanced']['googleanalytics_cache'] = array(
175 '#type' => 'checkbox',
176 '#title' => t('Cache tracking code file locally'),
177 '#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 your tracker!"),
178 '#default_value' => variable_get('googleanalytics_cache', 0),
179 );
180
181 // Allow for tracking of the originating node when viewing translation sets.
182 if (module_exists('translation')) {
183 $form['advanced']['googleanalytics_translation_set'] = array(
184 '#type' => 'checkbox',
185 '#title' => t('Track translation sets as one unit'),
186 '#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.'),
187 '#default_value' => variable_get('googleanalytics_translation_set', 0),
188 );
189 }
190
191 $site_search_dependencies = '<div class="admin-dependencies">';
192 $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')))));
193 $site_search_dependencies .= '</div>';
194
195 // Google already have many translations, if not - they display a note to change the language.
196 global $language;
197 $form['advanced']['googleanalytics_site_search'] = array(
198 '#type' => 'checkbox',
199 '#title' => t('Track internal search'),
200 '#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,
201 '#default_value' => variable_get('googleanalytics_site_search', FALSE),
202 '#disabled' => (module_exists('search') ? FALSE : TRUE),
203 );
204
205 $form['advanced']['googleanalytics_trackadsense'] = array(
206 '#type' => 'checkbox',
207 '#title' => t('Track AdSense ads'),
208 '#description' => t('If checked, your AdSense ads will be tracked in your Google Analytics account.'),
209 '#default_value' => variable_get('googleanalytics_trackadsense', FALSE),
210 );
211
212 $form['advanced']['codesnippet'] = array(
213 '#type' => 'fieldset',
214 '#title' => t('Custom JavaScript code'),
215 '#collapsible' => TRUE,
216 '#collapsed' => TRUE,
217 '#description' => t('You can add custom Google Analytics <a href="@snippets">code snippets</a> here. These will be added to every page that Google Analytics appears on. Before you add custom code to the below textarea\'s you should read <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'))
218 );
219 $form['advanced']['codesnippet']['googleanalytics_codesnippet_before'] = array(
220 '#type' => 'textarea',
221 '#title' => t('Code snippet (before)'),
222 '#default_value' => variable_get('googleanalytics_codesnippet_before', ''),
223 '#rows' => 5,
224 '#wysiwyg' => FALSE,
225 '#description' => t('Code in this textarea will be added <strong>before</strong> pageTracker._trackPageview().')
226 );
227 $form['advanced']['codesnippet']['googleanalytics_codesnippet_after'] = array(
228 '#type' => 'textarea',
229 '#title' => t('Code snippet (after)'),
230 '#default_value' => variable_get('googleanalytics_codesnippet_after', ''),
231 '#rows' => 5,
232 '#wysiwyg' => FALSE,
233 '#description' => t("Code in this textarea will be added <strong>after</strong> pageTracker._trackPageview(). This is useful if you'd like to track a site in two accounts.")
234 );
235
236 $form['advanced']['googleanalytics_js_scope'] = array(
237 '#type' => 'select',
238 '#title' => t('JavaScript scope'),
239 '#description' => t("<strong>Warning:</strong> Google recommends adding the external JavaScript files to footer for performance reasons."),
240 '#options' => array(
241 'footer' => t('Footer'),
242 'header' => t('Header'),
243 ),
244 '#default_value' => variable_get('googleanalytics_js_scope', 'footer'),
245 );
246
247 return system_settings_form($form);
248 }
249
250 function googleanalytics_admin_settings_form_validate($form, &$form_state) {
251 if (!preg_match('/^UA-\d{4,}-\d+$/', $form_state['values']['googleanalytics_account'])) {
252 form_set_error('googleanalytics_account', t('A valid Google Analytics account number is case sensitive and formatted like UA-xxxxxx-x.'));
253 }
254
255 // This is for the Newbie's who cannot read a text area description.
256 if (stristr($form_state['values']['googleanalytics_codesnippet_before'], 'google-analytics.com/ga.js')) {
257 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.'));
258 }
259 if (stristr($form_state['values']['googleanalytics_codesnippet_after'], 'google-analytics.com/ga.js')) {
260 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.'));
261 }
262 if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_before'])) {
263 form_set_error('googleanalytics_codesnippet_before', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
264 }
265 if (preg_match('/(.*)<\/?script(.*)>(.*)/i', $form_state['values']['googleanalytics_codesnippet_after'])) {
266 form_set_error('googleanalytics_codesnippet_after', t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
267 }
268
269 // Trim some text area values.
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 }

  ViewVC Help
Powered by ViewVC 1.1.2