# Feature flags, experiments and events

Feature flags and experiments, event collection, URL redirect and visual experiments.

Last updated: 2026-11-06

[Web version](https://www.longplayai.com/docs/guides/features)

<a id="prepare"></a>

## Prepare experiments and collection

- Create and publish the feature flag definition or experiment in the selected App/environment. Record its feature flag key and environment UUID. A commercial metering Feature UUID does not replace a feature flag key.
- For Customer evaluation and event collection, issue a Customer Token with features:read and events:write on the backend and configure the browser Origin.
- Enable the required behavior-event, experiment-exposure and feature-flag usage collection in that environment. Statistical results additionally need exposure sources, metrics and corresponding events.

- [Customers and sessions](https://www.longplayai.com/docs/guides/customers.md#sessions)

<a id="flags"></a>

## Read feature flags

**flags.ts**

```ts
import { createLongplay } from "@tinkerlandai/longplay-sdk";

const longplay = createLongplay({
  baseUrl: "https://api.longplayai.com",
  scope: { projectId: "<APP_UUID>", environment: "sandbox" },
});

export async function loadFlags(
  customerToken: string,
  customer: { id: string; externalUserId: string | null },
) {
  longplay.identify(customerToken);
  const identity = { id: customer.id, externalUserId: customer.externalUserId };
  const flags = await longplay.features({ attributes: { ...identity, country: "CA" } });
  const enabled = flags.getFeatureValue("new-checkout", false);
  const unsubscribe = flags.subscribe(() => console.log(flags.isOn("new-checkout")));
  longplay.track("Checkout Viewed", { source: "pricing" });
  await longplay.flush();
  return { enabled, stop: () => (unsubscribe(), flags.destroy()) };
}
```

- Pass `id` (the Customer UUID) and `externalUserId` so a user gets the same experiment variation everywhere.
- Local feature flags read these two fields. Remote feature flags read the session's verified Customer, so the SDK leaves them out of the remote evaluation request.
- `setAttributes()` replaces all attributes; keep these two fields on every update.
- Backends can read and evaluate with `getFeatures` or `evaluateFeatures` (`features:read`).
- When `evaluateFeatures` selects a `customerId`, leave `id`, `externalUserId`, `createdAt`, `ageDays` and `subscription` out of `attributes`. The Customer supplies them, and sending one returns `INVALID_REQUEST` naming the field.

<a id="events"></a>

## Track events

> Event recording with an official SDK requires at least `0.10.20`. `POST /api/v1/events` accepts only typed events with `kind` (`custom`, `experiment_exposure` or `feature_usage`); earlier SDKs cannot record events. Direct HTTP clients must implement the current typed request and per-event result contract and do not need an SDK.

`track(name, properties)` records a behavior event and `flush()` sends it. Experiment exposures and feature flag usage are recorded automatically; each kind can be turned on or off per environment in Console.

The example above evaluates new-checkout, tracks Checkout Viewed and calls flush(). Replace the key, event name and attributes with your business definitions. Call stop() and clear the Customer client on unmount or account changes.

<a id="verify"></a>

## Check evaluation and event results

**Results and troubleshooting**

| Observation | Check |
| --- | --- |
| Always receives the default | Check the key, publication environment, targeting attributes and traffic conditions; a default value is not proof of assignment. |
| 401 / 403 | Check the Customer Token, features:read/events:write, Origin and App/environment. |
| Events sent but no statistical result | Distinguish sending from acceptance, then check collection settings, event names, exposure linkage, metric configuration and data volume. A completed flush does not establish a statistical conclusion. |

<a id="page-experiments"></a>

## Website experiments

Load URL redirect and visual experiments on your pages without a credential. Copy the environment UUID from the experiment pages in Console.

**experiments.ts**

```ts
import { createPageExperiments } from "@tinkerlandai/longplay-sdk/page";
import { createVisualExperiments } from "@tinkerlandai/longplay-sdk/visual";

// The environment UUID is public; it selects published experiments and authenticates nothing.
const redirects = createPageExperiments({ environmentId: "<ENVIRONMENT_UUID>", baseUrl: "https://api.longplayai.com" });
const visual = createVisualExperiments({ environmentId: "<ENVIRONMENT_UUID>", baseUrl: "https://api.longplayai.com" });

export async function startExperiments() {
  await Promise.all([redirects.start(), visual.start()]);
}
```

<a id="operations"></a>

## Related operations

- **Feature flags, evaluation and events (3)**:  [getFeatures](https://www.longplayai.com/docs/api/get-features.md); [evaluateFeatures](https://www.longplayai.com/docs/api/evaluate-features.md); [trackEvents](https://www.longplayai.com/docs/api/track-events.md)

- [Paths, credentials and scopes in All operations](https://www.longplayai.com/docs/api/operations.md#features)
