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

TypeScript
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 methodSend it whenUpdates on the playerCounts toward
signInA player starts a sessionUsername, days in game, login streak
signUpA player is createdUsername, sign-up date
playerSnapshotYou want to set a player's state in one goLevels, currencies, memberships, quests, achievements, trust score, entity kind and links
levelUpA skill reaches a levelThat level
earnCurrencyA player gains currencyThe balance
spendCurrencyA player spends currencyThe balancespendCurrency
depositCurrencyCurrency comes into your game from outsideThe total depositeddepositCurrency
withdrawCurrencyCurrency leaves your gameThe total withdrawn
gainItem, loseItemA player gains or loses an itembuyItem, when the item was gained for a currency
setField, incField, maxField, minFieldA value of your own changesThat dynamic fielddynamicTracker
delFieldA value of your own goes awayRemoves the field
callContextA player reaches a moment you namedcompletionContext, and surfacing
addTags, removeTagsYou sort players into segmentsTags
trustScoreYour own measure of a player's trust changesTrust score
gainAchievement, loseAchievementAn achievement changesAchievements
gainMembership, renewMembership, loseMembershipA subscription or pass changesMemberships
questStart, questProgression, questEndA player moves through a questQuest completions
linkUser, unlinkUserTwo players or entities are linked, such as a pet and its ownerEntity links
identifierLinkA player connects an email, a social account or a walletIdentifiers
customAnything 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

TypeScript
// 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_kind and to_kind say 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_kind and to_kind both player) updates nothing and scores nothing.
  • Send in_game_balance with 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.

TypeScript
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 });
MethodDoes
setFieldSets the value
incFieldAdds to a number
maxFieldKeeps the larger of the stored and the sent number
minFieldKeeps the smaller
delFieldRemoves 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.

TypeScript
analytics.callContext('player-1', { context: 'enter_arena' });

Calling a context does three things:

  • Surfaces offers. An offer with a surfacingContext condition 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 completionContext condition 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.

TypeScript
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_kind on every call. Leaving it out makes them a regular player again.