# Set up BloxTrack in a Roblox game

BloxTrack collects events from your game's servers and shows players, funnels, retention and each player's path at
https://bloxtrack-app.vercel.app. This guide is written for people and for AI coding agents: follow it top to bottom.

**What you need:** your project's **ingest key** (`btk_in_...`). Sign in at https://bloxtrack-app.vercel.app, make a
project for the game, and copy the key it shows. (An agent: ask the person for it. Never invent one.)

## 1. Add the SDK (two files)

| File | Where in the game | Kind |
|---|---|---|
| [`BloxTrack.lua`](https://bloxtrack-app.vercel.app/sdk/BloxTrack.lua) | `ServerScriptService/BloxTrack` | ModuleScript |
| [`BloxTrackClient.lua`](https://bloxtrack-app.vercel.app/sdk/BloxTrackClient.lua) | `ReplicatedStorage/BloxTrackClient` | ModuleScript (optional, recommended) |

With Rojo, save them as `src/server/BloxTrack.lua` and `src/shared/BloxTrackClient.lua` (or wherever your project
maps ServerScriptService and ReplicatedStorage). Without Rojo, make the ModuleScripts in Studio and paste the code in.

Download them with:

```sh
curl -sO https://bloxtrack-app.vercel.app/sdk/BloxTrack.lua
curl -sO https://bloxtrack-app.vercel.app/sdk/BloxTrackClient.lua
```

## 2. Keep the key on the server

The key lets anyone send events to your project, so it must never reach players. Put it only where server code can
read it: a ModuleScript in `ServerScriptService` or `ServerStorage` (never `ReplicatedStorage`, `StarterPlayer`,
`StarterGui` or a LocalScript).

```lua
-- ServerScriptService/BloxTrackKey (ModuleScript)
return "btk_in_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```

If the game's code is in a git repo, add this file to `.gitignore` (and note in the README that a fresh clone must
recreate it).

## 3. Start it

One server Script, at startup:

```lua
-- ServerScriptService/Analytics (Script)
local ServerScriptService = game:GetService("ServerScriptService")
local BloxTrack = require(ServerScriptService.BloxTrack)

BloxTrack.Start({
	key = require(ServerScriptService.BloxTrackKey),
	testPlaceIds = {}, -- PlaceIds of private test copies of the game: their events are filed as "test"
})
```

And one LocalScript, so each session knows the player's device:

```lua
-- StarterPlayer/StarterPlayerScripts/Analytics (LocalScript)
require(game:GetService("ReplicatedStorage"):WaitForChild("BloxTrackClient"))
```

Then turn on **Game Settings > Security > Allow HTTP Requests** (in Studio, for each place). Without it nothing
can be sent; BloxTrack warns in the Output.

That's enough for players, new players, sessions, session length, devices, countries, retention and Latest events.

### Start options

| Option | Default | What it does |
|---|---|---|
| `key` | required | The ingest key. |
| `testPlaceIds` | `{}` | Places whose events are filed under `test`. Studio play tests are always `studio`; everything else is `live`. The dashboard shows each separately. |
| `isNewPlayer` | first session seen | `function(player) -> boolean`: true if the player has no save yet. Retention counts new players only, so give this if the game already has players (otherwise everyone counts as new on their first session after you add BloxTrack). It may yield, e.g. wait for your save to load. |
| `sessionProps` | none | `function(player) -> table`: extra props for `session_start` (level, coins, rebirths). May yield. |
| `clientEvents` | `{}` | Event names LocalScripts may send with `BloxTrackClient.Track`. Anything else from clients is ignored. |
| `autoSessions` | `true` | Send `session_start` and `session_end` for you. |
| `debug` | `false` | Print each send and the API's answer. |

Example with a save system:

```lua
BloxTrack.Start({
	key = require(ServerScriptService.BloxTrackKey),
	testPlaceIds = { 1234567890 },
	isNewPlayer = function(player)
		local data = PlayerData.WaitForData(player) -- your own save module
		return data.FirstJoin == true
	end,
	sessionProps = function(player)
		return { level = player:GetAttribute("Level"), coins = player.leaderstats.Coins.Value }
	end,
	clientEvents = { "menu_open", "settings_changed" },
})
```

## 4. Track what players do

```lua
BloxTrack.Track(player, name, key, value, props)
```

- `name`: what happened, snake_case, up to 40 characters: `purchase`, `level_up`, `quest_done`.
- `key` (optional): what it was about, a short string: the item, level, quest, area, shop.
- `value` (optional): one number worth adding up: coins spent, Robux, seconds, damage.
- `props` (optional): anything else, a small table of strings, numbers and booleans (under 2 KB as JSON).

```lua
BloxTrack.Track(player, "coins_spent", "Speed Coil", 250, { balance = coins })
BloxTrack.Track(player, "level_up", tostring(newLevel))
BloxTrack.Track(player, "quest_done", quest.Id, quest.Reward)
BloxTrack.Track(player, "purchase", "VIP", 199, { kind = "gamepass" }) -- Robux, after the purchase is confirmed
```

The dashboard counts players and events per name, splits any event by key, adds up values, and builds funnels from
names and keys (`level_up:5` means the event `level_up` with key `5`). Good keys make good charts, so read
[What to track](https://bloxtrack-app.vercel.app/docs/what-to-track) before adding many events.

### Steps (tutorials and onboarding)

For an ordered list of steps, declare the list once and send each step as it's reached. The dashboard's funnel then
starts from this list and shows steps nobody has reached yet.

```lua
BloxTrack.DefineSteps("tutorial_step", { "Spawned", "Picked a class", "First kill", "Opened shop", "Bought an item" })

-- later, wherever each happens:
BloxTrack.Step(player, "tutorial_step", "Picked a class")
```

Send each step once per player (the first time). Changing the list in a new version is fine: each version's list is
kept, and the dashboard filters by the version players started on.

### From the client

Only for things the server can't see (a menu opened, a setting changed). List the names in `clientEvents`, then:

```lua
local BloxTrackClient = require(game:GetService("ReplicatedStorage"):WaitForChild("BloxTrackClient"))
BloxTrackClient.Track("menu_open", "Shop")
```

Anything that involves currency, purchases or progress should be tracked on the server, where it really happens:
clients can send anything.

## 5. Check it works

1. Play in Studio (the events are filed under **Studio**).
2. Within about 15 seconds, open the project on https://bloxtrack-app.vercel.app: **Latest events** lists them.
3. In Studio's Output, BloxTrack prints `[BloxTrack] started: env studio` at startup, and warns if a send fails
   (HTTP off, a wrong key, a rejected event and why). `debug = true` prints every send.

To check from a terminal (an agent can do this), make a **read key** (`btk_rd_...`) on the project's settings page:

```sh
curl -s "https://bloxtrack-api.stockideashq.workers.dev/v1/recent?limit=10" -H "Authorization: Bearer btk_rd_..."
```

Studio play tests use your real account, so they show up as your UserId.

## Rules of thumb

- Track on the **server**. Keep the key out of anything players can read.
- Use a handful of well-named events with good keys rather than dozens of one-off names.
- Never put personal information in events: no usernames, display names or chat text. The UserId is enough.
- Don't track every frame or every coin pickup: add them up and send a total (e.g. every minute, or when the round ends).
- Send each step of a funnel the first time it happens; repeats are fine but add nothing to funnels.
- Events are batched and sent every 15 seconds, and on server shutdown. A failed send keeps its events for the next try.

## Limits

- Up to 500 events per request (the SDK sends at most 200) and 1 MB per request.
- `name`: snake_case, starting with a letter, up to 40 characters. `key`: up to 100 characters. `props`: up to 2 KB as
  JSON (bigger ones are stored without props).
- An event's time must be within the last 7 days; otherwise it's filed at the time it arrived.

More: [HTTP API](https://bloxtrack-app.vercel.app/docs/api), for sending from anything other than the SDK and for reading
stats from scripts.
