Building blocks7 / 12
Events
Events are how Stacked knows what players do. Your backend sends them with a server SDK as things happen in your game. One event can update the player's snapshot, move their offers forward, and score points in live ops and on leaderboards, all at once.
Send an event
analytics.levelUp('player-1', { skill_id: 'farming', level_value: 12 });Every method takes the player's id first, then the event's parameters.
- Events are sent in the background. The SDK batches them and sends every two seconds. Call
await analytics.flush()before your process exits. - They are applied within seconds, not at once. Fetch a player's offers again a moment after an event to see its effect.
- Send what happens, not what it is for. An event is not tied to an offer. Whatever is listening for it picks it up: this week's offer, next month's leaderboard.
- Ids you send appear in the dashboard. Items, currencies, skills and contexts seen in events are added to your asset library, ready to pick when you build a condition.
The Go SDK has the same methods, in Go style (analytics.LevelUp).
What each event does
| SDK method | Send it when | Updates on the player | Counts toward |
|---|---|---|---|
signIn | A player starts a session | Username, days in game, login streak | |
signUp | A player is created | Username, sign-up date | |
playerSnapshot | You want to set a player's state in one go | Levels, currencies, memberships, quests, achievements, trust score, entity kind and links | |
levelUp | A skill reaches a level | That level | |
earnCurrency | A player gains currency | The balance | |
spendCurrency | A player spends currency | The balance | spendCurrency |
depositCurrency | Currency comes into your game from outside | The total deposited | depositCurrency |
withdrawCurrency | Currency leaves your game | The total withdrawn | |
gainItem, loseItem | A player gains or loses an item | buyItem, when the item was gained for a currency | |
setField, incField, maxField, minField | A value of your own changes | That dynamic field | dynamicTracker |
delField | A value of your own goes away | Removes the field | |
callContext | A player reaches a moment you named | completionContext, and surfacing | |
addTags, removeTags | You sort players into segments | Tags | |
trustScore | Your own measure of a player's trust changes | Trust score | |
gainAchievement, loseAchievement | An achievement changes | Achievements | |
gainMembership, renewMembership, loseMembership | A subscription or pass changes | Memberships | |
questStart, questProgression, questEnd | A player moves through a quest | Quest completions | |
linkUser, unlinkUser | Two players or entities are linked, such as a pet and its owner | Entity links | |
identifierLink | A player connects an email, a social account or a wallet | Identifiers | |
custom | Anything else worth recording |
The last column names the completion conditions and scoring triggers the event feeds. An event with nothing there still matters: surfacing conditions read the snapshot it keeps up to date.
A custom event is recorded with the player's other events and can be inspected in the dashboard. It does not move offers or scores. To track something of your own, use a dynamic field or a context.
Currency and items
// 100 gold spent at a shop
analytics.spendCurrency('player-1', {
currency_id: 'gold',
currency_amount: 100,
to_kind: 'store',
to_id: 'starter-shop',
});
// A sword bought for 250 gold: counts as buying an item
analytics.gainItem('player-1', {
item_id: 'sword',
item_amount: 1,
from_kind: 'store',
from_id: 'starter-shop',
currency_id: 'gold',
currency_amount: 250,
});from_kindandto_kindsay where the currency or item came from or went:store,marketplace,auction,player, or a word of your own.- A trade between two players (
from_kindandto_kindbothplayer) updates nothing and scores nothing. - Send
in_game_balancewith a currency event to set the balance outright. Without it, the amount is added or taken off.
Dynamic fields
A dynamic field is a named value you keep on a player: boss_kills, best_lap_ms, favourite_class. It can be a number, a text or a boolean.
analytics.setField('player-1', { key: 'favourite_class', value: 'mage' });
analytics.incField('player-1', { key: 'boss_kills', value: 1 });
analytics.minField('player-1', { key: 'best_lap_ms', value: 83450 });| Method | Does |
|---|---|
setField | Sets the value |
incField | Adds to a number |
maxField | Keeps the larger of the stored and the sent number |
minField | Keeps the smaller |
delField | Removes the field |
Dynamic fields are the general-purpose tool. A surfacing condition can read one ("players with 10 boss kills or more"), a completion condition can count changes to one ("kill 5 more bosses"), and a scoring rule can turn one into points.
Contexts
A context is a named moment in your game: open_shop, enter_arena, finish_tutorial.
analytics.callContext('player-1', { context: 'enter_arena' });Calling a context does three things:
- Surfaces offers. An offer with a
surfacingContextcondition surfaces only when your game calls one of its contexts, and a connected game client is told at once (offer_surfaced). On Live, the interval between checks for new offers applies to context calls too. - Completes conditions. A
completionContextcondition counts the call. - Opens gated live ops events. An event gated behind a context starts for the player.
The snapshot in one call
playerSnapshot sets many parts of a player at once. Use it to bring existing players into Stacked, or to correct drift.
analytics.playerSnapshot('player-1', {
username: 'ada',
levels: [{ skill_id: 'farming', level_value: 12 }],
currencies: [{ id: 'gold', balance: 4200 }],
trust_score: 80,
});- Each entry you send is set. Entries you leave out of a list are kept, and an empty list clears it.
- A player with an entity kind needs
entity_kindon every call. Leaving it out makes them a regular player again.