Using JavaScript to Calculate Lowest 3 Category Scores | XM Community
Skip to main content

Hi all,


Looking for someone with JavaScript knowledge to help me with my custom script (I have no experience, I just learn by figuring the code out visually in this case). 

Essentially the survey has 6 categories. The user will answer a quiz with questions under those categories and score accordingly. We want the end of survey message to calculate the user’s lowest 3 category scores so we can send a customised message to recommend courses in those categories for the user to take. 

My issue is that the calculated lowest 3 scores do not appear and I’m not sure why. Appreciate any assistance on this :)

Sample message:

Thank you for completing the survey! Based on your responses, we've identified the three categories where you scored the lowest. To help you improve in these areas, we recommend the following courses:

  1. tCategory 1]: oCourse Recommendation]
  2. tCategory 2]: oCourse Recommendation]
  3. tCategory 3]: oCourse Recommendation]

Here is the code I have currently, as a question at the end with JavaScript: 

 

Qualtrics.SurveyEngine.addOnload(function()

{var scores = {

"The Lesson": "${e://Field/TheLessonScore}",

"Assessment And Feedback": "${e://Field/AssessmentAndFeedbackScore}",

"The Study": "${e://Field/TheStudyScore}",

"Differentiation": "${e://Field/DifferentiationScore}",

"The Tutorial": "${e://Field/TheTutorialScore}",

"Applying The Student Behaviour Policy": "${e://Field/ApplyingTheStudentBehaviourPolicyScore}"   

};

 

// Sort scores to find the lowest

var sortedScores = Object.keys(scores).sort(function(a, b) {

return scoresea] - scores[b]; 

});

 

// Set the lowest category names in embedded data fields

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory1", sortedScores,0].name);

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory2", sortedScores,1].name);

Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory3", sortedScores,2].name);

});

Below is the customised End of Survey code and preview message:

 


Thank you!

@LisaNguyen,

You should set up your items as an array of objects, then sort:

Qualtrics.SurveyEngine.addOnload(function() {
var scores = =
{name:"The Lesson", score:"${e://Field/TheLessonScore}"},
{name:"Assessment And Feedback", score:"${e://Field/AssessmentAndFeedbackScore}"},
{name:"The Study", score:"${e://Field/TheStudyScore}"},
{name:"Differentiation", score:"${e://Field/DifferentiationScore}"},
{name:"The Tutorial", score:"${e://Field/TheTutorialScore}"},
{name:"Applying The Student Behaviour Policy", score:"${e://Field/ApplyingTheStudentBehaviourPolicyScore}"
];

// Sort scores to find the lowest
scores.sort(function(a, b) { a.score - b.score; });

jQuery.each(scores, function(i) {
Qualtrics.SurveyEngine.setEmbeddedData("LowestCategory"+(i+1),this.name);
});
});

 


Thanks ​@TomG

 

I’ve added the JavaScript you advised and I’m not sure how to have it display the lowest 3 category scores in the End of Survey message. Appreciate your help on this as well.

Thank you for completing the survey! Based on your responses, we've identified the three categories where you scored the lowest. To help you improve in these areas, we recommend the following courses:

  1. : tCourse Recommendation]
  2. : tCourse Recommendation]
  3. : tCourse Recommendation]

 

Great job! According to your scores on the Phase 3 pre-survey, you may benefit from the Lessons in the following categories:

  1. ${e://Field/LowestCategory}
  2. ${e://Field/LowestCategory}
  3. ${e://Field/LowestCategory}

Add 1, 2, 3 to the end of the embedded data fields (e.g., ${e://Field/LowestCategory1})


Thanks Tom, I tried to update it but I see nothing.

Great job! According to your scores on the Phase 3 pre-survey, you may benefit from the Lessons in the following categories:

  1. ${e://Field/LowestCategory1}
  2. ${e://Field/LowestCategory2}
  3. ${e://Field/LowestCategory3}

Test 2 - ${e://Field/LowestCategory1} ${e://Field/LowestCategory2} ${e://Field/LowestCategory3}

 

 

Preview below: 

 

And this is my survey flow for reference:

 

 


You seem to be filling your embedded data score fields after your js script. Try moving the ED element before your JavaScript [end of survey] block.


Hi ​@vgayraud,

Thanks for your input 🙂.

 

I tried moving the ED element before the Javascript but nothing still shows.

 

Preview below:

 


Unless they’re higher in your survey flow, you don’t seem to have ED fields to store your results (LowestCategory1, LowestCategory2 and LowestCategory3). You seem to be piping in undefined ED fields.


You're right on that (I don’t have it on my survey flow). I’m not sure what to put down as the ED field for these.

 

i.e. ED “LowestCategory1” = *3not sure what to put here for value]*


You don’t need to put any value. Leave them empty and they’ll be filled by your script. They just need to be declared.

 


Thanks, I tried this but still same result. 

 


I’m not too sure then.. Are you using the “new survey taking experience” layout or one of the older layouts?

 


Appreciate the screenshots!

I got the 1st screenshot as “on” fort the New survey taking experience. I don’t see the “layout” tab on your 2nd screenshot so I will look into that with Qualtrics. 


Ok, then you can’t use the old .setEmbeddedData.

Your script should be like this :

Qualtrics.SurveyEngine.addOnload(function() {
var scores = =
{name:"The Lesson", score:"${e://Field/TheLessonScore}"},
{name:"Assessment And Feedback", score:"${e://Field/AssessmentAndFeedbackScore}"},
{name:"The Study", score:"${e://Field/TheStudyScore}"},
{name:"Differentiation", score:"${e://Field/DifferentiationScore}"},
{name:"The Tutorial", score:"${e://Field/TheTutorialScore}"},
{name:"Applying The Student Behaviour Policy", score:"${e://Field/ApplyingTheStudentBehaviourPolicyScore}"
];

// Sort scores to find the lowest
scores.sort(function(a, b) { a.score - b.score; });

jQuery.each(scores, function(i) {
Qualtrics.SurveyEngine.setJSEmbeddedData("LowestCategory"+(i+1),this.name);
});
});

Your EDs like this :

And your end of survey piped in text should be :

${e://Field/__js_LowestCategory1}

 


Also, ​@TomG ‘s script is using jQuery, which isn’t included in the new layout style. You’ll want to add this to your header, in source mode :

<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script>

 


Still doesn’t work unfortunately :(
 

Javascript on question at the end:

Survey workflow:


Preview end of survey message:

 


@vgayraud 

I tried the header script too. Same result as previous comment unfortunately. 

 


 

Qualtrics.SurveyEngine.addOnload(function() {
var scores =
{name:"The Lesson", score: parseFloat("${e://Field/TheLessonScore}")},
{name:"Assessment And Feedback", score: parseFloat("${e://Field/AssessmentAndFeedbackScore}")},
{name:"The Study", score: parseFloat("${e://Field/TheStudyScore}")},
{name:"Differentiation", score: parseFloat("${e://Field/DifferentiationScore}")},
{name:"The Tutorial", score: parseFloat("${e://Field/TheTutorialScore}")},
{name:"Applying The Student Behaviour Policy", score: parseFloat("${e://Field/ApplyingTheStudentBehaviourPolicyScore}")}
];

scores.sort(function(a, b) {
return a.score - b.score;
});

scores.forEach(function(item, index) {
Qualtrics.SurveyEngine.setJSEmbeddedData("LowestCategory" + (index + 1), item.name);
});

});

 


How do I open/use the attachment?


It’s a .qsf file (qualtrics survey definition file), but weirdly we can’t attach .qsf in here. You can remove the .txt extension and create a new questionnaire within Qualtrics with it.

It’s just a demo though with generic scores and category names, the updated code I gave you should work with your setup.

Good luck! :)


Thanks so much. I'm struggling to import the qsf because it comes up as text. I don't have the option to remove the txt extension as well it seems.


Leave a Reply