Setting Embedded Data | XM Community
Skip to main content

Hi. I'm trying to get respondents to enter in their ID but only wish to store a SHA-256 hash of the ID. Hence I've created a HTML form input and save it to embedded data. I've been successful setting embedded data in addOnload or addOnReady but when setting the embedded data in a promise, the data will not be stored even when attempted to getEmbeddedData retrieves the right value. Any advise?
Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/

    function hex (buff) {
        return ].map.call(new Uint8Array(buff), b => ('00' + b.toString(16)).slice(-2)).join('');
    }

    Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
    {
        if(type == "next")
        {
            var userID = document.getElementById('QNRIC').value;
            window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(userID))
                .then((val)=> {
                    //alert(hex(val))
                    Qualtrics.SurveyEngine.setEmbeddedData("hashNRIC",hex(val));
                    alert(Qualtrics.SurveyEngine.getEmbeddedData("hashNRIC"));
            });
        }
    });

Hi LQT Try this updated code
Qualtrics.SurveyEngine.addOnPageSubmit(function (type) {
    var $this = jQuery(this.questionContainer);
    if (type == "next") {


        function hex(buff) {
            return ].map.call(new Uint8Array(buff), b => ('00' + b.toString(16)).slice(-2)).join('');
        }


        var userID = jQuery(".QuestionBody inputyid]", $this).val();
        window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(userID))
                .then((val) => {
                    Qualtrics.SurveyEngine.setEmbeddedData("hashNRIC", hex(val));
                });
    }
});


Hi ChiragK . Thanks for your suggestion, unfortunately it doesn't address the problem and it can't retrieve user input. My code is able to retrieve user input from the textbox, was even able to set and get embedded data correctly (thru alert checks) but when i go to my survey embedded data column "hashNRIC", nothing is captured.
Providing my question block below.
Please Enter Your NRIC (HTML Version)

 



Hi LQT My bad, try following code and make sure you have created embedded data variable "hashNRIC" in survey flow above question block.
Qualtrics.SurveyEngine.addOnPageSubmit(function (type) {
    if (type == "next") {

        function hex(buff) {
            return ].map.call(new Uint8Array(buff), b => ('00' + b.toString(16)).slice(-2)).join('');
        }

        var userID = jQuery("#QNRIC").val();
        window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(userID))
                .then((val) => {
                    Qualtrics.SurveyEngine.setEmbeddedData("hashNRIC", hex(val));
                });
    }
});


Hmm, still doesn't work. My embedded data field is working as i'm able to set it in other areas like addOnLoad etc. Could be a problem with accessibility of Qualtrics.SurveyEngine in Promises callbacks. Thanks anyway ChiragK , if you think of any other ideas to try, let me know! :)


Since you are using it on Page Submit, I think the request to the server is being sent before the promise resolves and hence the embedded data field is empty.
I suggest triggering it on Next Button click to see if it works.


Leave a Reply