Part 3: Adding Social Information and New Factors

Adding social information

Social information is included in the default Empirica template via SocialExposure.jsx. However, nothing will show if we don't have any other players!
In this example we're going to modify the default behavior so that (a) the social information only shows after participants have entered their initial response, and (b) social information shows only for a participant's network neighbors.

In the admin panel

Return to your admin panel and to the configuration section.
First, create a multi-player game by adding a new factor value to playerCount (following the same procedure as before) with more than one player, then creating a new treatment and a new batch.
You can then launch the Empirica app multiple times in the same web browser by clicking New Player in the header tab. The game will start once the required number of players have entered the lobby.

On the server side

Let's go back to server/main.js
Within each round are multiple stages. We can add more stages by returning to the Empirica.gameInit method and adding the following after the other roud.addStage part.
1
round.addStage({
2
name: "social",
3
displayName: "Social Information",
4
durationInSeconds: 120
5
});
Copied!
So that your server/main.js file looks like this:
1
import Empirica from "meteor/empirica:core";
2
3
import "./callbacks.js";
4
import "./bots.js";
5
6
import { taskData } from "./constants";
7
8
// gameInit is where the structure of a game is defined.
9
// Just before every game starts, once all the players needed are ready, this
10
// function is called with the treatment and the list of players.
11
// You must then add rounds and stages to the game, depending on the treatment
12
// and the players. You can also get/set initial values on your game, players,
13
// rounds and stages (with get/set methods), that will be able to use later in
14
// the game.
15
Empirica.gameInit(game => {
16
// Establish node list
17
const nodes = [];
18
for (let i = 0; i <= game.players.length; i++) {
19
nodes.push(i);
20
}
21
22
game.players.forEach((player, i) => {
23
player.set("avatar", `/avatars/jdenticon/${player._id}`);
24
player.set("score", 0);
25
26
// Give each player a nodeId based on their position
27
player.set("nodeId", i);
28
29
// Assign each node as a neighbor
30
const networkNeighbors = nodes.filter(node => node !== i);
31
player.set("neighbors", networkNeighbors);
32
});
33
34
Object.keys(taskData).forEach(taskName => {
35
const task = taskData[taskName];
36
const round = game.addRound({
37
data: {
38
taskName: taskName,
39
questionText: task.questionText,
40
imagePath: task.path,
41
correctAnswer: task.correctAnswer
42
}
43
});
44
45
round.addStage({
46
name: "response",
47
displayName: "Response",
48
durationInSeconds: 120
49
});
50
51
round.addStage({
52
name: "social",
53
displayName: "Social Information",
54
durationInSeconds: 120
55
});
56
57
})
58
59
});
60
Copied!

In Round.jsx

We can access the stage information (including the name) within the app, so we're going to modify Round.jsx to display the SocialExposure component only when stage.name === "social".
Modify the <SocialExposure {...this.props} /> part with this code:
1
{
2
stage.name === "social" && (
3
<SocialExposure {...this.props} />
4
)
5
}
Copied!
So that your Round.jsx file looks like this:
1
import React from "react";
2
3
import PlayerProfile from "./PlayerProfile.jsx";
4
import SocialExposure from "./SocialExposure.jsx";
5
import Task from "./Task.jsx";
6
7
export default class Round extends React.Component {
8
render() {
9
const { round, stage, player, game } = this.props;
10
11
return (
12
<div className="round">
13
<div className="content">
14
<PlayerProfile player={player} stage={stage} game={game} />
15
<Task game={game} round={round} stage={stage} player={player} />
16
{
17
stage.name === "social" && (
18
<SocialExposure {...this.props} />
19
)
20
}
21
</div>
22
</div>
23
);
24
}
25
}
26
Copied!

In SocialExplosure.jsx

Finally, we'll modify SocialExposure.jsx .
We're going to remove the slider element, since we are not using that in this example, and just show the number:
1
renderSocialInteraction(otherPlayer) {
2
// Get the value or return NA if no value was entered
3
const value = otherPlayer.round.get("value") ?? "NA";
4
return (
5
<div className="alter" key={otherPlayer._id}>
6
<img src={otherPlayer.get("avatar")} className="profile-avatar" />
7
Guess: {value}
8
</div>
9
);
10
}
Copied!
This simple feature shows just how powerful Empirica can be. Just by virtue of storing user information with player.set() and displaying that information in the SocialExposure component, the participant's interface is automatically updated. This is due to the way React.js works with Meteor: any time a piece of information passed as one of the props is updated, then the display re-renders to show the new information.
We also only want to show information for players listed in player.get("neighbors") . Let's do this by replacing the declaration for const otherPlayers with:
1
const otherPlayers = game.players.filter(p =>
2
player.get("neighbors").includes(p.get("nodeId"))
3
);
Copied!
This sets the otherPlayers to players that are not the current player and players that are in the current player's neighbors (their node is in their list of neighbors).
Now, let's add a title to make this component match the other two. And let's make a condition to determine whether the phrase, "There are X other players:", should be plural or not. This paragraph will show different text depending on whether there is only one or more other players.
1
return (
2
<div className="social-exposure">
3
<h3 className="title">Social Information</h3>
4
<p className="title">
5
{
6
otherPlayers.length > 1
7
? <strong>There are {otherPlayers.length} other players:</strong>
8
: <strong>There is one other player:</strong>
9
}
10
</p>
11
{otherPlayers.map(p => this.renderSocialInteraction(p))}
12
</div>
13
);
Copied!
In the end, your SocialExposure.jsx component should look like this:
1
import React from "react";
2
3
export default class SocialExposure extends React.Component {
4
renderSocialInteraction(otherPlayer) {
5
// Get the value or return NA if no value was entered
6
const value = otherPlayer.round.get("value") ?? "NA";
7
return (
8
<div className="alter" key={otherPlayer._id}>
9
<img src={otherPlayer.get("avatar")} className="profile-avatar" />
10
Guess: {value}
11
</div>
12
);
13
}
14
15
render() {
16
const { game, player } = this.props;
17
18
const otherPlayers = game.players.filter(p =>
19
player.get("neighbors").includes(p.get("nodeId"))
20
);
21
22
if (otherPlayers.length === 0) {
23
return null;
24
}
25
26
return (
27
<div className="social-exposure">
28
<h3 className="title">Social Information</h3>
29
<p className="title">
30
{
31
otherPlayers.length > 1
32
? <strong>There are {otherPlayers.length} other players:</strong>
33
: <strong>There is one other player:</strong>
34
}
35
</p>
36
{otherPlayers.map(p => this.renderSocialInteraction(p))}
37
</div>
38
);
39
}
40
}
41
Copied!

Adding treatments and factors

One of the key features of Empirica is that you can think like a scientist and control your app with experimental conditions. To show how this works we'll add a factor, allowing us to modify the stage length without changing the code.
First, we add the new factor through the admin panel by navigating to the factors interface and clicking "New Factor".
We will create a stageLength factor of type Integer. You need to add a little description, but you do not need to specify a minimum or maximum.
Because we are replacing timing of the stages with this factor, you should make this factor Required so that it has to be entered when creating treatment. Otherwise, if you forget to set a stageLength, the experiment will break.
You can also archive your previous treatments that do not have a stageLength, to avoid being tempted to use them and declutter your interface. You'll always be able to unarchive these if necessary.
Now, add a new value to the factor you have created (such as 60, for 60 seconds).
Now create a new treatment where you use the stage length. You can now use this new treatment when you create new batches.
We use this treatment information in the Empirica.gameInit callback. In server/main.js , go to the part where stages are added to the round, and change the durationInSeconds: 120to durationInSeconds: game.treatment.stageLength for both stages.
1
round.addStage({
2
name: "response",
3
displayName: "Response",
4
durationInSeconds: game.treatment.stageLength
5
});
6
7
round.addStage({
8
name: "social",
9
displayName: "Social Information",
10
durationInSeconds: game.treatment.stageLength
11
});
Copied!
If you are trying to edit elements (e.g., change the styling) you might want the stage length to be longer so you have time to work on your changes without creating new games over and over again. Consider creating a stageLength factor that is longer and make a 'test' or 'dev' treatment with this factor that's just for you when you want to do some long edits.

Exporting the Treatments and Factors

The Treatments and Factors that you have created are all saved on your local database. This means that you will no longer have access to them if you share this someone through GitHub, if you reset your app, or if you deploy your app.
You should export these Treatments and Factors so that they can be imported back in if need be.
Go to Configuration on the admin panel (where you create Treatments and Factors) and click the Export button. This will allow you to download a .yaml file of your Treatments and Factors. Add it to the root of your Empirica experiment folder (or somewhere else where it is handy to access). It can be good to rename it to something like factors.yaml.
Now, if you need to recreate your Treatments and Factors, you can just go Configuration on the admin panel, click Import and select your .yaml file.
Last modified 3mo ago