Search code examples
javascriptsurveyqualtrics

Embedded data in Qualtrics showing up in the 'Data & Analysis' tab, but is not showing up for participants


So I have a bit of JS in my Qualtrics survey that takes the response to a question, and if that response is not empty, it will embed the participant’s response (to be called later in the survey). If they don’t enter anything, I have made it print a string. I have done this with the following:

Qualtrics.SurveyEngine.addOnUnload(function()
{
    /*Place your JavaScript here to run when the page is unloaded*/
    var tboxID= "QR~"+this.questionId;
    var hometownStr = document.getElementById(tboxID).value;

    Qualtrics.SurveyEngine.setEmbeddedData('hometownStr',hometownStr);

    if(!hometownStr || 0 === hometownStr.length)
    {
        Qualtrics.SurveyEngine.setEmbeddedData('hometownStr', "your hometown"); 
    }
});

After the block this question is in I have embedded the data.

So, let's say I am asked the question (in which the JS is embedded): What is your hometown? I either answer that my hometown is (for example) "Paisley", or I leave it blank. If I don't answer the question, the next question will say This is a person from your hometown. If I do answer the question, it should print: This is a person from Paisley.

However, the latter example does not print "Paisley" (it prints nothing). FYI, the former example works fine.

The strange thing is that, though "Paisley" is not printed in the survey, it comes out in the final data (found in the Data & Analysis tab).

Note: I am calling the embedded data by ${e://Field/hometownStr}.

Does anyone know why this might be happening? Thanks in advance for any help; please tell me I am stupid and overlooking something major, and this isn't a bug in Qualtrics...

FYI, this code has worked in the past, so I don't think it's an issue with the code. I refactored some of the questions and blocks and it just stopped working.


Solution

  • Move the survey flow embedded data block BEFORE the block where the JS sets it.

    EDIT: I noticed you are using addOnUnload. Use addOnPageSubmit instead.