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

Using embedded data to count how many times spacebar is pressed

  • November 8, 2024
  • 12 replies
  • 91 views

Forum|alt.badge.img+1

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:

 

Best answer by Nam Nguyen

@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.

12 replies

Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • November 8, 2024

@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);

});
});

 


Forum|alt.badge.img+1
  • Author
  • November 8, 2024

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 :(


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • November 8, 2024

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?


Forum|alt.badge.img+1
  • Author
  • November 8, 2024

Yes! Anything to change here?


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • November 8, 2024

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
 

 


Forum|alt.badge.img+1
  • Author
  • November 8, 2024

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 :(

 

 


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • November 8, 2024

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


Forum|alt.badge.img+1
  • Author
  • November 8, 2024

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


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • November 9, 2024

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


Forum|alt.badge.img+1
  • Author
  • November 9, 2024

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!


Nam Nguyen
QPN Level 8 ●●●●●●●●
Forum|alt.badge.img+29
  • QPN Level 8 ●●●●●●●●
  • Answer
  • November 9, 2024

@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.


Forum|alt.badge.img+1
  • Author
  • November 11, 2024

Thanks a lot for your help here anyways!