Recording data from javascript | XM Community
Skip to main content
Hello,



We have a survey where we'd like to record how long people spend on the Qualtrics tab (how long it is active); we've got a video for people to watch, and would like to be able to verify how long they spend watching (vs. surfing).



I found some custom script (pasted below), and know that I need to add some embedded data to record from the script (also below)... but I'm not sure how to name the embedded value or where to put this in the script box, relative to the timing script. I'm also not sure where these codes should go - both in the "onload" part, or elsewhere?



Any help or comments would be much appreciated!



Thank you,

Sarah

:)



----> custom Javascript to track active tab:

{



var count = 0;

var myInterval;

// Active

window.addEventListener('focus', startTimer);



// Inactive

window.addEventListener('blur', stopTimer);



function timerHandler() {

count++;

document.getElementById("seconds").innerHTML = count;

}



// Start timer

function startTimer() {

console.log('focus');

myInterval = window.setInterval(timerHandler, 1000);

}



// Stop timer

function stopTimer() {

window.clearInterval(myInterval);

}

});



---> Embedded data

{

Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);

Qualtrics.SurveyEngine.getEmbeddedData('FIELD');



});
Hi,

I think placing them to the `onload` part will be OK. Importantly, both of them must be in the same part, otherwise the `value` won't be know in the latter one. Personally, I would put it into an Next button onclick event. This way your embedded data will be updated right before the respondent leave the page. So the latter part of the code would look like this:



function edata()

{Qualtrics.SurveyEngine.setEmbeddedData('FIELD', value);}



document.getElementById('NextButton').onclick = function() {edata()}



I think you can choose any name for your embedded field, however I rather wouldn't use uncommon characters as for example &#%?! Importantly, you have to define your embedded field in the survey flow manually, otherwise it doesn't appear in the output.

If it doesn't work, check whether there are some errors in the console log (press F12) or use `alert` function to check values of your variables at particular places of your code.

Good luck with coding!

Leave a Reply