I'm currently trying to get a behavioral task to work with Qualtrics using Javascript. Essentially, the task consists of people pumping a balloon. Each pump brings it closer to popping the balloon, but they get more rewards based on the number of times they pump. They can collect rewards at any time. In total, they repeat pumping balloon and collecting rewards 30 times (30 rounds). I'm interested in how many times they press the pump button in each round.
I was able to find a script online that can be used to display the task in Qualtrics (did not write it myself). I'll add the code to the comments. The only thing that was missing was a way to get Qualtrics to record the data I want. I've looked through forums and found that the following script can be used to get Qualtrics to record data:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Qualtrics.SurveyEngine.setEmbeddedData( 'embeddedDataVariable', value );
Qualtrics.SurveyEngine.getEmbeddedData( 'embeddedDataVariable');
<!-- end snippet -->
I would also have to set and embedded data variable in the survey flow in Qualtrics. I thought that the best way to do this is to have an embedded data variable representing pumps for each round (e.g., BART1 for pumps in the first round, BART2 for pumps in the second round). The code would be set up to have a conditional (if) statement that would record the number of pumps using the above code if the round # matched the embedded data variable I want to record. E.g.:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
} if (round = 1){
Qualtrics.SurveyEngine.setEmbeddedData("BART1", value);
Qualtrics.SurveyEngine.getEmbeddedData("BART1");
} else if (round = 2){
Qualtrics.SurveyEngine.setEmbeddedData("BART2", value);
Qualtrics.SurveyEngine.getEmbeddedData("BART2");
}
<!-- end snippet -->
I have 2 issues that I've been trying to resolve. First, I'm not sure where in the script I'm supposed to put the two Qualtrics lines of code. I've tried putting it at different points in the script (within the pump button function, collect button function, and the continue button function), but none of them seem to work.
The other issue, which is related to the first, is what variable I should be recording (what variable do I input in the value field of the Qualtrics code). I assume I should be putting either the "pumps" or "pumpmeup" variable depending on where in the script the Qualtrics set and add lines of code are, but if you guys have any other suggestions, let me know.
If any of you can help me out with this, I'd really appreciate it! I'm very much a beginner in Javascript (very little experience), so any help is appreciated.
Page 1 / 1
<script>$(document).ready(function() {
var saveThis = 'hidden'; // text fields that saves data should not be shown; can be shown in testing
// initialize values
var round = 0;
var start_size = 150; // start value of widht & height of the image; must correspond to the value that is specified for the #ballon id in style.css
var increase = 2; // number of pixels by which balloon is increased each pump
var size; // start_size incremented by 'increase'
var pumps;
var total = 0; // money that has been earned in total
var rounds_played = 30;
var explode_array = [31, 80, 63, 103, 20, 26, 100, 75, 109, 72, 88, 77, 113, 22, 83, 86, 57, 14, 9, 90, 56, 41, 56, 27, 108, 42, 116, 18, 43, 95];
var maximal_pumps = 128;
var pumpmeup; // number pumps in a given round; is updated each round
var number_pumps = []; // arrays for saving number of pumps
var exploded = []; // array for saving whether ballon has exploded
var explosion; // will an explosion occur? 1 = yes, 0 = no
var last_win = 0; // initialize variable that contains the win of the previous round
// initialize language
var label_press = 'Pump Balloon';
var label_collect = 'Collect Money';
var label_balance = 'Total money collected:';
var label_last = 'Money gained last round:';
var label_currency = ' USD';
var label_header = 'Balloon Pump Round ';
var label_gonext1 = 'Start Next Round';
var label_gonext2 = 'Spiel beenden';
var msg_explosion1 = '<p>The Balloon exploded after ';
var msg_explosion2 = ' pumps. No money will be transfered to the bank. <p>Click below to start the next round.</p>';
var msg_collect1 = '<p>The balloon did not explode!</p><p>The money collected in Round 1 is ';
var msg_collect2 = ' USD.</p><p> The money you collected will be put into the bank</p>';
var msg_end1 = '<p>Damit ist dieser Teil der Studie abgeschlossen. Sie haben im Ballon-Spiel ';
var msg_end2 = ' Taler Gewinn gemacht. </p><p>Klicken Sie auf <i>Weiter</i>, um mit der Studie fortzufahren.</p>';
var err_msg = 'You need to pump the balloon at least once before collecting the money. Please click "Pump Balloon"';
var saveThis = 'hidden'; // text fields that saves data should not be shown; can be shown in testing
// initialize values
var round = 0;
var start_size = 150; // start value of widht & height of the image; must correspond to the value that is specified for the #ballon id in style.css
var increase = 2; // number of pixels by which balloon is increased each pump
var size; // start_size incremented by 'increase'
var pumps;
var total = 0; // money that has been earned in total
var rounds_played = 30;
var explode_array = [31, 80, 63, 103, 20, 26, 100, 75, 109, 72, 88, 77, 113, 22, 83, 86, 57, 14, 9, 90, 56, 41, 56, 27, 108, 42, 116, 18, 43, 95];
var maximal_pumps = 128;
var pumpmeup; // number pumps in a given round; is updated each round
var number_pumps = []; // arrays for saving number of pumps
var exploded = []; // array for saving whether ballon has exploded
var explosion; // will an explosion occur? 1 = yes, 0 = no
var last_win = 0; // initialize variable that contains the win of the previous round
// initialize language
var label_press = 'Pump Balloon';
var label_collect = 'Collect Money';
var label_balance = 'Total money collected:';
var label_last = 'Money gained last round:';
var label_currency = ' USD';
var label_header = 'Balloon Pump Round ';
var label_gonext1 = 'Start Next Round';
var label_gonext2 = 'Spiel beenden';
var msg_explosion1 = '<p>The Balloon exploded after ';
var msg_explosion2 = ' pumps. No money will be transfered to the bank. <p>Click below to start the next round.</p>';
var msg_collect1 = '<p>The balloon did not explode!</p><p>The money collected in Round 1 is ';
var msg_collect2 = ' USD.</p><p> The money you collected will be put into the bank</p>';
var msg_end1 = '<p>Damit ist dieser Teil der Studie abgeschlossen. Sie haben im Ballon-Spiel ';
var msg_end2 = ' Taler Gewinn gemacht. </p><p>Klicken Sie auf <i>Weiter</i>, um mit der Studie fortzufahren.</p>';
var err_msg = 'You need to pump the balloon at least once before collecting the money. Please click "Pump Balloon"';
Rest of code (because I didn't have enough space):
// initialize labels
$('#press').html(label_press);
$('#collect').html(label_collect);
$('#total_term').html(label_balance);
$('#total_value').html(total+label_currency);
$('#last_term').html(label_last);
$('#last_value').html(last_win+label_currency);
// below: create functions that define game functionality
// what happens when a new round starts
var new_round = function() {
console.log(number_pumps);
console.log(exploded);
$('#gonext').hide();
$('#message').hide();
$('#collect').show();
$('#press').show();
round += 1;
size = start_size;
pumps = 0;
$('#ballon').width(size);
$('#ballon').height(size);
$('#ballon').show();
$('#round').html('<h2>'+label_header+round+'<h2>');
};
// what happens when the game ends
var end_game = function() {
$('#total').remove();
$('#collect').remove();
$('#ballon').remove();
$('#press').remove();
$('#gonext').remove();
$('#round').remove();
$('#last_round').remove();
$('#goOn').show();
$('#message').html(msg_end1+total+msg_end2).show();
store_data(); // note: this function needs to be defined properly
};
// Important: this function will have to be replaced to ensure that
// the data is actually sent to _your_ server:
var store_data = function() {
$('#saveThis1').html('<input type='+saveThis+' name ="v_177" value="'+number_pumps+'" />');
$('#saveThis2').html('<input type='+saveThis+' name ="v_178" value="'+exploded+'" />');
$('#saveThis3').html('<input type='+saveThis+' name ="v_577" value="'+total+'" />');
};
// message shown if balloon explodes
var explosion_message = function() {
$('#collect').hide();
$('#press').hide();
$('#message').html(msg_explosion1+pumpmeup+msg_explosion2).show();
};
// message shown if balloon does not explode
var collected_message = function() {
$('#collect').hide();
$('#press').hide();
$('#message').html(msg_collect1+pumpmeup+msg_collect2).show();
};
// animate explosion using jQuery UI explosion
var balloon_explode = function() {
$('#ballon').hide( "explode", {pieces: 48}, 1000 );
// activate this if you have a sound file to play a sound
// when the balloon explodes:
// document.getElementById('explosion_sound').play();
};
// show button that starts next round
var gonext_message = function() {
$('#ballon').hide();
if (round < rounds_played) {
$('#gonext').html(label_gonext1).show();
} else {
$('#gonext').html(label_gonext2).show();
}
};
// add money to bank
var increase_value = function() {
$('#total_value').html(total+label_currency);
};
var show_last = function() {
$('#last_value').html(last_win+label_currency);
};
// button functionalities
// pump button functionality
$('#press').click(function() {
if (pumps >= 0 && pumps < maximal_pumps) { // interacts with the collect function, which sets pumps to -1, making the button temporarily unclickable
explosion = 0; // is set to one if pumping goes beyond explosion point; see below
pumps += 1;
if (pumps < explode_array[round-1]) {
size +=increase;
$('#ballon').width(size);
$('#ballon').height(size);
} else {
last_win = 0;
pumpmeup = pumps;
pumps = -1; // makes pumping button unclickable until new round starts
explosion = 1; // save that balloon has exploded this round
balloon_explode();
exploded.push(explosion); // save whether balloon has exploded or not
number_pumps.push(pumpmeup); // save number of pumps
setTimeout(explosion_message, 1200);
setTimeout(gonext_message, 1200);
setTimeout(show_last, 1200);
}
}
});
// collect button: release pressure and hope for money
$('#collect').click(function() {
if (pumps === 0) {
alert(err_msg);
} else if (pumps > 0) { // only works after at least one pump has been made
exploded.push(explosion); // save whether balloon has exploded or not
// activate this if you have a sound file to play a sound
// when the balloon does not explode:
// document.getElementById('tada_sound').play();
number_pumps.push(pumps); // save number of pumps
pumpmeup = pumps;
pumps = -1; // makes pumping button unclickable until new round starts
$('#ballon').hide();
collected_message();
gonext_message();
total += pumpmeup;
last_win = pumpmeup;
increase_value();
show_last();
});
// click this button to start the next round (or end game when all rounds are played)
$('#gonext').click(function() {
if (round < rounds_played) {
new_round();
} else {
end_game();
}
});
// continue button is shown when the game has ended. This needs to be replaced
// by a function that takes into account on which platform the BART runs (i.e.
// how will the page be submitted?)
$("#goOn").click(function() {
$("form[name=f1]").submit();
});
// start the game!
new_round();
});
</script>
// initialize labels
$('#press').html(label_press);
$('#collect').html(label_collect);
$('#total_term').html(label_balance);
$('#total_value').html(total+label_currency);
$('#last_term').html(label_last);
$('#last_value').html(last_win+label_currency);
// below: create functions that define game functionality
// what happens when a new round starts
var new_round = function() {
console.log(number_pumps);
console.log(exploded);
$('#gonext').hide();
$('#message').hide();
$('#collect').show();
$('#press').show();
round += 1;
size = start_size;
pumps = 0;
$('#ballon').width(size);
$('#ballon').height(size);
$('#ballon').show();
$('#round').html('<h2>'+label_header+round+'<h2>');
};
// what happens when the game ends
var end_game = function() {
$('#total').remove();
$('#collect').remove();
$('#ballon').remove();
$('#press').remove();
$('#gonext').remove();
$('#round').remove();
$('#last_round').remove();
$('#goOn').show();
$('#message').html(msg_end1+total+msg_end2).show();
store_data(); // note: this function needs to be defined properly
};
// Important: this function will have to be replaced to ensure that
// the data is actually sent to _your_ server:
var store_data = function() {
$('#saveThis1').html('<input type='+saveThis+' name ="v_177" value="'+number_pumps+'" />');
$('#saveThis2').html('<input type='+saveThis+' name ="v_178" value="'+exploded+'" />');
$('#saveThis3').html('<input type='+saveThis+' name ="v_577" value="'+total+'" />');
};
// message shown if balloon explodes
var explosion_message = function() {
$('#collect').hide();
$('#press').hide();
$('#message').html(msg_explosion1+pumpmeup+msg_explosion2).show();
};
// message shown if balloon does not explode
var collected_message = function() {
$('#collect').hide();
$('#press').hide();
$('#message').html(msg_collect1+pumpmeup+msg_collect2).show();
};
// animate explosion using jQuery UI explosion
var balloon_explode = function() {
$('#ballon').hide( "explode", {pieces: 48}, 1000 );
// activate this if you have a sound file to play a sound
// when the balloon explodes:
// document.getElementById('explosion_sound').play();
};
// show button that starts next round
var gonext_message = function() {
$('#ballon').hide();
if (round < rounds_played) {
$('#gonext').html(label_gonext1).show();
} else {
$('#gonext').html(label_gonext2).show();
}
};
// add money to bank
var increase_value = function() {
$('#total_value').html(total+label_currency);
};
var show_last = function() {
$('#last_value').html(last_win+label_currency);
};
// button functionalities
// pump button functionality
$('#press').click(function() {
if (pumps >= 0 && pumps < maximal_pumps) { // interacts with the collect function, which sets pumps to -1, making the button temporarily unclickable
explosion = 0; // is set to one if pumping goes beyond explosion point; see below
pumps += 1;
if (pumps < explode_array[round-1]) {
size +=increase;
$('#ballon').width(size);
$('#ballon').height(size);
} else {
last_win = 0;
pumpmeup = pumps;
pumps = -1; // makes pumping button unclickable until new round starts
explosion = 1; // save that balloon has exploded this round
balloon_explode();
exploded.push(explosion); // save whether balloon has exploded or not
number_pumps.push(pumpmeup); // save number of pumps
setTimeout(explosion_message, 1200);
setTimeout(gonext_message, 1200);
setTimeout(show_last, 1200);
}
}
});
// collect button: release pressure and hope for money
$('#collect').click(function() {
if (pumps === 0) {
alert(err_msg);
} else if (pumps > 0) { // only works after at least one pump has been made
exploded.push(explosion); // save whether balloon has exploded or not
// activate this if you have a sound file to play a sound
// when the balloon does not explode:
// document.getElementById('tada_sound').play();
number_pumps.push(pumps); // save number of pumps
pumpmeup = pumps;
pumps = -1; // makes pumping button unclickable until new round starts
$('#ballon').hide();
collected_message();
gonext_message();
total += pumpmeup;
last_win = pumpmeup;
increase_value();
show_last();
});
// click this button to start the next round (or end game when all rounds are played)
$('#gonext').click(function() {
if (round < rounds_played) {
new_round();
} else {
end_game();
}
});
// continue button is shown when the game has ended. This needs to be replaced
// by a function that takes into account on which platform the BART runs (i.e.
// how will the page be submitted?)
$("#goOn").click(function() {
$("form[name=f1]").submit();
});
// start the game!
new_round();
});
</script>
Hey @newtothis! For a question involving this much custom code, we would recommend reaching out to an external consultant! They will best be suited to review your code as needed and provide the appropriate suggestions!
Hello! I was wondering if you had made any progress in resolving this issue? I am also doing a study that uses the BART program, and am having a similar issue where I am only able to record the data from the first balloon and none of the data from the 29 subsequent balloons are being recorded. I am also very new to javascript, so I was wondering if you had discovered anything that may help guide my search.
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.