I wanted to record data from my custom Javascript code | XM Community
Solved

I wanted to record data from my custom Javascript code


Hello everyone,

I'm new to Qualtrics, I wanted to save the data in qualtrics from custom javascript code. can anyone tell me how to do that ?

Thanks,
Abhi
icon

Best answer by Michael_Campbell_RedPepper 26 March 2018, 17:46

View original

16 replies

Userlevel 4
Badge +3
Yeah! Save it as an Embedded Data Field! It will show up in reporting and exports.

Here is some API code to get you started!

`Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);`
`Qualtrics.SurveyEngine.getEmbeddedData('FIELD');`
Userlevel 7
Badge +27
Just a note on this, you need to "declare" your embedded data in the survey flow somewhere before your JavaScript executes.
As a beginner it is little tough to understand can you tell me how to do that if you guys have any documentation can you share that with me?
Thanks
Abhi
Userlevel 7
Badge +27
Just add your variable to an embedded data block in the survey flow:
!
> @TomG said:
> Just add your variable to an embedded data block in the survey flow:
> !
>

Thank you
Userlevel 4
Badge +3
For sure!
> @Michael_Campbell_RedPepper said:
> Yeah! Save it as an Embedded Data Field! It will show up in reporting and exports.
>
> Here is some API code to get you started!
>
> `Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);`
> `Qualtrics.SurveyEngine.getEmbeddedData('FIELD');`

thank you
Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value); what is the field over here?
Userlevel 7
Badge +27
FIELD is the name of the Qualtrics embedded variable.
where i need to put
Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);
Qualtrics.SurveyEngine.getEmbeddedData('FIELD');
in my custom javascript code?
Userlevel 7
Badge +27
Since we haven't seen your JavaScript code, it is impossible for use to tell you where to put that line of code. If you post your code, we may be able to help.
> @TomG said:
> Just add your variable to an embedded data block in the survey flow:
> !
>

Just adding a screen grab of what the "Survey Flow" tab looks like:

!
I had found the setEmbeddedData API call, but none of the values pass to it appear in the exported csv file. What might account for this?
Userlevel 7
Badge +27
You have to add the embedded data variables to your survey flow prior to the question where JS sets them.
Badge

Hi everyone! I'm also in a similar position. I'm using Qualtrics to record feedback data. However, I also want to record some metadata (

groupId
) around this. I'm passing the metadata through JavaScript according to the documentations mentioned. Below is my HTML code snippet:




I have included this in the base HTML file. Most of the script was mentioned in the documentation. I've only added the
addOnload
method. However, on my server, it says
Uncaught ReferenceError: Qualtrics is not defined
. Isn't the script supposed to define Qualtrics on its own? Could anyone point out what I'm missing here? Thanks!

Userlevel 1
Badge +1

So if I am following this thread correctly, The Set embed data block should appear before the script executes like this assuming the script executes in Block 8:

I am unclear where to put the script to record the data. This is where I put it:

Qualtrics.SurveyEngine.addOnload(function()
{
    var qid = this.questionId;

    document.onkeydown = function(event) {
        console.log('keydown',event);
        if (event.which == 37) {
            event.preventDefault();
            Qualtrics.SurveyEngine.registry[qid].setChoiceValue(1, true);
            jQuery('#NextButton').click();
        } else if (event.which == 39) {
            event.preventDefault();
            Qualtrics.SurveyEngine.registry[qid].setChoiceValue(2, true);
            jQuery('#NextButton').click();
        }
    }


});

Qualtrics.SurveyEngine.addOnReady(function()
{
    var qobj = this;
    setTimeout(function() { jQuery("#"+qobj.questionId+" .hideAfter45").hide(); },5000);


});

Qualtrics.SurveyEngine.addOnUnload(function()
{
    `Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);`
`Qualtrics.SurveyEngine.getEmbeddedData('FIELD');`

});

 

Is this correct?

I have a series of questions that I want to capture the embedded data. Do I need a embed data block before each question or just one embed data block at the start of the series

Leave a Reply