API
Elements on this page refer to the file structure, concepts, and life cycle of an Empirica experiment.
This document describes Empirica's server, client and shared APIs.

Server

Empirica.gameInit(callback)

The gameInit callback is called just before a game starts, when all players are ready, and it must create rounds and stages for the game.
One (and one only) gameInit callback is required for Empirica to work.
The callback receives one argument, the game object, which gives access to the players and the treatment for this game.
It also offers the addRound() method, which allows to add a round to the game. The returned Round object will implement the addStage(stageArgs) method, which allows to add a Stage to the Round. The stageArgs object to be passed to the stage creation method must contain:
  • name: the name used to identify this stage in the UI code
  • displayName: which will be showed to the UI to players
  • durationInSeconds: the stage duration, in seconds
Note that the Game has not yet been created when the callback is called, and you do not have access to the other properties of the Game which will be created subsequently.

Example

1
Empirica.gameInit(game => {
2
game.players.forEach((player, i) => {
3
player.set("avatar", `/avatars/jdenticon/${player._id}`);
4
player.set("score", 0);
5
});
6
​
7
_.times(10, i => {
8
const round = game.addRound();
9
round.addStage({
10
name: "response",
11
displayName: "Response",
12
durationInSeconds: 120
13
});
14
​
15
if (game.treatment.playerCount > 1) {
16
round.addStage({
17
name: "response",
18
displayName: "Response",
19
durationInSeconds: 120
20
});
21
}
22
});
23
});
Copied!

Game Callbacks

Game hooks are optional methods attached to various events throughout the game life cycle to update data on the server-side.
Contrary to client side data updates, sever-side updates are synchronous, there is no risk of conflicting updates, and important calculations can be taken at precise points along the game.

Empirica.onGameStart(callback)

onGameStart is triggered once per game, before the game starts, and before the first onRoundStart. It receives the game object. Contrary to gameInit, the Game has been created at this point.

Example

1
Empirica.onGameStart(game => {
2
if (game.treatment.myFactor === "fourtytwo") {
3
game.set("maxScore", 100);
4
} else {
5
game.set("maxScore", 0);
6
}
7
});
Copied!

Empirica.onRoundStart(callback)

onRoundStart is triggered before each round starts, and before onStageStart. It receives the same options as onGameStart, and the round that is starting.

Example

1
Empirica.onRoundStart((game, round) => {
2
round.set("scoreToReach", game.get("maxScore"));
3
});
Copied!

Empirica.onStageStart(callback)

onRoundStart is triggered before each stage starts. It receives the same options as onRoundStart, and the stage that is starting.

Example

1
Empirica.onStageStart((game, round, stage) => {
2
stage.set("randomColor", myRandomColorGenerator());
3
});
Copied!

Empirica.onStageEnd(callback)

onStageEnd is triggered after each stage. It receives the current game, the current round, and stage that just ended.

Example

1
Empirica.onStageEnd((game, round, stage) => {
2
stage.set("scoreGroup", stage.get("score") > 10 ? "great" : "not_great");
3
});
Copied!

Empirica.onRoundEnd(callback)

onRoundEnd is triggered after each round. It receives the current game, and the round that just ended.

Example

1
Empirica.onRoundEnd((game, round) => {
2
let maxScore = 0;
3
game.players.forEach(player => {
4
const playerScore = player.round.get("score") || 0;
5
if (playerScore > maxScore) {
6
maxScore = playerScore;
7
}
8
});
9
round.set("maxScore", maxScore);
10
});
Copied!

Empirica.onGameEnd(callback)

onGameEnd is triggered when the game ends. It receives the game that just ended.

Example

1
Empirica.onGameEnd(game => {
2
let maxScore = 0;
3
game.rounds.forEach(round => {
4
const roundMaxScore = round.get("maxScore") || 0;
5
if (roundMaxScore > maxScore) {
6
maxScore = roundMaxScore;
7
}
8
});
9
game.set("maxScore", maxScore);
10
});
Copied!

Change Callbacks

​onSet, onAppend and onChange are called on every single update made by all players in each game, so they can rapidly become computationally expensive and have the potential to seriously slow down the app. Use wisely.
It is very useful to be able to react to each update a user makes. Try nontheless to limit the amount of computations and database saves done in these callbacks. You can also try to limit the amount of calls to set() and append() you make (avoid calling them on a continuous drag of a slider for example) and inside these callbacks use the key argument at the very beginning of the callback to filter out which keys your need to run logic against.
If you are not using these callbacks, comment them out, so the system does not call them for nothing.

Empirica.onSet(callback)

onSet is called when the experiment code call the .set() method on games, rounds, stages, players, playerRounds or playerStages.

Example

1
Empirica.onSet((
2
game,
3
round,
4
stage,
5
player, // Player who made the change
6
target, // Object on which the change was made (eg. player.set() => player)
7
targetType, // Type of object on which the change was made (eg. player.set() => "player")
8
key, // Key of changed value (e.g. player.set("score", 1) => "score")
9
value, // New value
10
prevValue // Previous value
11
) => {
12
// Example filtering
13
if (key !== "value") {
14
return;
15
}
16
​
17
// Do some important calculation
18
});
Copied!

Empirica.onAppend(callback)

onSet is called when the experiment code call the .append() method on games, rounds, stages, players, playerRounds or playerStages.

Example

1
Empirica.onAppend((
2
game,
3
round,
4
stage,
5
player, // Player who made the change
6
target, // Object on which the change was made (eg. player.set() => player)
7
targetType, // Type of object on which the change was made (eg. player.set() => "player")
8
key, // Key of changed value (e.g. player.set("score", 1) => "score")
9
value, // New value
10
prevValue // Previous value
11
) => {
12
// Note: `value` is the single last value (e.g 0.2), while `prevValue` will
13
// be an array of the previsous values (e.g. [0.3, 0.4, 0.65]).
14
});
Copied!

Empirica.onChange(callback)

onChange is called when the experiment code call the .set() or the .append() method on games, rounds, stages, players, playerRounds or playerStages.
onChange is useful to run server-side logic for any user interaction. Note the extra isAppend boolean that will allow to differenciate sets and appends.

Example

1
Empirica.onChange((
2
game,
3
round,
4
stage,
5
player, // Player who made the change
6
target, // Object on which the change was made (eg. player.set() => player)
7
targetType, // Type of object on which the change was made (eg. player.set() => "player")
8
key, // Key of changed value (e.g. player.set("score", 1) => "score")
9
value, // New value
10
prevValue, // Previous value
11
isAppend // True if the change was an append, false if it was a set
12
) => {
13
Game.set("lastChangeAt", new Date().toString());
14
});
Copied!

Empirica.onSubmit(callback)

onSubmit is called when the experiment code call the .submit() on a Stage.
Note that onSubmit is only called if .submit() is explicitely called on the Stage object. Players for which the stage times out naturally, onSubmit will not be triggered.

Example

1
Empirica.onSubmit((game, round, stage, player) => {
2
stage.set("lastSubmitAt", new Date().toString());
3
});
Copied!

Adding Bots

Adding bots to a game is as simple as defining a few callbacks. You can add different bots with different behaviors.

Empirica.bot(name, configuration)

The bot method allows to add a bot with name (e.g. "Alice"), while the configuration is a set of callbacks that will allow to configure how the bot is suppose to react in certain conditions.
The configuration has the follows callbacks:
  • onStageTick: called during each stage at 1 second interval
  • onStageStart: CURRENTLY NOT SUPPORTED called at the beginning of each
    stage (after onRoundStart/onStageStart)
  • onStageEnd: CURRENTLY NOT SUPPORTED called at the end of each stage
    (after onStageEnd, before onRoundEnd if it's the enf of the round)
  • onPlayerChange: CURRENTLY NOT SUPPORTED called each time any (human)
    player has changed a value
All callbacks are called with the following arguments:
  • bot: is the Player object representing this bot
  • game: the current Game​
  • round: the current Round​
  • stage: the current Stage​
  • secondsRemaining: the number of remaining seconds in the stage

Example

1
Empirica.bot("bob", {
2
onStageTick(bot, game, round, stage, secondsRemaining) {
3
let score = 0;
4
game.players.forEach(player => {
5
if (player === bot) {
6
return;
7
}
8
const playerScore = player.get("score");
9
if (playerScore > score) {
10
score = playerScore
11
}
12
});
13
bot.set("score", score+1);
14
}
15
};)
Copied!

Client

Empirica.round(Component)

Set the Round Component that will contain all of the UI logic for your game.

Props

Component will receive the following props:
Prop
Type
Description
game
​Game​
The current game.
player
​Player​
The current player.
round
​Round​
The current round.
stage
​Stage​
The current stage.

Example

1
const Round = ({ player, game, round, stage }) => (
2
<div className="round">
3
<div className="profile">{player.id}: {player.get("score")}</p>
4
<div className="stimulus">{stage.get("somePieceOfData...")</p>
5
// ... Add round logic here. This is not a good example, we recommend you
6
// take a look a Tutorial or a Demo app for better examples.
7
</div>
8
);
9
Empirica.round(Round);
Copied!

Empirica.consent(Component)

Optionally set the Consent Component you want to present players before they are allowed to register.

Props

Component will receive the following props:
Prop
Type
Description
onConsent
Function
A function to call when the user has given consent (usually, clicked a "I consent" button).

Example

1
const Consent = ({ onConsent }) => (
2
<div className="consent">
3
<p>This experiment is part of...</p>
4
<p>
5
<button onClick={onConsent}>I CONSENT</button>
6
</p>
7
</div>
8
);
9
Empirica.consent(Consent);
Copied!

Empirica.introSteps(callback)

Set the intro steps to present to the user after consent and registration, and before they are allowed into the Lobby. These steps might be instructions, a quiz/test, a survey... You may present the steps in multiple pages.
The introSteps callback should return an array of 0 or more React Components to show the user in order.

Props

Component will receive the following props:
Prop
Type
Description
game
​Game​
The current game.
player
​Player​
The current player.
N.B.: The game given here only has the treatment field defined as the game has not yet been created.

Example

1
Empirica.introSteps((game, player) => {
2
const steps = [InstructionStepOne];
3
if (game.treatment.playerCount > 1) {
4
steps.push(InstructionStepTwo);
5
}
6
steps.push(Quiz);
7
return steps;
8
});
Copied!
N.B.: InstructionStepOne or Quiz, in this example, are components that are not implemented in this example, they are simply React Components.

Empirica.exitSteps(callback)

Set the exit steps to present to the user after the game has finished successfully (all rounds finished) or not (lobby timeout, cancelled game,...)
The exitSteps callback should return an array of 1 or more React Components to show the user in order.

Props

Component will receive the following props:
Prop
Type
Description
game
​Game​
The current game.
player
​Player​
The current player.

Example

1
Empirica.exitSteps((game, player) => {
2
if (player.exitStatus !== "finished") {
3
return [Sorry];
4
}
5
return [ExitSurvey, Thanks];
6
});
Copied!
N.B.: ExitSurvey or Thanks, in this example, are components that are not implemented in this example, they are simply React Components.

Empirica.lobby(Component)

Optionally set the Lobby Component to replace the default lobby.

Props

Component will receive the following props:
Prop
Type
Description
gameLobby
​GameLobby​
The current game lobby.
player
​Player​
The current player.

Example

1
const Lobby = ({ player, gameLobby }) => (
2
<header className="lobby">
3
<h1>Please wait until the game is ready...</h1>
4
<p>
5
There are {gameLobby.readyCount} players ready out of{" "}
6
{gameLobby.treatment.playerCount} expected total.
7
</p>
8
</header>
9
);
10
Empirica.lobby(Lobby);
Copied!

Empirica.header(Component)

Optionally set the Header Component to replace the default app header.

Props

Component will NOT receive any props.

Example

1
const Header = () => (
2
<header className="app-header">
3
<img src="/my-logo.png" />
4
<h1>My Experiment</h1>
5
</header>
6
);
7
Empirica.header(Header);
Copied!

Empirica.breadcrumb(Component)

Optionally set the Breadcrumb Component to replace the default Round/Stage progress indicator. This is the UI that shows which are the current Round and Stage, between the page Header and the Round

Props

Component will receive the following props:
Prop
Type
Description
game
​Game​
The current game.
player
​Player​
The current player.
round
​Round​
The current round.
stage
​Stage​
The current stage.

Example

1
const Breadcrumb = ({ round, stage }) => (
2
<ul className="breadcrumb">
3
<li>Round {round.index + 1}</li>
4
{round.stages.map(s => (
5
<li key={s.name} className={s.name === stage.name ? "current" : ""}>
6
{s.displayName}
7
</li>
8
))}
9
</ul>
10
);
11
Empirica.breadcrumb(Breadcrumb);
Copied!

Empirica.routes()

routes are the entry point for the Empirica app. It is required to be used as part of the React render tree for Empirica to work properly and the example below usually does not need changing, other than the HTML node to attach to (document.getElementById("app") here).
N.B.: This must be called after any other Empirica calls (Empirica.round(), Empirica.introSteps(), ...).

Example

1
Meteor.startup(() => {
2
render(Empirica.routes(), document.getElementById("app"));
3
});
Copied!

Shared

Game object

Property
Type
Description
index
Number
An auto-increment number assigned to each Game in order (1, 2, 3...)
treatment
Object (key: String, value: String or Integer)
An object representing the Factors set on this game, e.g. { "playerCount": 12 }.
players
Array of Player objects​
Players participating in this Game.
rounds
Array of Round objects​
On the server side, this will show every round that makes up the game. But on the client side, this will only show the current round.
createdAt
Date
Time at which the game was created which corresponds approximately to the time at which the Game started.

Round object

Property
Type
Description
index
Object
The 0 based position of the current round in the ordered list of rounds in a game.
stages
Array of Stage objects​
Stages composing this Round.

Stage object

Property
Type
Description
index
Object
The 0 based position of the current stage in the ordered list of a all of the game's stages.
name
String
Programmatic name of stage (i.e. to be used in code, e.g if (name === "outcome") ...).
displayName
String
Human name of stage (i.e. to be showed to the Player, e.g "Round Outcome").
durationInSeconds
Integer
The stage duration, in seconds.
startTimeAt
Date
Time at which the stage started. (only set if stage has already started, i.e. not set in onStageStart).

Player object

Property
Type
Description
index
Number
An auto-increment number assigned to each Player in order (1, 2, 3...)
id
String
The ID the player used to register (e.g. MTurk ID).
urlParams
Object (key/value: String)
Parameters that were set on the URL when the user registered.
bot
String
Name of the bot used for this player, if the player is a bot (e.g. Alice).
readyAt
Date
Time at witch the player became ready (done with intro steps).
exitAt
Date
Time when the player exited the Game (whether the game ended normally or not, see exitStatus).
exitStatus
String
Status of the Player at Game exit. Can be: "gameFull", "gameCancelled", "gameLobbyTimedOut", "playerEndedLobbyWait", "playerLobbyTimedOut", "finished". "finished" represent the normal exit.
online
Boolean
True if the player is currently online.
idle
Boolean
True if the player is currently online but idle. Idleness is defined as either the page not being active (on another tab/window) or not detecting any activity (mouse/keyboard) for more than 60s.
lastActivityAt
Date
Time when the player was last seen online and active (not idle). Server only (this is not accessible on the client at the moment).
lastLogin.at
Date
Time the player last come online (registered, reopened page and auto-login kicked in or re-entered player ID – if they were forgotten).
lastLogin.ip
String
​IP address of player on last connection.
lastLogin.userAgent
String
​User-Agent of player on last connection.

GameLobby object

Property
Type
Description
treatment
Object (key: String, value: String or Integer)
An object representing the Factors set on this game, e.g. { "playerCount": 12 }.
queuedCount
Integer
Total number of players queued for this game, including ready players and players currently going through the intro steps. N.B.: There could be more players in queuedCount than specified by the playerCount Factor, as Empirica can sometimes overbook Games to shorten wait times. Use gameLobby.treatment.playerCount to get the expected number of players.
readyCount
Integer
Number of players ready to play. They have completed the intro steps, and they are on the lobby page.
Last modified 6mo ago