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

How to read and write embedded data upon window change?


Forum|alt.badge.img+2

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)
	});

});

 

4 replies

Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+44
  • 1549 replies
  • August 20, 2023

@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!


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • 2 replies
  • August 21, 2023

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?


Deepak
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+44
  • 1549 replies
  • August 22, 2023
adamkucz wrote:

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!


Forum|alt.badge.img+2
  • Author
  • Level 1 ●
  • 2 replies
  • August 28, 2023

@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