Creating a new variable that counts the values of another embedded variable | XM Community
Skip to main content

Hi! Pardon me if this has been asked before. I perused the forums and didn't find anything.
I've launched an intercept survey on our website using Website/App Feedback. The intercept is using JavaScript to embed a multi-value variable, which I'm passing into the survey in the survey flow. For the sake of explanation, let's say this variable is fruit, and fruit can have one or more values.
I am wanting to create a new variable that counts how many fruits are embedded in each response. Let's call it fruit_count. So, if for a given response fruit = "orange", fruit_count should equal 1. If fruit = 'banana, strawberry', fruit_count should equal 2, and so on.
Does anyone know a way to achieve this? The field editor in the Data Analysis tab looked promising, but was a dead end.
Thanks! Grateful for the support in this community.

What kind of intercept is it? If it's a Custom HTML survey inserted you could have some creative survey logic in the flow which would count it behind the scenes using a bunch of if statements by looking through each fruit individually. You just keep a counter and increment it every time you find a fruit. If you aren't using Customer HTML or something Non-guided where you can't use survey logic I am at a loss short of doing it manually after the fact.


You could do it in the survey with JS added to a question all respondents see. Add fruit_count as an embedded data field at the beginning of the survey flow. Then this JS to a question:
Qualtrics.SurveyEngine.addOnload(function() {
var fcount = "{e://Field/fruit}".split(", ").length;
Qualtrics.SurveyEngine.setEmbeddedData("fruit_count",fcount);
});


Thank you so much, MSobotta and TomG . The survey is just a pop-over, so not using Custom HTML.
TomG I deployed this JavaScript but it's returning

1
for each response. I'm playing around with it to see if I can get it working, but also open to ideas.
Thanks again for your kind help!


https://community.qualtrics.com/XMcommunity/discussion/comment/49868#Comment_49868The split string is probably wrong. I used comma+space because that is what you posted and how Qualtrics delimits multiple answers. Maybe the delimiter is just a comma.


That being said you would have to make sure you are not off by one.


https://community.qualtrics.com/XMcommunity/discussion/comment/49870#Comment_49870That seems to be it! Thanks so much for your help.


https://community.qualtrics.com/XMcommunity/discussion/comment/49881#Comment_49881Good call, thank you!


Leave a Reply