Set Embedded Data Not Working - Get Age from Birthday and Assign Age Group | XM Community
Solved

Set Embedded Data Not Working - Get Age from Birthday and Assign Age Group


I know this question has been asked many times, but I've researched and tried everything and for some reason this still isn't working for me - trying to assign an embedded data field called UserAge from a question getting birthday in the format MM/DD/YYYY.
1. I've set an embedded data field, UserAge at the start of my survey flow.
2. I've placed a block below with the question asking for birthday, with the following JS:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');
Qualtrics.SurveyEngine.setEmbeddedData('UserAge', age);
});
Have also tried the different codes on this thread (and looked at other threads as well):
https://www.qualtrics.com/community/discussion/1397/javascript-to-calculate-users-age-based-on-dob-check-age-eligibility-provide-validation-alert/p1
3. After the block, I have a "Then" branch based on UserAge (if under 18, terminate).
Am I missing anything? For some reason, I can't get UserAge to write from setEmbeddedData. Using alerts, I can see that no value is being assigned to it.
Side note - I also want to create additional variables from birthday - (1) birth year, and (2) age groups (e.g. a variable called age group with categories of 18-30, 30-50, 50-70, 70+).
Any help would be much appreciated! New to JS and survey flow programming.
Thanks!

icon

Best answer by AnnaH 22 May 2020, 16:10

View original

2 replies

Userlevel 7
Badge +27

  1. Did you load moment.js in your survey header?

  2. Did you check the console for errors?

  3. Use console.log instead of alerts for debugging

  4. You can use moment.js to get the birth year (see format)

  5. You can use if/else statements to assign to age categories

Got it to work, thanks - forgot to load in the header. It's not letting me accept your answer unfortunately but thank you!

Leave a Reply