# Track your economy

> Record currency sources, sinks, balances and player-to-player transfers from server code, and configure the currencies Bloxlytics accepts.

Economy tracking answers where currency comes from, where it goes and how much players hold. It powers [Economy](https://docs.bloxlytics.app/analytics/economy) and the **Currency generated** alert. You make one call wherever your server already changes a player's balance.

```luau
Analytics:TrackCurrencyEarned(player, "Coins", 150, "Quests", nil, newBalance)
Analytics:TrackCurrencySpent(player, "Coins", 500, "Eggs", "GoldenEgg", newBalance)
Analytics:TrackBalance(player, "Gems", gems)
Analytics:TrackCurrencyTransfer(player, otherPlayer, "Coins", 100, "Trade")
```

> **Server-authoritative only:** Call these from the server code that actually changes the balance, after it has changed. Never call them from a `RemoteEvent` handler that trusts an amount sent by the client: an exploiter could then report any amount they like, and your economy charts would be meaningless.

## Sources and sinks

```luau
Analytics:TrackCurrencyEarned(player: Player, currency: string, amount: number, source: string?, item: string?, balance: number?)
Analytics:TrackCurrencySpent(player: Player, currency: string, amount: number, sink: string?, item: string?, balance: number?)
```

Both send a `$economy` event. `TrackCurrencyEarned` records a **source** (`flow = "source"`), `TrackCurrencySpent` a **sink** (`flow = "sink"`).

| Argument | Type | Rules |
| --- | --- | --- |
| `player` | `Player` | Required. |
| `currency` | `string` | Non-empty, at most 32 characters. Matched against your configured currencies without regard to case. |
| `amount` | `number` | Greater than 0 and finite. Always positive: the direction comes from which function you call. |
| `source` / `sink` | `string?` | The category, such as `"Quests"`, `"Daily rewards"`, `"Eggs"` or `"Upgrades"`. At most 64 characters. |
| `item` | `string?` | What was bought or earned, such as `"GoldenEgg"`. At most 64 characters. |
| `balance` | `number?` | The player's balance **after** the change. Finite. Optional, but it powers the average and median balance charts. |

> **Warning:** The SDK checks the player, currency and amount, but it doesn't shorten `source`, `sink`, `item` or `reason`. If one of them is longer than 64 characters, the API rejects the whole event and the reason appears in the debugger. Keep categories short and fixed.

Use a small, fixed set of categories. Bloxlytics breaks sources and sinks down by category, so `"Quests"` everywhere is far more useful than one category per quest. Put the detail in `item`.

## Balances

```luau
Analytics:TrackBalance(player: Player, currency: string, balance: number)
```

Sends a `$balance` snapshot: the player's current balance, without a change. Useful when a player joins (after their data loads) or at regular intervals, so balance charts include players who don't earn or spend in a session. `balance` must be a finite number; zero and negative values are allowed. Balance snapshots don't appear in the live event feed.

## Transfers

```luau
Analytics:TrackCurrencyTransfer(from: Player, to: Player | number, currency: string, amount: number, reason: string?)
```

Sends a `$economy_transfer` for currency moving from one player to another, such as a trade or a gift. `to` may be a `Player` or a user id (for example, when the receiver is offline). The event belongs to the sender. `amount` must be greater than 0 and finite, and `reason` at most 64 characters.

A transfer isn't a source or a sink: the currency in circulation doesn't change. If your game takes a fee on trades, also record the fee as a sink.

## When calls are dropped

The economy functions return nothing and never warn about the player or the amount: a call with a missing player, a zero, negative, `NaN` or infinite amount is silently ignored. Other cases:

| Situation | What happens |
| --- | --- |
| `currency` isn't a non-empty string | Dropped, with `Economy: currency must be a non-empty string`. |
| Currency not in the dashboard's list (when the list isn't empty) | **Sent anyway**, with the warning `Currency "<name>" isn't configured in Bloxlytics → SDK settings; events will be rejected`. The API then rejects it, and the debugger shows `Unknown currency "<name>" — add it in SDK settings`. |
| **Economy** turned off in the dashboard | Dropped in game once servers load the change, and rejected by the API in the meantime. |
| SDK not started, or Studio without `TrackInStudio` | Ignored. |

## Configure currencies

In **Bloxlytics → your project → SDK → Configuration**, under **Currencies**, add each currency your game uses, with a **Key** and a **Label**:

- The **Key** is what you pass as `currency` in code, such as `Coins`. Keys may contain letters, numbers, spaces and `_`, start with a letter, are at most 32 characters and must be unique (ignoring case).
- The **Label** is the name shown in charts, such as `Gold coins`. At most 32 characters.
- You can define up to 20 currencies.

While the list is empty, every currency name is accepted. **As soon as you define one currency, economy events for any currency not in the list are rejected.** This keeps typos such as `"coins "` or `"Coin"` out of your charts. The check ignores case, so `coins` matches the key `Coins`.

Servers receive the currency list through [remote configuration](https://docs.bloxlytics.app/sdk/remote-config), within 5 minutes. They use it only to warn you early; the API makes the final decision as events arrive.

## Example

A shop purchase and a quest reward in a typical data module:

```luau ServerScriptService/Economy.server.luau
local Analytics = require(game.ServerScriptService.Analytics)

local balances: { [Player]: number } = {}

local function grantCoins(player: Player, amount: number, source: string)
	balances[player] = (balances[player] or 0) + amount
	Analytics:TrackCurrencyEarned(player, "Coins", amount, source, nil, balances[player])
end

local function trySpendCoins(player: Player, price: number, sink: string, item: string): boolean
	local balance = balances[player] or 0
	if balance < price then
		return false
	end
	balances[player] = balance - price
	Analytics:TrackCurrencySpent(player, "Coins", price, sink, item, balances[player])
	return true
end

-- A quest reward
grantCoins(player, 150, "Quests")

-- Buying an egg
if trySpendCoins(player, 500, "Eggs", "GoldenEgg") then
	-- hatch the egg …
end
```

When a player's data loads, a balance snapshot fills in the picture for players who won't earn or spend this session:

```luau
Analytics:TrackBalance(player, "Coins", balances[player] or 0)
```

## Robux purchases that grant currency

A developer product that grants currency is both a purchase and a currency source. Track the purchase with [`WrapProcessReceipt`](https://docs.bloxlytics.app/sdk/purchases#processreceipt) and the currency with `TrackCurrencyEarned`, using a category such as `"Purchases"`:

```luau
Analytics:TrackCurrencyEarned(player, "Gems", 500, "Purchases", "500 Gems", newGems)
```
