# Install the SDK

> Add the Luau analytics module to your experience, store your keys as secrets and confirm that events arrive.

This page takes you from an empty place to events arriving in Bloxlytics. It takes about ten minutes. You need a Bloxlytics project for the experience (see [add an experience](https://docs.bloxlytics.app/projects/add-an-experience)) and a role with the **Edit project settings and SDK keys** permission to see the keys.

> **Info:** The **SDK** page of your project generates the snippets below with your project's values filled in. Open it alongside this page: **Bloxlytics → your project → SDK**.

## Install

**1. Allow HTTP requests**

In Studio, open **Game Settings → Security** and turn on **Allow HTTP Requests**. Without it every request the SDK makes fails, and the SDK keeps retrying with backoff.

**2. Add the Analytics module**

On the SDK page, in **Installation**, download **Analytics.luau**. In Studio, insert a **ModuleScript** into **ServerScriptService**, name it `Analytics` and paste the file's contents into it.

The module must stay on the server. Don't put it in ReplicatedStorage or anywhere a client can require it.

If you use Cmdr and want remote commands, also download **CmdrBridge.luau** and add it as a ModuleScript named `CmdrBridge` **inside** `Analytics` (or next to it in ServerScriptService). See [the Cmdr bridge](https://docs.bloxlytics.app/sdk/cmdr-bridge).

**3. Store the keys**

The **Keys** section of the SDK page shows your **Project ID** (the experience's Universe ID), the **API URL**, the **Write key** and the **Bridge key**.

**Experience secrets (recommended)**

In Creator Hub, open your experience and go to **Secrets**. Add a secret named `bloxlytics_write_key` with the write key as its value, restricted to the API domain `api.bloxlytics.app`. If you use the bridge, add `bloxlytics_bridge_key` the same way.

Read them in your boot script with `HttpService:GetSecret`. The SDK takes the `Secret` object as it is and adds it to the request header, so the key never appears as a plain string in your place file.

**Paste in the script**

Paste the keys as strings into a server Script. This is quicker for a first test but the key is then part of your place file, and anyone with edit access to the place can read it. Never put a key in ReplicatedStorage or a LocalScript.

**4. Start the SDK**

Add a **Script** to ServerScriptService (for example `BloxlyticsBoot`) and call `Start` once:

```luau ServerScriptService/BloxlyticsBoot.server.luau
local HttpService = game:GetService("HttpService")
local Analytics = require(game.ServerScriptService.Analytics)

-- Optional: short facts shown on this server's card in Bloxlytics.
Analytics:SetServerTag("mode", "Classic")

Analytics:Start({
	-- No ProjectID needed: it is the Universe ID, read from game.GameId.
	WriteKey = HttpService:GetSecret("bloxlytics_write_key"),
	ApiUrl = "https://api.bloxlytics.app",
	-- TrackInStudio = true, -- send analytics while testing in Studio
})
```

Call `Start` as early as you can, so players already in the server when it runs still get a session. If you call it after players have joined, the SDK starts sessions for them too.

**5. Publish and play**

Publish the place and join a live server. Analytics is off in Studio unless you set `TrackInStudio = true` (see [Studio behaviour](#studio-behaviour)).

> **Done:** Sessions, joins, leaves, server heartbeats and game pass prompts are now tracked without any further code. Next, add [custom events](https://docs.bloxlytics.app/sdk/custom-events), [economy](https://docs.bloxlytics.app/sdk/economy) and [purchases](https://docs.bloxlytics.app/sdk/purchases).

### If `GetSecret` can fail

`HttpService:GetSecret` is a Roblox call, and it raises an error if the secret isn't available to the server (for example, a secret name typed wrongly). That error happens in your script, before the SDK can fail softly. If you'd rather your boot script never stops, wrap it:

```luau ServerScriptService/BloxlyticsBoot.server.luau
local HttpService = game:GetService("HttpService")
local Analytics = require(game.ServerScriptService.Analytics)

local ok, writeKey = pcall(function()
	return HttpService:GetSecret("bloxlytics_write_key")
end)

-- If the secret is missing, WriteKey is nil: Start warns once and stays off.
Analytics:Start({
	WriteKey = if ok then writeKey else nil,
	ApiUrl = "https://api.bloxlytics.app",
})
```

## Check that it works

**1. Watch the debugger**

On the SDK page, the **Debugger** panel refreshes every 10 seconds. Within about 10 seconds of the server starting it should show **Receiving batches**, with **Accepted events** counting up, the **SDK version** (`1.0.0`) and the **Last server**.

**2. Look at realtime**

Open [Realtime](https://docs.bloxlytics.app/analytics/realtime). Your server appears with its player count after its first heartbeat, and joins appear in the live feed.

**3. Read the output if nothing arrives**

Check the server output for lines starting with `[Bloxlytics]`. Every configuration problem is reported there once. Set `Debug = true` in the configuration to also print each send and retry, such as `[Bloxlytics] sent 12 events` or `[Bloxlytics] send failed (0); retrying in 2s`.

You can also ask the module directly from the server console:

```luau
local Analytics = require(game.ServerScriptService.Analytics)
print(Analytics:GetStats()) -- { queued, dropped, enabled, backoff }
```

`enabled = false` means the SDK is off: either the configuration was rejected (look for the warning) or this is Studio without `TrackInStudio`. A growing `queued` with a non-zero `backoff` means sends are failing; see [delivery](https://docs.bloxlytics.app/sdk/delivery).

### Common warnings

| Warning (after `[Bloxlytics] Not started:`) | What to do |
| --- | --- |
| `WriteKey must start with bxw_ (use the write key, not an Open Cloud or bridge key)` | You pasted the bridge key or an Open Cloud key. Copy the **Write key** from the SDK page. |
| `WriteKey must be a string or a Secret from HttpService:GetSecret` | `WriteKey` is missing or `nil`. Check the secret name. |
| `ApiUrl must be your Bloxlytics API URL` | `ApiUrl` is missing or isn't an `http://` or `https://` URL. Use `https://api.bloxlytics.app`. |
| `this place isn't published, so it has no Universe ID yet; …` | Publish the place, or set `ProjectID` to test it unpublished. |
| `ProjectID … isn't this experience's Universe ID (…)` | Remove `ProjectID`: the SDK reads the right one from `game.GameId`. |

If the API rejects batches (for example after you rotate the write key), the module warns `[Bloxlytics] API rejected a batch (401): …` once per status code and the reason is listed under **Recent problems** in the debugger. More fixes are in [troubleshooting](https://docs.bloxlytics.app/help/troubleshooting).

## Studio behaviour

Analytics and the Cmdr bridge behave differently in Studio, on purpose.

**Analytics is off in Studio by default.** `Start` still checks your configuration and returns `true`, but no events are queued or sent, so test sessions don't count towards your numbers. With `Debug = true` it prints `Studio session — tracking disabled (set TrackInStudio = true to test)`. Set `TrackInStudio = true` to test analytics; batches are then marked as coming from Studio and heartbeats carry `studio = true`, so the server is shown as a Studio session.

> **Note:** While analytics is off in Studio the SDK never fetches remote configuration, so `Analytics:GetVariant` always returns `nil` there. Set `TrackInStudio = true` to test [experiments](https://docs.bloxlytics.app/sdk/experiments).

**The Cmdr bridge runs in Studio by default**, so you can test commands before publishing. Studio has no `JobId`, so each Studio session gets its own server key, printed when you start:

```text
[Bloxlytics] Studio session. Target this server from the website with server "studio-1a2b3c4d".
```

Pick that server in Bloxlytics to send it commands. Studio sessions are never chosen automatically for "any server" commands. Set `EnableBridgeInStudio = false` to keep the bridge off in Studio.

**Unpublished places** have no Universe ID (`game.GameId` is `0`), so the SDK can't work out the project on its own and refuses to start. Publish the place, or set `ProjectID` to the experience's Universe ID while you test.

## Updating the SDK

To update, download the new **Analytics.luau** (and **CmdrBridge.luau**) from the SDK page and replace the contents of the existing ModuleScripts. Your boot script doesn't change. The **SDK version** shown in the debugger is the version of the most recent batch received.
