I have a problem with saving data. I have three experiments from the lab.js library embedded in my study. I added them one by one following the instructions. They display fine, unfortunately after saving the study, the answers from other questions are saved normally, and in the case of these experiments, the table is empty. Below is a screenshot of the flow and the javascript code for one of the studies.
Thanks in advance for your help
Qualtrics.SurveyEngine.addOnReady(function() { const page = this page.hideNextButton()
// Listen for the study sending data window.addEventListener('message', function _labjs_data_handler(event) { // Make sure that the event is from lab.js, then ... if (event.data.type === 'labjs.data') { // ... extract the JSON data lab.js is sending. const data = event.data.json
// ... save data and submit page Qualtrics.SurveyEngine.setEmbeddedData('stroop-data', data) window.removeEventListener('message', _labjs_data_handler) page.clickNextButton() } }) });
Page 1 / 1
@291657agA I assume your JavaScript is used in the question block after the embedded data block, right? Is the next button clicked?
This line looks like the response you expect is a JSON?
const data = event.data.json
Storing the whole JSON in your embedded data will probably not work. So you might want to try this:
// Listen for the study sending data window.addEventListener('message', function _labjs_data_handler(event) { // Make sure the event is from lab.js and contains data if (event.data.type === 'labjs.data') { // Log the response details console.log('Data received from lab.js:'); console.log(event.data);
// Convert the JSON object to a string const dataString = JSON.stringify(event.data.json);
// Save the stringified data as embedded data in Qualtrics Qualtrics.SurveyEngine.setEmbeddedData('stroop-data', dataString);
// Remove the event listener after data is received window.removeEventListener('message', _labjs_data_handler);
// Trigger the next button to move to the next Qualtrics page page.clickNextButton(); } }); });
I additionally included some console.log statement to see which data is provided from the library (as I am not aware of this libary). Would be great if you could share the log from the browser console in the developer tools. You may also check for other errors shown in the console.
Last question - which layout do you use in your survey?
Best Christian
@291657agA The only other post I found related to this library is this:
// Listen for the study sending data window.addEventListener('message', function _labjs_data_handler(event) { // Make sure the event is from lab.js and contains data if (event.data.type === 'labjs.data') { // Log the response details console.log('Data received from lab.js:'); console.log(event.data);
// Convert the JSON object to a string const dataString = JSON.stringify(event.data.json);
// Save the stringified data as embedded data in Qualtrics Qualtrics.SurveyEngine.setEmbeddedData('stroop-data', dataString);
// Remove the event listener after data is received window.removeEventListener('message', _labjs_data_handler);
// Trigger the next button to move to the next Qualtrics page page.clickNextButton(); } }); });
I additionally included some console.log statement to see which data is provided from the library (as I am not aware of this libary). Would be great if you could share the log from the browser console in the developer tools. You may also check for other errors shown in the console.
Last question - which layout do you use in your survey?
Best Christian
Hi, to answer your questions one by one:
Yes, my JavaScript is used in the question block after the embedded data
The next button is clicked at the end of each section
Actually i do not know what data type i expect, i am not really familliar with coding, esspecially with javascript, i’ve just follow the instructions to which you pasted the link in the next answer
@291657agA Did you add the console.log() lines of code meanwhile? I clicked through your survey but in the console I did not see the statement I added. I am also not sure if I clicked the right questions and so on from your survey so that the question which includes the JavaScript is executed.
However, you mentioned that you work with simple layout. I would suggest you to test with another one. Simple layout behaves a little bit different. I could imagine that the embedded data name is an issue. Instead of the setEmbeddedData, you must use setJSEmbeddedData in simple layout. The revised code would be:
// Listen for the study sending data window.addEventListener('message', function _labjs_data_handler(event) { // Make sure the event is from lab.js and contains data if (event.data.type === 'labjs.data') { // Log the response details console.log('Data received from lab.js:'); console.log(event.data);
// Convert the JSON object to a string const dataString = JSON.stringify(event.data.json);
// Save the stringified data as embedded data in Qualtrics Qualtrics.SurveyEngine.setJSEmbeddedData('stroop-data', dataString);
// Remove the event listener after data is received window.removeEventListener('message', _labjs_data_handler);
// Trigger the next button to move to the next Qualtrics page page.clickNextButton(); } }); });
Additionally, you would need to rename the embedded data in your survey flow to “__js_” + variable name. In your case __js_stroop-data.
In the JavaScript the prefix should not be added. So it’s correct like this:
// Listen for the study sending data window.addEventListener('message', function _labjs_data_handler(event) { // Make sure the event is from lab.js and contains data if (event.data.type === 'labjs.data') { // Get restult const data = event.data.json;
// Save the stringified data as embedded data in Qualtrics Qualtrics.SurveyEngine.setJSEmbeddedData('stroop-data', data);
// Remove the event listener after data is received window.removeEventListener('message', _labjs_data_handler);
// Trigger the next button to move to the next Qualtrics page page.clickNextButton(); } }); });
Best Christian
@chackbusch no i did not change anything. Unfortunately changing layout didn’t help. How can i use setJSEmbeddedData in simple layout? Do i have to write something additional?
@291657agA Please read my previous comment. I provided some updated code there. Furtermore, make sure to rename the embedded data field in the survey flow.