Storing matrix question statements as embedded data (according to respondent's choice)
Hi everyone!
I am trying to construct something really tricky:
I have a matrix question with 6 statements ...
our company
competitor 1
competitor 2
competitor 3
competitor 4
competitor 5
and 5 scale points:
no knowledge
knowledge
considerance
purchase
loyalty
The scale points are recoded in values from 1 to 5.
Now I want to safe two of the statements as embedded data according to the following rules:
a) If for "our company" 2 or higher is chosen, then store "our company" AND store another statement, namely the one with the highest chosen scale point. If there are two or more statements with the same (highest) chosen scale point, then take one of them randomly and store it.
b) If for "our company" 1 is chosen, then store the two (other) statements with the highest chosen scale point. If there are three or more statements with the same (highest) chosen scale point, then take two of them randomly and store them.
I created two embedded data fields:
SelectedStatement1
SelectedStatement2
Then I created a new "Text/Graphic" question right after my matrix question (only text display, no answer option) and within this question I set up this java code:
// Create an HTTP GET request HttpGet getRequest = new HttpGet(baseUrl);
// Set the API key as a request header getRequest.addHeader("X-API-TOKEN", apiKey);
// Send the GET request HttpResponse response = httpClient.execute(getRequest);
// Check the response status code int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { // Request was successful String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Response from Qualtrics API:"); System.out.println(responseBody); } else { // Request failed System.err.println("Request failed with status code: " + statusCode); } } catch (Exception e) { e.printStackTrace(); } } }
But it does not work: The embedded data fields are always empty.
Can anyone help me with this?
I would be very happy.
Henrike
Page 1 / 1
@HenrikeS - Your JavaScript is completely unrelated to what you are trying to accomplish. You’ll need to create JavaScript to apply your rules and set embedded data fields using the addOnPageSubmit function. Based on what you posted, you may need to find someone that knows JavaScript to do it for you.
@HenrikeS
Add a page break between matrix question and Text/Graphic question. Also, add recode values for Matrix scales. Then add the below script to text/graphic question.
Note: Update the question Id’s in script.
Qualtrics.SurveyEngine.addOnload(function() { jQuery("#QID6").hide(); //to hide the text-graphic question
var a = (parseInt("${q://QID5/SelectedAnswerRecode/1}"))||0 var b = (parseInt("${q://QID5/SelectedAnswerRecode/2}"))||0 var c = (parseInt("${q://QID5/SelectedAnswerRecode/3}"))||0 var d = (parseInt("${q://QID5/SelectedAnswerRecode/4}"))||0 var e = (parseInt("${q://QID5/SelectedAnswerRecode/5}"))||0 var f = (parseInt("${q://QID5/SelectedAnswerRecode/6}"))||0 var arr = b, c, d, e, f]; var largest = arra0]; var secondLargest = 0; var statement1 var statement2
//script to find largest answer for (var i = 0; i < arr.length; i++) { if (arrai] > largest ) { largest = arrai]; } }
//script to find 2nd largest answer var max = Math.max.apply(null, arr); // get the max of the array arr.splice(arr.indexOf(max), 1); // remove max from the array secondLargest = Math.max.apply(null, arr);
if (largest == b) { largest = "Competitor 1" } else if(largest == c) { largest = "Competitor 2" } else if(largest == d) { largest = "Competitor 3" } else if(largest == e) { largest = "Competitor 4" } else { largest = "Competitor 5" }
if (a >= 2) { statement1 = "Our Company" statement2 = largest Qualtrics.SurveyEngine.setEmbeddedData('SelectedStatement1', statement1 ); Qualtrics.SurveyEngine.setEmbeddedData('SelectedStatement2', statement2 ); } else { statement1 = largest statement2 = secondLargest Qualtrics.SurveyEngine.setEmbeddedData('SelectedStatement1', statement1 ); Qualtrics.SurveyEngine.setEmbeddedData('SelectedStatement2', statement2 ); } });
@HenrikeS - Your JavaScript is completely unrelated to what you are trying to accomplish. You’ll need to create JavaScript to apply your rules and set embedded data fields using the addOnPageSubmit function. Based on what you posted, you may need to find someone that knows JavaScript to do it for you.
Sorry, I posted the wrong code (my concentration got lost after hours of error seeking). The correct one is:
// Find the selected value for each statement jQuery('.q-matrix-table tbody tr').each(function() { var statement = jQuery(this).find('td').first().text(); var selectedValue = parseInt(jQuery(this).find('input:checked').val());
// Find other statements with the same highest value var highestValue = selectedStatementse0].value; var highestValueStatements = selectedStatements.filter(function(statement) { return statement.value === highestValue; });
Anushree, that is a great step forward - now the embedded fields are filled!
But it is not working completely correctly. When I set the following choices for my matrix question …
our company: 1
competitor 1: 4
competitor 2: 5
competitor 3: 2
competitor 4: 3
competitor 5: 3
… then
SelectedStatement1 is competitor 1
SelectedStatement2 is competitor 1
Correctly it should be:
SelectedStatement1 is our company
SelectedStatement2 is competitor 2
I tried it again with other choices, but the result is always the same:
SelectedStatement1 is competitor 1
SelectedStatement2 is competitor 1
@HenrikeS
No, the coding I have provided gives the correct result. I tried replicating the same answer you have provided and received the expected output.
Please check if you have updated the question id in the code.
var a = (parseInt("${q://QID5/SelectedAnswerRecode/1}"))||0 var b = (parseInt("${q://QID5/SelectedAnswerRecode/2}"))||0 var c = (parseInt("${q://QID5/SelectedAnswerRecode/3}"))||0 var d = (parseInt("${q://QID5/SelectedAnswerRecode/4}"))||0 var e = (parseInt("${q://QID5/SelectedAnswerRecode/5}"))||0 var f = (parseInt("${q://QID5/SelectedAnswerRecode/6}"))||0
@Anushree
Yes, I have updated the question IDs. I will check everything again, maybe I have made another mistake ...
@HenrikeS - Also check the recoded values as well, as the context piped in is selectedChoiceRecodedValue.
Let me know if the script solves the requirement.
@Anushree
Fixed it!!!
I found out that question export tags for my matrix question statements were not numbered sequentially (obviously I had deleted some so that they were numbered
QID31_1
QID31_16
QID31_17
QID31_18
QID31_19
QID31_21)
Now I created a new matrix question wherin the question export tags are sequentially numbered.
AND IT WORKS!
Thank you so much, Anushree!
@Anushree
One bug left:
When “Our Company” rates 1 and three competitors rate the same, then one of the three competitors is selected for SelectedStatement1 and for SelectedStatement2.
This seems to work:
Qualtrics.SurveyEngine.addOnload(function() {
jQuery("#QID30").hide(); //to hide the text-graphic question
var a = (parseInt("${q://QID31/SelectedAnswerRecode/1}")) || 0;
var b = (parseInt("${q://QID31/SelectedAnswerRecode/2}")) || 0;
var c = (parseInt("${q://QID31/SelectedAnswerRecode/3}")) || 0;
var d = (parseInt("${q://QID31/SelectedAnswerRecode/4}")) || 0;
var e = (parseInt("${q://QID31/SelectedAnswerRecode/5}")) || 0;
var f = (parseInt("${q://QID31/SelectedAnswerRecode/6}")) || 0;
var arr = =b, c, d, e, f];
// Find the largest and second largest answers
var largest = Math.max.apply(null, arr);
var secondLargest = Math.max.apply(null, arr.filter(x => x !== largest));
// Create an array to store Competitors with the largest score
var largestCompetitors = s];
if (largest == b) largestCompetitors.push("Competitor 1");
if (largest == c) largestCompetitors.push("Competitor 2");
if (largest == d) largestCompetitors.push("Competitor 3");
if (largest == e) largestCompetitors.push("Competitor 4");
if (largest == f) largestCompetitors.push("Competitor 5");
// Randomly select one from the largestCompetitors
var selectedLargest = largestCompetitorstMath.floor(Math.random() * largestCompetitors.length)];
var statement1;
var statement2;
if (a >= 2) {
statement1 = "Our Company";
statement2 = selectedLargest;
} else {
statement1 = selectedLargest;
// Filter out selectedLargest from largestCompetitors
var remainingCompetitors = largestCompetitors.filter(x => x !== selectedLargest);
// Randomly select one from the remainingCompetitors