How to read and write embedded data upon window change? | XM Community
Skip to main content

I want to record a count of how many times a respondent changes their window while taking my survey. I have the following code, but it doesn’t read in the `DocumentHiddenCount` embedded field (set to 0 at the start of the survey) correctly. When I look at it in my browser, the `”${e://Field/DocumentHiddenCount}"` disappears.

 

Qualtrics.SurveyEngine.addOnReady(function()
{
document.addEventListener("visibilitychange", () => {
var dhc = parseInt("${e://Field/DocumentHiddenCount}")
console.log(dhc)
dhc += 0.5
Qualtrics.SurveyEngine.setEmbeddedData("DocumentHiddenCount", dhc)
console.log(dhc)
});

});

 

@adamkucz Hey try adding the below in Header (source) of your survey, it will record the value in the embedded data appropriately. Keep the value of embedded data empty.

<script>
Qualtrics.SurveyEngine.addOnReady(function() {
document.addEventListener("visibilitychange", function() {
var dhc = 0;
console.log(dhc);
dhc += 0.5;
Qualtrics.SurveyEngine.setEmbeddedData("DocumentHiddenCount", dhc);
console.log(dhc);
});
});
</script>

Hope it helps!


Hey @Deepak . Unfortunately this did not work, because `dhc` is set to 0 in your script, but I need to read it in from the embedded data. Your script constantly resets `dhc` to 0 and thus does not include a cumulative count. Do you know how to fix this?


Hey @Deepak . Unfortunately this did not work, because `dhc` is set to 0 in your script, but I need to read it in from the embedded data. Your script constantly resets `dhc` to 0 and thus does not include a cumulative count. Do you know how to fix this?

Sure, your script looks correct then I just made some changes try this:

<script>

Qualtrics.SurveyEngine.addOnReady(function() {

document.addEventListener("visibilitychange", function() {

var dhc = parseFloat("${e://Field/DocumentHiddenCount}");

console.log(dhc);

dhc += 0.5;

Qualtrics.SurveyEngine.setEmbeddedData("DocumentHiddenCount", dhc.toString());

console.log(dhc);

});

});

</script>

Add this in your surveys header.

Hope it helps!


@Deepak, I appreciate the help. Unfortunately this is also not working. When I inspect the code in my browser, it removes the arguments to `parseFloat()`. Any idea what is going on?
 

 


Leave a Reply