|
| 1 | +PostHog is available for Roblox through [Wally](https://wally.run) (the Roblox package manager), or as a model file you import in Studio. The SDK is server-authoritative: the server owns all batching and HTTP, and each player is a PostHog user keyed by their Roblox `UserId`. |
| 2 | + |
| 3 | +### Requirements |
| 4 | + |
| 5 | +- HTTP requests enabled for your experience: **Game Settings > Security > Allow HTTP Requests**. Only the Roblox server can make outbound HTTP requests, and only when this is on. |
| 6 | +- A PostHog project API key (starts with `phc_`). |
| 7 | + |
| 8 | +### Via Wally (recommended) |
| 9 | + |
| 10 | +Add the dependency to your `wally.toml`: |
| 11 | + |
| 12 | +```toml |
| 13 | +[dependencies] |
| 14 | +PostHog = "posthog/posthog-roblox@0.1.0" |
| 15 | +``` |
| 16 | + |
| 17 | +Run `wally install`, then map the installed package into `ReplicatedStorage` in your [Rojo](https://rojo.space) project file so it replicates to clients: |
| 18 | + |
| 19 | +```json |
| 20 | +"ReplicatedStorage": { |
| 21 | + "$className": "ReplicatedStorage", |
| 22 | + "PostHog": { "$path": "Packages/PostHog" } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### Via model file |
| 27 | + |
| 28 | +Download the latest `posthog-roblox.rbxm` from the [releases page](https://github.com/PostHog/posthog-roblox/releases). In Studio, right-click `ReplicatedStorage`, choose **Insert from File**, and select the file. Make sure the inserted instance is named `PostHog`. |
| 29 | + |
| 30 | +Either way, you should end up with `ReplicatedStorage > PostHog`, which both server and client code require. |
| 31 | + |
| 32 | +### Initialize on the server |
| 33 | + |
| 34 | +Initialize PostHog once from a server `Script` (for example in `ServerScriptService`): |
| 35 | + |
| 36 | +```lua |
| 37 | +local ReplicatedStorage = game:GetService("ReplicatedStorage") |
| 38 | +local PostHog = require(ReplicatedStorage:WaitForChild("PostHog")) |
| 39 | + |
| 40 | +PostHog:Init({ |
| 41 | + apiKey = "<ph_project_token>", |
| 42 | + host = "<ph_client_api_host>", -- usually https://us.i.posthog.com or https://eu.i.posthog.com |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +> **Note:** `Init` must run on the server. Calling it from a `LocalScript` is ignored. Requiring the same module from a `LocalScript` gives you a thin client relay instead (see [Capturing events](#capturing-events)). |
| 47 | +
|
| 48 | +### Configuration options |
| 49 | + |
| 50 | +Pass any of these to `PostHog:Init`. Only `apiKey` is required; everything else has a sensible default. |
| 51 | + |
| 52 | +```lua |
| 53 | +PostHog:Init({ |
| 54 | + -- Required |
| 55 | + apiKey = "<ph_project_token>", |
| 56 | + |
| 57 | + -- Connection |
| 58 | + host = "https://us.i.posthog.com", |
| 59 | + |
| 60 | + -- Event queue |
| 61 | + flushAt = 20, -- Flush once this many events are queued (default: 20) |
| 62 | + flushIntervalSeconds = 30, -- Flush at least this often (default: 30) |
| 63 | + maxQueueSize = 1000, -- Drop the oldest events past this size (default: 1000) |
| 64 | + maxBatchSize = 50, -- Maximum events per HTTP request (default: 50) |
| 65 | + |
| 66 | + -- Feature flags |
| 67 | + preloadFeatureFlags = true, -- Fetch a player's flags when they join (default: true) |
| 68 | + sendFeatureFlagEvents = true, -- Emit $feature_flag_called when a flag is read (default: true) |
| 69 | + sendDefaultPersonPropertiesForFlags = true, |
| 70 | + |
| 71 | + -- Autocapture and error tracking |
| 72 | + captureLifecycleEvents = true, -- player_joined/left/idle, server_started/shutdown (default: true) |
| 73 | + captureErrors = true, -- Capture unhandled errors as $exception events (default: true) |
| 74 | + errorDebounceSeconds = 1, -- Minimum gap between auto-captured server errors (default: 1) |
| 75 | + |
| 76 | + -- Identity |
| 77 | + personProfiles = "identified_only", -- "identified_only" | "always" | "never" |
| 78 | + serverDistinctId = "server", -- distinct_id used for server-scoped (nil subject) events |
| 79 | + |
| 80 | + -- Client relay |
| 81 | + enableClientRelay = true, -- Create the RemoteEvent that lets clients relay events (default: true) |
| 82 | + clientRateLimitPerSecond = 20, -- Per-player rate limit for relayed messages (default: 20) |
| 83 | + maxClientPropertyCount = 100, -- Maximum properties accepted from one client message (default: 100) |
| 84 | + |
| 85 | + -- Diagnostics |
| 86 | + logLevel = "warn", -- "debug" | "info" | "warn" | "error" | "none" |
| 87 | +}) |
| 88 | +``` |
0 commit comments