Setting Embedded Data with JavaScript from iframe data. | Experience Community
Skip to main content

Setting Embedded Data with JavaScript from iframe data.

  • April 28, 2026
  • 2 replies
  • 44 views

Forum|alt.badge.img

Context: For a study, I have (together with ChatGPT) written an html and JavaScript code to display a panorama picture and allow users to click on the picture on places where they would take a swab. The code stores the coordinates of those swabs, as well as the order of the swabs, so I can later turn it into a heatmap for analysis. The code therefore stores two variables: the final stage with all swabs and an event log. I display this webpage using an iframe in a survey. 

Problem: The data is correctly stored in the JS variable and visible in the developer console of the webbrowser when previewing the survey. I want the data to be transferred to a Qualtrics Embedded Data variable upon going to the next page. However, I cannot manage to store that data in a Qualtrics Embedded Data variable. I currently have the following code for the specific question with the panorama iframe:

Qualtrics.SurveyEngine.addOnload(function() {

let swabData = null;
let alreadySubmitting = false;

window.addEventListener("message", function(event) {
console.log("RECEIVED:", event.data);

if (event.data && event.data.eventLog) {
swabData = event.data;
}
});

const nextButton = document.getElementById("NextButton");

nextButton.addEventListener("click", function(e) {

if (alreadySubmitting) return;

e.preventDefault();

const iframe = document.getElementById("swabFrame");
iframe.contentWindow.postMessage("requestData", "*");

const waitForData = setInterval(() => {

if (swabData) {
clearInterval(waitForData);

console.log("Storing data:", swabData);

Qualtrics.SurveyEngine.setEmbeddedData("eventLog", JSON.stringify(swabData.eventLog));
Qualtrics.SurveyEngine.setEmbeddedData("finalSwabs", JSON.stringify(swabData.finalSwabs));
Qualtrics.SurveyEngine.setEmbeddedData("test", "It does not save from here");

alreadySubmitting = true;
Qualtrics.SurveyEngine.navClick(event, 'NextButton');
}

}, 100);

setTimeout(() => {
if (!swabData) {
console.warn("Timeout, continuing anyway");
alreadySubmitting = true;
nextButton.click();
}
}, 1000);

});

});

All console.log()s print the correct data in the developer console. The setEmbeddedData on the test value does not save anything either. My Survey Flow looks as follows:

The console in the developer tools shows the following:

SENDING DATA: {eventLog: Array(4), finalSwabs: Array(4)}
RECEIVED: {eventLog: Array(4), finalSwabs: Array(4)}
Storing data: {eventLog: Array(4), finalSwabs: Array(4)}
Just got a page template transaction id 5 but we are on 5. Ignoring new template.
Just got a page template transaction id 5 but we are on 5. Ignoring new template.

 

What am I doing wrong here?

2 replies

arunxmarchitect
Level 4 ●●●●
Forum|alt.badge.img+7

@Timon , the issue is that setEmbeddedData() does not work in the New Survey Taking Experience in Qualtrics.

Ref: Qualtrics JS documentation: https://api.qualtrics.com/82bd4d5c331f1-qualtrics-java-script-question-api-class

Steps to fix

1. Replace the method
Use this instead:

Qualtrics.SurveyEngine.setJSEmbeddedData(key, value);

2. Update your code

Qualtrics.SurveyEngine.setJSEmbeddedData("eventLog", JSON.stringify(swabData.eventLog));
Qualtrics.SurveyEngine.setJSEmbeddedData("finalSwabs", JSON.stringify(swabData.finalSwabs));
Qualtrics.SurveyEngine.setJSEmbeddedData("test", "Now it will save");

3. Understand the field names

  • Qualtrics will store these as:
    • __js_eventLog
    • __js_finalSwabs
    • __js_test

4. Update Survey Flow

  • Add embedded data fields with:

    __js_eventLog
    __js_finalSwabs

5. Use in piped text (if needed)

${e://Field/__js_eventLog}

6. Ensure timing

  • Make sure setJSEmbeddedData() runs before navigating to the next page.

 

 


Forum|alt.badge.img
  • Author
  • April 30, 2026

Thanks for the response ​@arunxmarchitect! I am still using the “old” survey taking experience and was therefore still using the regular setEmbeddedData. Setting embedded data before the interval function worked fine, just not from within the interval function. When I tried using the new survey taking experience, with your changes, I ran into a different problem: 

Error executing custom js: TypeError: Cannot read properties of null (reading 'addEventListener')
    at ne.<anonymous> (<anonymous>:16:14)
    at main.3389e44f.js:32:34919
    at Array.forEach (<anonymous>)
    at ue (main.3389e44f.js:32:34903)
    at main.3389e44f.js:32:23510
    at Array.forEach (<anonymous>)
    at Object.o [as notify] (main.3389e44f.js:32:23498)
    at main.3389e44f.js:32:71717
    at Generator.next (<anonymous>)
    at u (4283.fab782ed.js:1:55256)

Trying to tackle that with ChatGPT led to a code that does prints the right content at the right time and does not generate visible errors, but it still does not save the data to the embedded data variable.