Commit ee090c45 authored by hass's avatar hass
Browse files

#1300240: Add tracking for form error messages

parent 3c85b1c5
......@@ -202,6 +202,23 @@ function googleanalytics_admin_settings_form($form_state) {
'#maxlength' => 255,
);
// Message specific configurations.
$form['tracking']['messagetracking'] = array(
'#type' => 'fieldset',
'#title' => t('Messages'),
);
$form['tracking']['messagetracking']['googleanalytics_trackmessages'] = array(
'#type' => 'checkboxes',
'#title' => t('Track messages of type'),
'#default_value' => variable_get('googleanalytics_trackmessages', array()),
'#description' => t('This will track the selected message types shown to users. Tracking of form validation errors may help you identifying usability issues in your site. Keep in mind that Google allows a maximum of 500 events per session only and every message is tracked as one individual event. If the limit has been exceeded no further events are tracked, but normal page tracking is not effected. Messages from excluded pages cannot tracked.'),
'#options' => array(
'status' => t('Status message'),
'warning' => t('Warning message'),
'error' => t('Error message'),
),
);
// Google already have many translations, if not - they display a note to change the language.
global $language;
$form['tracking']['search_and_adsense'] = array(
......
......@@ -71,6 +71,17 @@ Drupal.behaviors.trackingSettingsSummary = {
return Drupal.t('@items enabled', {'@items' : vals.join(', ')});
});
$('fieldset#edit-messagetracking', context).drupalSetSummary(function (context) {
var vals = [];
$('input[type="checkbox"]:checked', context).each(function () {
vals.push($.trim($(this).next('label').text()));
});
if (!vals.length) {
return Drupal.t('Not tracked');
}
return Drupal.t('@items enabled', {'@items' : vals.join(', ')});
});
$('fieldset#edit-search-and-adsense', context).drupalSetSummary(function (context) {
var vals = [];
if ($('input#edit-googleanalytics-site-search', context).is(':checked')) {
......
......@@ -127,6 +127,26 @@ function googleanalytics_page_alter(&$page) {
drupal_add_js(drupal_get_path('module', 'googleanalytics') . '/googleanalytics.js');
}
// Add messages tracking.
$message_events = '';
if ($message_types = variable_get('googleanalytics_trackmessages', array())) {
$message_types = array_values(array_filter($message_types));
$status_heading = array(
'status' => t('Status message'),
'warning' => t('Warning message'),
'error' => t('Error message'),
);
foreach (drupal_get_messages(NULL, FALSE) as $type => $messages) {
// Track only the selected message types.
if (in_array($type, $message_types)) {
foreach ($messages as $message) {
$message_events .= '_gaq.push(["_trackEvent", ' . drupal_json_encode(t('Messages')) . ', ' . drupal_json_encode($status_heading[$type]) . ', ' . drupal_json_encode(strip_tags($message)) . ']);';
}
}
}
}
// Add User profile segmentation values.
$profile_fields = variable_get('googleanalytics_segmentation', array());
if (!empty($profile_fields)) {
......@@ -256,6 +276,9 @@ function googleanalytics_page_alter(&$page) {
else {
$script .= '_gaq.push(["_trackPageview", ' . $url_custom . ']);';
}
if (!empty($message_events)) {
$script .= $message_events;
}
if (!empty($codesnippet_after)) {
$script .= $codesnippet_after;
}
......
......@@ -299,6 +299,52 @@ class GoogleAnalyticsCustomVariablesTest extends DrupalWebTestCase {
}
}
class GoogleAnalyticsStatusMessagesTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Google Analytics status messages tests'),
'description' => t('Test status messages functionality of Google Analytics module.'),
'group' => 'Google Analytics',
);
}
function setUp() {
parent::setUp('googleanalytics');
$permissions = array(
'access administration pages',
'administer google analytics',
);
// User to set up google_analytics.
$this->admin_user = $this->drupalCreateUser($permissions);
}
function testGoogleAnalyticsStatusMessages() {
$ua_code = 'UA-123456-4';
variable_set('googleanalytics_account', $ua_code);
// Enable logging of errors only.
variable_set('googleanalytics_trackmessages', array('error' => 'error'));
$this->drupalPost('user/login', array(), 'Log in');
$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Username field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Username field is required." is shown.');
$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Password field is required."]);', '[testGoogleAnalyticsStatusMessages]: _trackEvent "Password field is required." is shown.');
// @todo: investigate why drupal_set_message() fails.
//drupal_set_message('Example status message.', 'status');
//drupal_set_message('Example warning message.', 'warning');
//drupal_set_message('Example error message.', 'error');
//drupal_set_message('Example error <em>message</em> with html tags and <a href="http://example.com/">link</a>.', 'error');
//$this->drupalGet('');
//$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Status message", "Example status message."]);', '[testGoogleAnalyticsStatusMessages]: Example status message is not enabled for tracking.');
//$this->assertNoRaw('_gaq.push(["_trackEvent", "Messages", "Warning message", "Example warning message."]);', '[testGoogleAnalyticsStatusMessages]: Example warning message is not enabled for tracking.');
//$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Example error message."]);', '[testGoogleAnalyticsStatusMessages]: Example error message is shown.');
//$this->assertRaw('_gaq.push(["_trackEvent", "Messages", "Error message", "Example error message with html tags and link."]);', '[testGoogleAnalyticsStatusMessages]: HTML has been stripped successful from Example error message with html tags and link.');
}
}
class GoogleAnalyticsRolesTest extends DrupalWebTestCase {
public static function getInfo() {
......
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