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 00:24:38 -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 appears on. Google Analytics will only accept custom variables if the name and value combined are less than 64 bytes after URL encoding. Translation: keep them short, 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']['custom_var'][$i]['slot'] = array(
+ '#default_value' => $i,
+ '#description' => t('Slot number'),
+ '#disabled' => TRUE,
+ '#size' => 1,
+ '#type' => 'textfield',
+ );
+ $form['googleanalytics_custom_var']['custom_var'][$i]['name'] = array(
+ '#default_value' => !empty($googleanalytics_custom_vars['custom_var'][$i]['name']) ? $googleanalytics_custom_vars['custom_var'][$i]['name'] : '',
+ '#description' => t('The custom variable name.'),
+ '#size' => 20,
+ '#type' => 'textfield',
+ );
+ $form['googleanalytics_custom_var']['custom_var'][$i]['value'] = array(
+ '#default_value' => !empty($googleanalytics_custom_vars['custom_var'][$i]['value']) ? $googleanalytics_custom_vars['custom_var'][$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']['custom_var'][$i]['scope'] = array(
+ '#default_value' => !empty($googleanalytics_custom_vars['custom_var'][$i]['scope']) ? $googleanalytics_custom_vars['custom_var'][$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', 'comment'),
+ );
+
+
// 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']['custom_var'] as $custom_var) {
+ // Validate empty names/values.
+ if (empty($custom_var['name']) && !empty($custom_var['value'])) {
+ form_set_error("googleanalytics_custom_var][custom_var][" . $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][custom_var][" . $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][custom_var][" . $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['custom_var']) as $key => $id) {
+ $rows[] = array(
+ 'data' => array(
+ drupal_render($form['custom_var'][$id]['slot']),
+ drupal_render($form['custom_var'][$id]['name']),
+ drupal_render($form['custom_var'][$id]['value']),
+ drupal_render($form['custom_var'][$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: 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 00:24:38 -0000
@@ -31,6 +31,7 @@
function googleanalytics_uninstall() {
variable_del('googleanalytics_account');
+ variable_del('googleanalytics_custom_var');
variable_del('googleanalytics_codesnippet_before');
variable_del('googleanalytics_codesnippet_after');
variable_del('googleanalytics_segmentation');
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 00:24:38 -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['custom_var'][$i]['name']) ? $googleanalytics_custom_vars['custom_var'][$i]['name'] : '';
+ if (!empty($custom_var_name)) {
+ $custom_var_value = !empty($googleanalytics_custom_vars['custom_var'][$i]['value']) ? $googleanalytics_custom_vars['custom_var'][$i]['value'] : '';
+ $custom_var_scope = !empty($googleanalytics_custom_vars['custom_var'][$i]['scope']) ? $googleanalytics_custom_vars['custom_var'][$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"]);';
}