Index: googleanalytics.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.admin.inc,v retrieving revision 1.23 diff -u -r1.23 googleanalytics.admin.inc --- googleanalytics.admin.inc 1 Oct 2010 22:24:16 -0000 1.23 +++ googleanalytics.admin.inc 2 Jan 2011 14:07:15 -0000 @@ -186,7 +186,7 @@ $form['segmentation']['googleanalytics_segmentation'] = array( '#type' => 'select', - '#title' => t('Segment users based on these selected properties'), + '#title' => t('Segment users based on these selected properties') . ' ***DEPRECATED***', '#description' => t("You can supplement Google Analytics' basic IP address tracking of visitors by segmenting users based on these selected properties.") . (!$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('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')) .' '. t('You can select multiple values.'), '#default_value' => variable_get('googleanalytics_segmentation', array()), '#options' => $fields, @@ -194,6 +194,63 @@ '#multiple' => TRUE ); + + $token_enabled = module_exists('token'); + + $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', + ); + + $googleanalytics_custom_vars = variable_get('googleanalytics_custom_var', array()); + + // Google Analytics supports up to 5 custom variables. + for ($i = 1; $i < 6; $i++) { + $form['googleanalytics_custom_var']['slots'][$i]['slot'] = array( + '#default_value' => $i, + '#description' => t('Slot number'), + '#disabled' => TRUE, + '#size' => 1, + '#type' => 'textfield', + ); + $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.') : 'The custom variable value.'), + '#type' => 'textfield', + ); + $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( + '#access' => $token_enabled ? TRUE : FALSE, + '#theme' => 'token_tree', + '#token_types' => array('node'), + ); + + // Advanced feature configurations. $form['advanced'] = array( '#type' => 'fieldset', @@ -265,6 +322,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']); @@ -294,3 +367,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($variables) { + $form = $variables['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', array('header' => $header, 'rows' => $rows)); + $output .= drupal_render($form['googleanalytics_custom_var_description']); + + if (!empty($form['googleanalytics_custom_var_token_tree'])) { + $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]', + ':author:edit-url]', + ':author:url]', + ':author:path]', + ':mail]', + ':name]', + ':uid]', + 'user:edit-url]', + 'user:url]', + 'user:path]', + ); + + return preg_match('/' . implode('|', array_map('preg_quote', $token_blacklist)) . '/i', $token_string); +} Index: README.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/README.txt,v retrieving revision 1.9 diff -u -r1.9 README.txt --- README.txt 2 Jan 2011 00:59:21 -0000 1.9 +++ README.txt 2 Jan 2011 14:07:14 -0000 @@ -57,6 +57,20 @@ A code snippet that creates opt-out by role functionality for unchecked roles can be found in the Google Analytics handbook at http://drupal.org/node/261997. +Custom variables +================= +One example for custom variables tracking is the "User roles" tracking. Enter +the below configuration data into the custom variables settings form under +admin/config/system/googleanalytics. + +Slot: 1 +Name: User roles +Value: [current-user:roles] +Scope: Visitor + +More details about Custom variables can be found in the Google API documentation at +http://code.google.com/intl/en/apis/analytics/docs/tracking/gaTrackingCustomVariables.html + Advanced Settings ================= You can include additional JavaScript snippets in the custom javascript Index: googleanalytics.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.install,v retrieving revision 1.21 diff -u -r1.21 googleanalytics.install --- googleanalytics.install 9 Oct 2010 14:39:38 -0000 1.21 +++ googleanalytics.install 2 Jan 2011 14:07:15 -0000 @@ -31,6 +31,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'); @@ -322,3 +323,28 @@ return t('The default country in your regional settings is not Germany. The anonymizing of IP addresses setting has not been changed. Make sure your site settings comply with the local privacy rules.'); } } + +/** + * Upgrade "User roles" tracking to custom variables. + */ +function googleanalytics_update_7002() { + + // 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'] = '[current-user:roles]'; + $googleanalytics_custom_vars['slots'][1]['scope'] = 1; // Sets the scope to visitor-level. + + variable_set('googleanalytics_custom_var', $googleanalytics_custom_vars); + return t('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 { + return t('You need to upgrade the deprecated profile segmentation settings to custom variables manually or you may loose tracking data in future!'); + } +} Index: googleanalytics.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/google_analytics/googleanalytics.module,v retrieving revision 1.69 diff -u -r1.69 googleanalytics.module --- googleanalytics.module 25 Dec 2010 21:09:11 -0000 1.69 +++ googleanalytics.module 2 Jan 2011 14:07:16 -0000 @@ -19,6 +19,17 @@ } } +/** + * Implementation of hook_theme(). + */ +function googleanalytics_theme() { + return array( + 'googleanalytics_admin_custom_var_table' => array( + 'render element' => 'form', + ), + ); +} + function googleanalytics_permission() { return array( 'administer google analytics' => array( @@ -148,6 +159,40 @@ $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($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 = strlen(rawurlencode($custom_var_name)); + $tmp_value = rawurlencode($custom_var_value); + $value_length = 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_json_encode($custom_var_name); + $custom_var_value = drupal_json_encode($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_json_encode($id) . ']);'; @@ -162,6 +207,9 @@ if (!empty($codesnippet_before)) { $script .= $codesnippet_before; } + if (!empty($custom_var)) { + $script .= $custom_var; + } if (empty($url_custom)) { $script .= '_gaq.push(["_trackPageview"]);'; } Index: googleanalytics.tokens.inc =================================================================== RCS file: googleanalytics.tokens.inc diff -N googleanalytics.tokens.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ googleanalytics.tokens.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,45 @@ + t('User roles'), + 'description' => t('The roles the user account is a member of as comma separated list.'), + ); + + return array( + 'tokens' => array('user' => $user), + ); +} + +/** + * Implements hook_tokens(). + */ +function googleanalytics_tokens($type, $tokens, array $data = array(), array $options = array()) { + $sanitize = !empty($options['sanitize']); + + $replacements = array(); + + if ($type == 'user' && !empty($data['user'])) { + $account = $data['user']; + foreach ($tokens as $name => $original) { + switch ($name) { + // Basic user account information. + case 'roles': + $roles = implode(',', $account->roles); + $replacements[$original] = $sanitize ? check_plain($roles) : $roles; + break; + } + } + } + + return $replacements; +}