Index: googleanalytics.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v retrieving revision 1.13.2.9 diff -u -r1.13.2.9 googleanalytics.admin.inc --- googleanalytics.admin.inc 1 Oct 2010 22:19:01 -0000 1.13.2.9 +++ googleanalytics.admin.inc 9 Jan 2011 02:01:05 -0000 @@ -104,32 +104,6 @@ ); } - $profile_enabled = module_exists('profile'); - $form['segmentation'] = array( - '#type' => 'fieldset', - '#title' => t('User segmentation settings'), - '#collapsible' => TRUE, - ); - - // Compile a list of fields to show. - $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles'))); - if ($profile_enabled) { - $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY weight'); - while ($record = db_fetch_object($result)) { - $fields[$record->name] = $record->title; - } - } - - $form['segmentation']['googleanalytics_segmentation'] = array( - '#type' => 'select', - '#title' => t('Add segmentation information to tracking code'), - '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . (!$profile_enabled ? ' '. t('Enable the profile module 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 Google Analytics terms of use.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('You can select multiple values.'), - '#default_value' => variable_get('googleanalytics_segmentation', array()), - '#options' => $fields, - '#size' => count($fields)>3 ? 10 : 3, - '#multiple' => TRUE - ); - // Link specific configurations. $form['linktracking'] = array( '#type' => 'fieldset', @@ -163,6 +137,96 @@ '#maxlength' => 255, ); + // Backward compatibility only. + // TODO: If currently not in use, hide the UI and later remove the code. + $segmentation = variable_get('googleanalytics_segmentation', array()); + + if (!empty($segmentation) || variable_get('googleanalytics_segmentation_DEPRECATED', FALSE)) { + $profile_enabled = module_exists('profile'); + $form['segmentation'] = array( + '#type' => 'fieldset', + '#title' => t('User segmentation settings'), + '#collapsible' => TRUE, + ); + + // Compile a list of fields to show. + $fields = variable_get('googleanalytics_segmentation_default_fields', array('roles' => t('User roles'))); + if ($profile_enabled) { + $result = db_query('SELECT name, title FROM {profile_fields} ORDER BY weight'); + while ($record = db_fetch_object($result)) { + $fields[$record->name] = $record->title; + } + } + + $form['segmentation']['googleanalytics_segmentation'] = array( + '#type' => 'select', + '#title' => t('Add segmentation information to tracking code') . ' ***DEPRECATED***', + '#description' => t('Segment users based on different properties, additionally to the basic IP address based tracking provided by Google Analytics.') . (!$profile_enabled ? ' '. t('Enable the profile module 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 Google Analytics terms of use.', array('@ga_tos' => 'http://www.google.com/analytics/en-GB/tos.html')) .' '. t('You can select multiple values.'), + '#default_value' => $segmentation, + '#options' => $fields, + '#size' => count($fields)>3 ? 10 : 3, + '#multiple' => TRUE + ); + } + + $form['googleanalytics_custom_var'] = array( + '#collapsible' => TRUE, + '#description' => t('You can add Google Analytics Custom Variables here. These will be added to every page that Google Analytics tracking code appears on. Google Analytics will only accept custom variables if the name and value combined are less than 64 bytes after URL encoding. Keep the names as short as possible and expect long values to get trimmed. You may use tokens in custom variable values. Global and user tokens are always available; on node pages, node tokens are also available.', array('!custom_var_documentation' => 'http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html')), + '#theme' => 'googleanalytics_admin_custom_var_table', + '#title' => t('Custom variables'), + '#tree' => TRUE, + '#type' => 'fieldset', + ); + + $token_enabled = module_exists('token'); + $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array()); + + // Google Analytics supports up to 5 custom variables. + for ($i = 1; $i < 6; $i++) { + // TODO: '#default_value' is currently broken, see http://drupal.org/node/410926. + $form['googleanalytics_custom_var']['slots'][$i]['slot'] = array( + //'#attributes' => array('readonly' => 'readonly'), + //'#default_value' => $i, + '#description' => t('Slot number'), + '#disabled' => TRUE, + '#size' => 1, + '#type' => 'textfield', + '#value' => $i, + ); + $form['googleanalytics_custom_var']['slots'][$i]['name'] = array( + '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['name']) ? $googleanalytics_custom_vars['slots'][$i]['name'] : '', + '#description' => t('The custom variable name.'), + '#size' => 20, + '#type' => 'textfield', + ); + $form['googleanalytics_custom_var']['slots'][$i]['value'] = array( + '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '', + '#description' => ($token_enabled ? t('The custom variable value. You may use tokens in this field.') : t('The custom variable value.')), + '#type' => 'textfield', + '#element_validate' => array('token_element_validate_token_context'), + '#token_types' => array('all'), + ); + $form['googleanalytics_custom_var']['slots'][$i]['scope'] = array( + '#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['scope']) ? $googleanalytics_custom_vars['slots'][$i]['scope'] : 3, + '#description' => t('The scope for the custom variable.'), + '#type' => 'select', + '#options' => array( + 1 => t('Visitor'), + 2 => t('Session'), + 3 => t('Page'), + ), + ); + } + + $form['googleanalytics_custom_var']['googleanalytics_custom_var_description'] = array( + '#type' => 'item', + '#description' => t('You can supplement Google Analytics\' basic IP address tracking of visitors by segmenting users based on these selected properties.') . (!$token_enabled ? ' '. t('Enable the token module to be able to use tokens for more granular tracking.', array('@module_list' => url('admin/build/modules'))) : '') .' '. t('Section 8.1 of the Google Analytics terms of use 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')), + ); + $form['googleanalytics_custom_var']['googleanalytics_custom_var_token_tree'] = array( + '#theme' => 'token_tree', + '#token_types' => array('all'), + ); + // Advanced feature configurations. $form['advanced'] = array( '#type' => 'fieldset', @@ -260,6 +324,22 @@ } function googleanalytics_admin_settings_form_validate($form, &$form_state) { + // Custom variables validation. + foreach ($form_state['values']['googleanalytics_custom_var']['slots'] as $custom_var) { + // Validate empty names/values. + if (empty($custom_var['name']) && !empty($custom_var['value'])) { + form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][name", t('Custom variable #@slot: Name is required, if value has been provided.', array('@slot' => $custom_var['slot']))); + } + elseif (!empty($custom_var['name']) && empty($custom_var['value'])) { + form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", t('Custom variable #@slot: Value is required, if name has been provided.', array('@slot' => $custom_var['slot']))); + } + + // Block tokens with personally identifying information. + if (_googleanalytics_contains_forbidden_token($custom_var['value'])) { + form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", t('Custom variable #@slot: Forbidden token has been found in custom variable named %name. Never use tokens with personally identifying information!', array('%name' => $custom_var['name'], '@slot' => $custom_var['slot']))); + } + } + // Trim some text values. $form_state['values']['googleanalytics_account'] = trim($form_state['values']['googleanalytics_account']); $form_state['values']['googleanalytics_pages'] = trim($form_state['values']['googleanalytics_pages']); @@ -289,3 +369,74 @@ form_set_error('googleanalytics_codesnippet_after', t('Do not include the <script> tags in the javascript code snippets.')); } } + +/** + * Layout for the custom variables table in the admin settings form. + */ +function theme_googleanalytics_admin_custom_var_table($form) { + + $header = array( + array('data' => t('Slot')), + array('data' => t('Name')), + array('data' => t('Value')), + array('data' => t('Scope')), + ); + + $rows = array(); + foreach (element_children($form['slots']) as $key => $id) { + $rows[] = array( + 'data' => array( + drupal_render($form['slots'][$id]['slot']), + drupal_render($form['slots'][$id]['name']), + drupal_render($form['slots'][$id]['value']), + drupal_render($form['slots'][$id]['scope']), + ), + ); + } + + $output = theme('table', $header, $rows); + $output .= drupal_render($form['googleanalytics_custom_var_description']); + $output .= drupal_render($form['googleanalytics_custom_var_token_tree']); + + return $output; +} + +/** + * Validate if a string contains forbidden tokens not allowed by privacy rules. + * + * @param $token_string + * A string with one or more tokens to be validated. + * @return boolean + * TRUE if blacklisted token has been found, otherwise FALSE. + */ +function _googleanalytics_contains_forbidden_token($token_string) { + // List of strings in tokens with personal identifying information not allowed + // for privacy reasons. See section 8.1 of the Google Analytics terms of use + // for more detailed information. + // + // This list can never ever be complete. For this reason it tries to use a + // regex and may kill a few other valid tokens, but it's the only way to + // protect users as much as possible from admins with illegal ideas. + // + // User tokens are not prefixed with colon to catch 'current-user' and 'user'. + // + // TODO: If someone have better ideas, share them, please! + $token_blacklist = array( + 'author-uid]', + 'author-name', + 'author-mail', + 'author-homepage]', + '[user-name]', + '[user-id]', + '[user-mail]', + // [user] tokens + '[user]', + '[user-raw]', + '[uid]', + '[mail]', + '[account-url]', + '[account-edit]', + ); + + return preg_match('/' . implode('|', array_map('preg_quote', $token_blacklist)) . '/i', $token_string); +} Index: googleanalytics.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v retrieving revision 1.13.2.7 diff -u -r1.13.2.7 googleanalytics.install --- googleanalytics.install 6 Oct 2010 22:11:09 -0000 1.13.2.7 +++ googleanalytics.install 9 Jan 2011 02:01:06 -0000 @@ -22,6 +22,7 @@ function googleanalytics_uninstall() { variable_del('googleanalytics_account'); + variable_del('googleanalytics_custom_vars'); variable_del('googleanalytics_codesnippet_before'); variable_del('googleanalytics_codesnippet_after'); variable_del('googleanalytics_segmentation'); @@ -333,3 +334,31 @@ return $ret; } + +/** + * Upgrade "User roles" tracking to custom variables. + */ +function googleanalytics_update_6301() { + $ret = array(); + + // Read previous segmentation settings. + $segmentation = variable_get('googleanalytics_segmentation', array()); + + if (in_array('roles', $segmentation)) { + // Upgrade previous segmentation settings to new custom variables settings. + $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array()); + + $googleanalytics_custom_vars['slots'][1]['slot'] = 1; + $googleanalytics_custom_vars['slots'][1]['name'] = 'User roles'; + $googleanalytics_custom_vars['slots'][1]['value'] = '[roles]'; + $googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level. + + variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars); + $ret[] = array('success' => TRUE, 'query' => 'The deprecated profile segmentation setting for "User roles" has been upgraded to custom variables. You need to upgrade other profile fields manually or you may loose tracking data in future!'); + } + else { + $ret[] = array('success' => TRUE, 'query' => 'You need to upgrade the deprecated profile segmentation settings to custom variables manually or you may loose tracking data in future!'); + } + + return $ret; +} Index: googleanalytics.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v retrieving revision 1.36.2.23 diff -u -r1.36.2.23 googleanalytics.module --- googleanalytics.module 25 Dec 2010 20:25:24 -0000 1.36.2.23 +++ googleanalytics.module 9 Jan 2011 02:01:06 -0000 @@ -19,6 +19,20 @@ } } +/** + * Implementation of hook_theme(). + * + * @see drupal_common_theme(), common.inc + * @see template_preprocess_page(), theme.inc + */ +function googleanalytics_theme() { + return array( + 'googleanalytics_admin_custom_var_table' => array( + 'arguments' => array('form' => NULL), + ), + ); +} + function googleanalytics_perm() { return array('administer google analytics', 'opt-in or out of tracking', 'use PHP for tracking visibility'); } @@ -138,6 +152,42 @@ $codesnippet_before = variable_get('googleanalytics_codesnippet_before', ''); $codesnippet_after = variable_get('googleanalytics_codesnippet_after', ''); + // Add custom variables. + $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array()); + $custom_var = ''; + for ($i = 1; $i < 6; $i++) { + $custom_var_name = !empty($googleanalytics_custom_vars['slots'][$i]['name']) ? $googleanalytics_custom_vars['slots'][$i]['name'] : ''; + if (!empty($custom_var_name)) { + $custom_var_value = !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : ''; + $custom_var_scope = !empty($googleanalytics_custom_vars['slots'][$i]['scope']) ? $googleanalytics_custom_vars['slots'][$i]['scope'] : 3; + + if (module_exists('token')) { + $node = menu_get_object(); + $custom_var_value = token_replace_multiple($custom_var_value, array('global' => NULL, 'user' => $user, 'node' => $node), '[', ']', array('clear' => TRUE)); + } + + // Suppress empty custom variables. + if (!is_numeric($custom_var_value) && empty($custom_var_value)) { + continue; + } + + // The length of the string used for the 'name' and the length of the + // string used for the 'value' must not exceed 64 bytes after url encoding. + $name_length = drupal_strlen(rawurlencode($custom_var_name)); + $tmp_value = rawurlencode($custom_var_value); + $value_length = drupal_strlen($tmp_value); + if ($name_length + $value_length > 64) { + // Trim value and remove fragments of url encoding. + $tmp_value = rtrim(substr($tmp_value, 0, 63 - $name_length), '%0..9A..F'); + $custom_var_value = urldecode($tmp_value); + } + + $custom_var_name = drupal_to_js($custom_var_name); + $custom_var_value = drupal_to_js($custom_var_value); + $custom_var .= "_gaq.push(['_setCustomVar', $i, $custom_var_name, $custom_var_value, $custom_var_scope]);"; + } + } + // Build tracker code. $script = 'var _gaq = _gaq || [];'; $script .= '_gaq.push(["_setAccount", ' . drupal_to_js($id) . ']);'; @@ -152,6 +202,9 @@ if (!empty($codesnippet_before)) { $script .= $codesnippet_before; } + if (!empty($custom_var)) { + $script .= $custom_var; + } if (empty($url_custom)) { $script .= '_gaq.push(["_trackPageview"]);'; } @@ -408,3 +461,24 @@ } return $page_match; } + +/** + * Implementation of hook_token_values(). + */ +function googleanalytics_token_values($type, $object = NULL, $options = array()) { + if ($type == 'user') { + $user = $object; + $tokens['roles'] = implode(',', $user->roles); + return $tokens; + } +} + +/** + * Implementation of hook_token_list(). + */ +function googleanalytics_token_list($type = 'all') { + if ($type == 'user' || $type == 'all') { + $tokens['user']['roles'] = t("User's roles as comma separated list."); + return $tokens; + } +}