Using embedded data to count how many times spacebar is pressed | XM Community
Skip to main content

Dear community,

Qualtrics-newie here. Within my Qualtrics survey, I included a game for which I want to count the number of times the spacebar is pushed. For some reason , I never manage to get the actual number in the data output. Do you have any idea what I can do to make this work?

 

This is the Java Code I use in the survey, as well as the survey flow:

 

Code:

Qualtrics.SurveyEngine.addOnReady(function() {
    var spacebarCount = 0;

    // Funktion zum Zählen der Leertastenanschläge
    function keydownHandler(event) {
        var code = event.code || event.keyCode;
        if (code === 'Space' || code === 32 || event.key === ' ') {
            spacebarCount++;
        }
    }

    // Event-Listener hinzufügen
    document.addEventListener('keydown', keydownHandler);

    // Speichere die Leertastenanschläge beim Verlassen der Seite
    this.addOnPageSubmit(function() {
        Qualtrics.SurveyEngine.setEmbeddedData("SpacebarClicks", spacebarCount);
    });

    // Event-Listener entfernen beim Entladen der Seite
    this.addOnUnload(function() {
        document.removeEventListener('keydown', keydownHandler);
    });
});

 

 

Survey Flow:

 

@merle The JS template already have onPageUnload,  this.addOnPageSubmit is kinda not a thing.
Your code did count the key press correctly but not saving it after unload the page. Try this one instead

Qualtrics.SurveyEngine.addOnload(function() {
var spacebarCount = 0;

function keydownHandler(event) {
var code = event.code || event.keyCode;
if (code === 'Space' || code === 32 || event.key === ' ') {
spacebarCount++;
Qualtrics.SurveyEngine.setEmbeddedData("SpacebarClicks", spacebarCount);
}
}

document.addEventListener('keydown', keydownHandler);

Qualtrics.SurveyEngine.addOnUnload(function() {
document.removeEventListener('keydown', keydownHandler);

});
});

 


Dear @Nam Nguyen , thanks for your fast reply! I just tried it but unfortunately the output for SpacebarClicks as well as SpacebarClicksNumeric is still empty in the CSV export.

Do you have any other idea what’s wrong here? I’m trying to fix this issue for 3 weeks now and can’t believe such a simple task of measure is so complex to code :(


Dear @Nam Nguyen , thanks for your fast reply! I just tried it but unfortunately the output for SpacebarClicks as well as SpacebarClicksNumeric is still empty in the CSV export.

Do you have any other idea what’s wrong here? I’m trying to fix this issue for 3 weeks now and can’t believe such a simple task of measure is so complex to code :(

@merle Are you using Simple Layouts?


Yes! Anything to change here?


Yes! Anything to change here?

@merle In that case, use this code, just change the setJSEmbeddedData

Qualtrics.SurveyEngine.addOnload(function() {
var spacebarCount = 0;

function keydownHandler(event) {
var code = event.code || event.keyCode;
if (code === 'Space' || code === 32 || event.key === ' ') {
spacebarCount++;
Qualtrics.SurveyEngine.setJSEmbeddedData("SpacebarClicks", spacebarCount);
}
}

document.addEventListener('keydown', keydownHandler);

Qualtrics.SurveyEngine.addOnUnload(function() {
document.removeEventListener('keydown', keydownHandler);

});
});

Name your embedded data like this: __js_SpacebarClicks
 

 


I did it exactly like you said, but for some reason the output is still empty for __js_Space… und SpacebarClickNumeric. Any other idea? Sorry this is so complicated and takes so much effort :(

 

 


I did it exactly like you said, but for some reason the output is still empty for __js_Space… und SpacebarClickNumeric. Any other idea? Sorry this is so complicated and takes so much effort

@merle Did you change the code, it’s now setJSEmbeddedData


Yes I did, I copied the code from your reply and inserted it into the JavaScript for the question


Yes I did, I copied the code from your reply and inserted it into the JavaScript for the question

@merle It’s worked on my side. I don’t know what happened to yours. Export the QSF and let me take a look at it. Also, you don’t need the equation * 1 for the numeric. Just straight up assign it


Dear Nam,

thanks you so much for all your effort on this!!! And sorry, I assume the problem is on my side, I am really new to all of this.

 

Please find attached the text from the QSF doc.

Looking forward to your reply!


@merle Here’s the problem, my code work just fine, but Qualtrics JavaScript can only listen to what’s happening inside the survey. When respondent playing your game on scratch, they are interacting with an iframe that you embedded in, so no JS can track what’s going on in there. You have to find another solution sorry I can’t help you with this.


Thanks a lot for your help here anyways!


Leave a Reply