Commit 9cafb688 authored by hass's avatar hass
Browse files

#1595176: Allow tokens in custom variable names

parent 4f203dcc
......@@ -281,7 +281,7 @@ function googleanalytics_admin_settings_form($form_state) {
$form['googleanalytics_custom_var'] = array(
'#collapsed' => TRUE,
'#collapsible' => TRUE,
'#description' => t('You can add Google Analytics <a href="@custom_var_documentation">Custom Variables</a> here. These will be added to every page that Google Analytics tracking code appears on. Google Analytics will only accept custom variables if the <em>name</em> and <em>value</em> 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' => 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables')),
'#description' => t('You can add Google Analytics <a href="@custom_var_documentation">Custom Variables</a> here. These will be added to every page that Google Analytics tracking code appears on. Google Analytics will only accept custom variables if the <em>name</em> and <em>value</em> combined are less than 128 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 names and values. Global and user tokens are always available; on node pages, node tokens are also available.', array('@custom_var_documentation' => 'https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables')),
'#theme' => 'googleanalytics_admin_custom_var_table',
'#title' => t('Custom variables'),
'#tree' => TRUE,
......@@ -304,14 +304,17 @@ function googleanalytics_admin_settings_form($form_state) {
$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.'),
'#maxlength' => 255,
'#size' => 20,
'#title' => t('Custom variable name #@slot', array('@slot' => $i)),
'#title_display' => 'invisible',
'#type' => 'textfield',
'#element_validate' => array('googleanalytics_token_element_validate'),
'#token_types' => array('node'),
);
$form['googleanalytics_custom_var']['slots'][$i]['value'] = array(
'#default_value' => !empty($googleanalytics_custom_vars['slots'][$i]['value']) ? $googleanalytics_custom_vars['slots'][$i]['value'] : '',
'#description' => t('The custom variable value. You may use tokens in this field.'),
'#description' => t('The custom variable value.'),
'#maxlength' => 255,
'#title' => t('Custom variable value #@slot', array('@slot' => $i)),
'#title_display' => 'invisible',
......@@ -320,6 +323,7 @@ function googleanalytics_admin_settings_form($form_state) {
'#token_types' => array('node'),
);
if (module_exists('token')) {
$form['googleanalytics_custom_var']['slots'][$i]['name']['#element_validate'][] = 'token_element_validate';
$form['googleanalytics_custom_var']['slots'][$i]['value']['#element_validate'][] = 'token_element_validate';
}
$form['googleanalytics_custom_var']['slots'][$i]['scope'] = array(
......@@ -414,12 +418,15 @@ function googleanalytics_admin_settings_form($form_state) {
function googleanalytics_admin_settings_form_validate($form, &$form_state) {
// Custom variables validation.
foreach ($form_state['values']['googleanalytics_custom_var']['slots'] as $custom_var) {
$form_state['values']['googleanalytics_custom_var']['slots'][$custom_var['slot']]['name'] = trim($custom_var['name']);
$form_state['values']['googleanalytics_custom_var']['slots'][$custom_var['slot']]['value'] = trim($custom_var['value']);
// 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('The custom variable @slot-number requires a <em>Value</em> if a <em>Name</em> has been provided.', array('@slot-number' => $custom_var['slot'])));
form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][name", t('The custom variable @slot-number requires a <em>Name</em> if a <em>Value</em> has been provided.', array('@slot-number' => $custom_var['slot'])));
}
elseif (!empty($custom_var['name']) && empty($custom_var['value'])) {
form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", t('The custom variable @slot-number requires a <em>Name</em> if a <em>Value</em> has been provided.', array('@slot-number' => $custom_var['slot'])));
form_set_error("googleanalytics_custom_var][slots][" . $custom_var['slot'] . "][value", t('The custom variable @slot-number requires a <em>Value</em> if a <em>Name</em> has been provided.', array('@slot-number' => $custom_var['slot'])));
}
}
......
......@@ -193,10 +193,11 @@ function googleanalytics_page_alter(&$page) {
if (is_object($node)) {
$types += array('node' => $node);
}
$custom_var_name = token_replace($custom_var_name, $types, array('clear' => TRUE));
$custom_var_value = token_replace($custom_var_value, $types, array('clear' => TRUE));
// Suppress empty custom variables.
if (!is_numeric($custom_var_value) && empty($custom_var_value)) {
// Suppress empty custom names and/or variables.
if (!drupal_strlen(trim($custom_var_name)) || !drupal_strlen(trim($custom_var_value))) {
continue;
}
......
......@@ -160,7 +160,128 @@ class GoogleAnalyticsBasicTest extends DrupalWebTestCase {
$this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
$this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
}
}
class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Google Analytics Custom Variables tests'),
'description' => t('Test custom variables functionality of Google Analytics module.'),
'group' => 'Google Analytics',
);
}
function setUp() {
// @todo: Enabling token module fails. The reason maybe that GA does not
// require token module and therefore it's not set in the .info file.
// No idea how to fake this for the testing infrastucture.
//parent::setUp('googleanalytics', 'token');
parent::setUp('googleanalytics');
$permissions = array(
'access administration pages',
'administer google analytics',
);
// User to set up google_analytics.
$this->admin_user = $this->drupalCreateUser($permissions);
}
function testGoogleAnalyticsCustomVariables() {
$ua_code = 'UA-123456-3';
variable_set('googleanalytics_account', $ua_code);
// Basic test if the feature works.
$custom_vars = array(
'slots' => array(
1 => array(
'slot' => 1,
'name' => 'Foo 1',
'value' => 'Bar 1',
'scope' => 3,
),
2 => array(
'slot' => 2,
'name' => 'Foo 2',
'value' => 'Bar 2',
'scope' => 2,
),
3 => array(
'slot' => 3,
'name' => 'Foo 3',
'value' => 'Bar 3',
'scope' => 3,
),
4 => array(
'slot' => 4,
'name' => 'Foo 4',
'value' => 'Bar 4',
'scope' => 2,
),
5 => array(
'slot' => 5,
'name' => 'Foo 5',
'value' => 'Bar 5',
'scope' => 1,
),
)
);
variable_set('googleanalytics_custom_var', $custom_vars);
$this->drupalGet('');
foreach ($custom_vars['slots'] as $slot) {
$this->assertRaw("_gaq.push(['_setCustomVar', " . $slot['slot'] . ", \"" . $slot['name'] . "\", \"" . $slot['value'] . "\", " . $slot['scope'] . "]);", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' . $slot['slot'] . ' is shown.');
}
// Test whether tokens are replaced in custom variable names.
$site_slogan = $this->randomName(16);
variable_set('site_slogan', $site_slogan);
$custom_vars = array(
'slots' => array(
1 => array(
'slot' => 1,
'name' => 'Name: [site:slogan]',
'value' => 'Value: [site:slogan]',
'scope' => 3,
),
2 => array(
'slot' => 2,
'name' => '',
'value' => $this->randomName(16),
'scope' => 1,
),
3 => array(
'slot' => 3,
'name' => $this->randomName(16),
'value' => '',
'scope' => 2,
),
4 => array(
'slot' => 4,
'name' => '',
'value' => '',
'scope' => 3,
),
5 => array(
'slot' => 5,
'name' => '',
'value' => '',
'scope' => 3,
),
)
);
variable_set('googleanalytics_custom_var', $custom_vars);
$this->verbose('<pre>' . print_r($custom_vars, TRUE) . '</pre>');
$this->drupalGet('');
$this->assertRaw("_gaq.push(['_setCustomVar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
$this->assertNoRaw("_gaq.push(['_setCustomVar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
}
}
class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment