How can I count errors? | XM Community
Skip to main content
My survey has questions which require validation, and I'd like to know how many times each respondent made a wrong answer before he moved on. This means I'd like to count either the number of times the error message appeared or how many times the respondent hit the next page button.



I have no idea in JavaScript, however, someone gave me the following:



!





I have an embedded data field named "errors" of type text and set to 0. It is generated before the page of the question in which I want to count the error.



Long story short, this doesn't work - the errors field is still 0, no matter how many times one hits the nextbutton on the page.



Thanks in advance!
EDIT: Although the Workaround is still valid (despite being long-winded), 'The Large Problem' as described below is incorrect - use getEmbeddedData instead of just setting a variable - see other responses in this thread.



---



Really good question - worth creating a feature request due to the larger problem described below. I don't think it's possible (due to the larger problem) to achieve this as perfectly as possible, but I've suggested a workaround below.



THE LARGER PROBLEM



Really good question - worth creating a feature request out of it as getting/setting embedded data when a page refreshes does NOT work as expected. Specifically, if I have defined an embedded data variable and then decide to bring it in JS and then re-set its value within the onLoad fucntion, a page refresh or a validation error does not overwrite the embedded data variable. For example, if you have define a variable n = 0 to count the times a page is refreshed, then the following code (when set on any question within a page) does NOT work:



var n = parseInt("${e://Field/n}")

Qualtrics.SurveyEngine.setEmbeddedData("n", n+1)



While this code should take n the first time when it's 0, then set its value to 1; if a page is refreshed/validation error occurs, then it should take n as 1 and set its value to 2 and so on. Instead what happens is that n is set to 1 every time (i.e. it's initialized to 0 every time).



WORKAROUND



Instead of validation, I've taken advantage of display logic. Specifically, if the user selects the wrong choice of the validation question (or whatever criteria you want to specify), then they progress on the next page which increments the embedded data variable n. Once the variable is incremented, then they immediately go to the previous page to complete the validation question again. A descriptive text question contains an error message and is updated if the validation is unsuccessful. Importantly, the back button needs to be enabled from the Survey Options (hide it on all other pages if you don't need it).



There is a lot of room for customization.



Working version attached - see JS code on Q1, Q2 and note again that the Back Button is enabled via the Survey Options.
Thanks a lot!



I actually found some other workaround. I use the code below, which listens to the event of clicking then next page button. I think it solves the problem you are discussing by simply increasing the "clicks" variable ("n" for you) each time this event occurs. This counts the numbers of clicks on the button, which means the number of errors is "clicks"-1. There are two shortcomings.

The first, which your solution shares, is that if I have several questions in the page I can only know the number of times that any question was answered incorrectly (maybe it's one question, maybe fourteen), where I'd rather know how many times each question was answered incorrectly

A second issue, which You solution solves, is that if respondents move on to next pages using the spacebar, it escapes this event listener (maybe there is an event listener specifically for the spacebar clicking event?)



Here's the code I used (with variable named "clicks_page_1")



Qualtrics.SurveyEngine.addOnReady(function()

{

/*Place your JavaScript here to run when the page is fully displayed*/

var clicks = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("clicks_page_1"));

jQuery('#NextButton').on('click', function() {

clicks++;

Qualtrics.SurveyEngine.setEmbeddedData("clicks_page_1", clicks);





});



});
Snap! Silly me... My previous elaboration of 'the larger problem' is dead wrong.



Starting from your great insight to actually use the getEmbeddedData function, I've improved it ever so slightly to capture validation errors specifically and to also be question-specific. Insert this code to any question whose validation errors you want to capture (after creating an embedded data variable errors_q_1 before the page):



var question = $(this.questionId)

$(question).select('.ValidationError').each(function(error) {

if (error.innerHTML != '') {

var errors = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("errors_q_1"))

errors++;

Qualtrics.SurveyEngine.setEmbeddedData("errors_q_1", errors);

}

})



You can even customize it if you have multiple validation errors (e.g. force response and numbers only)



var question = $(this.questionId)

$(question).select('.ValidationError').each(function(error) {

if (error.innerHTML == 'error message 1 goes here') {

var errors = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("errors_q_1_v1"))

errors++;

Qualtrics.SurveyEngine.setEmbeddedData("errors_q_1_v1", errors);

}

if (error.innerHTML == 'error message 2 goes here') {

var errors2 = parseInt(Qualtrics.SurveyEngine.getEmbeddedData("errors_q_1_v2"))

errors2++;

Qualtrics.SurveyEngine.setEmbeddedData("errors_q_1_v2", errors2);

}

})

Leave a Reply