Skip to main content

Saving Trial Data

In the example, we add a label to the page that asks "Is mobile research fun?" and two buttons, "Yes" and "No", as responses. For this example, a trial is each time a user clicks on one of the buttons. The code to save trial data is within the event handler for the buttons:

yesButton.onTapDown(() => {
game.addTrialData("mobile_research_fun", true);
game.addTrialData("trial_index", game.trialIndex);
game.trialComplete();
});

When the user clicks on the "Yes" button:

  • The variable mobile_research_fun is saved in the trial data with the value true (because this is the event handler for the "Yes" button).
  • The variable trial_index is saved in the trial data with the value of the current trial, which is contained in game.trialIndex. Remember, game.trialIndex is a variable that keeps track of the current trial the user is on.
  • The call to game.trialComplete() tells the m2c2kit engine that there is no more data to save for the current trial. This function will automatically increment the game.trialIndex counter by 1.

What do the data look like?

In a real assessment, the data will be saved, usually sent to a server. For the tutorial, the data are printed to the console. Click one of the buttons, and you will see something similar to this:

Trial Data: {"document_uuid":"9d6761b0-5593-4f66-b2aa-6befb8f97d9b", "session_uuid":"a274f6e1-79dc-4925-9468-29b906ac9acb",
"activity_uuid":"c50ddb34-a477-4b23-8456-36aaa23777ea","trial_index":0,"mobile_research_fun":true,
"device_metadata":{"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"devicePixelRatio":1.5,"screen":{"availHeight":1392,"availWidth":2560,"colorDepth":24,"height":1440,
"orientation":{"type":"landscape-primary","angle":0},"pixelDepth":24,"width":2560},
"webGlRenderer":"Google Inc. (NVIDIA), ANGLE (NVIDIA, NVIDIA GeForce RTX 3060 Ti Direct3D11 vs_5_0 ps_5_0, D3D11)"}}

The characters after Trial Data: are the data in JSON. After formatting (you can use a website like https://jsonformatter.org/), the JSON data are easier to view:

{
"document_uuid":"9d6761b0-5593-4f66-b2aa-6befb8f97d9b"
"session_uuid": "a274f6e1-79dc-4925-9468-29b906ac9acb",
"activity_uuid": "c50ddb34-a477-4b23-8456-36aaa23777ea",
"trial_index": 0,
"mobile_research_fun": true,
"device_metadata": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36",
"devicePixelRatio": 1.5,
"screen": {
"availHeight": 1392,
"availWidth": 2560,
"colorDepth": 24,
"height": 1440,
"orientation": {
"type": "landscape-primary",
"angle": 0
},
"pixelDepth": 24,
"width": 2560
},
"webGlRenderer": "Google Inc. (NVIDIA), ANGLE (NVIDIA, NVIDIA GeForce RTX 3060 Ti Direct3D11 vs_5_0 ps_5_0, D3D11)"
}
}

Deciphering the JSON data

  • Lines 5 and 6 are the two variables that we defined in the trial schema: trial_index and mobile_research_fun.

  • Lines 2 through 4 are uuids, or universally unique identifiers. For now, you can ignore these. Just know that these are automatically generated by m2c2kit to keep track of the activity (the assessment), session (a set of activities), and this trial's JSON data (a "document"). These are randomly generated and will be unique each time.

  • Lines 7-23 are information about the device (metadata), automatically collected by m2c2kit. For now, you can ignore these. These are often useful for diagnostic purposes and to know how your users are interacting with your assessment.

Each time you click the button, it is a new trial. In the console, you can see the trial_index variable will increment by 1 each time. The mobile_research_fun variable will be true or false depending on which button you click.

Loading...
Explore:
  • You will get an error if you try to save trial data of the wrong type. For example, this example incorrectly saves the value of mobile_research_fun as a string ("true"), and not a boolean(true): game.addTrialData("mobile_research_fun", "true")
  • Similarly, you will get an error if you try to save trial data for a variable that does not exist in the schema. This example incorrectly tries to add a value for a variable called mobile_research_is_fun, which does not exist: game.addTrialData("mobile_research_is_fun", true)