# Best practices

> A production checklist for AppHelm - keys, consent, identity, event names, sampling, releases and source maps, environments, alerts, flags, personal data, browser performance, shutdown and testing.

Source: https://apphelm.dev/docs/best-practices

Use this page as a checklist before you ship AppHelm to production, and again when you add a platform. Each item links to the page that explains it in full.

## Checklist

| Area | Do this | Read |
| --- | --- | --- |
| Keys | Browser key in browsers, `events:write`-only key in apps, server keys in server env only | [Keys](#keys) |
| Consent | Wire `setConsent` to your banner; ask separately for replay | [Consent](#consent) |
| Identity | `identify` with your internal user id at sign-in, `reset` at sign-out | [Identity](#identity) |
| Events | snake_case names for things that happened, properties for detail | [Event names](#event-names) |
| Sampling | Set a trace rate; lower replay's rate and keep replay on error | [Sampling](#sampling) |
| Releases | The same `release` in the SDK, the source map upload and the deploy | [Releases, source maps and symbols](#releases-source-maps-and-symbols) |
| Environments | Set `environment` everywhere, and pass it to flags if you use environment rollouts | [Environments](#environments) |
| Alerts | An alert for new and regressed issues, sent where your team looks | [Alerts](#alerts) |
| Flags | Off path first, check once, archive when done | [Feature flags](#feature-flags) |
| Personal data | No emails, names or free text in properties; mask replay | [Personal data](#personal-data) |
| Browser performance | One client per page; let optional modules load lazily | [Browser performance](#browser-performance) |
| Servers | Flush or close before the process exits | [Shutdown](#shutdown) |
| Testing | `enabled: false` in tests, a staging environment, Check connection after each new platform | [Testing and staging](#testing-and-staging) |

## Keys

- **Use the key each platform is meant for.** Browser code gets the browser key (`ahl_pub_…`). Mobile apps get a server key with `events:write` only, for that product only; it ships in the binary, so treat it as public. Servers get a server key from an environment variable or secret manager.
- **One key per service.** Separate keys let you rotate one service without touching the others, and the key list's **last used** time tells you which are still needed.
- **Grant only what the code needs.** Sending data needs `events:write`. Local flag evaluation needs `products:read`, which can read flag salts, so keep such keys on servers. Give agents and scripts their own narrow keys.
- **Plan rotation.** A workspace holds 10 active server keys, so keep a free slot; mobile keys stay in old app versions until users update.
- **Give each origin its own browser key**, including staging and local development, since origins match exactly.

See [Keys, scopes and environments](https://apphelm.dev/docs/keys-and-environments).

## Consent

- **Call `setConsent` from your consent banner.** The browser SDK records and stores nothing until analytics consent is given, so a site without this call sends nothing.
- **Pass a stored choice at start-up** as the `consent` option, so returning visitors' first page view is recorded.
- **Ask for replay separately.** Replay records only with replay consent, which needs analytics consent too.
- **Respect withdrawal.** `setConsent({ analytics: false })` drops queued events and stops autocapture, error capture and replay. Visitors whose browser sends Global Privacy Control are never recorded.
- **Mobile apps** record by default: if your app needs consent first, start opted out (`defaultOptIn: false`, `defaultOptedOut = true`, `optOut = true`) and call `optIn()` from your prompt.

Flags load and read without consent; only their exposure events wait for it. See [Privacy, consent and data controls](https://apphelm.dev/docs/privacy) and [Install and configure](https://apphelm.dev/docs/install#consent-and-opt-out).

## Identity

- **`identify` at sign-in with your internal user id**, not an email address. It links the anonymous visit to the person, so funnels and experiments count people, not devices.
- **`reset` at sign-out**, so the next person on a shared device starts fresh.
- **Use the same id on every platform.** Pass the same user id as `distinctId` to server SDKs and as the browser SDK's `distinctId` or `identify`. Server events then join that person's browser and app events.
- **Send customer signals from the server.** Identify with traits, accounts, goals and outcomes (`apphelm.identify`, `apphelm.group` …) are refused from browser keys.

See [Users, people and groups](https://apphelm.dev/docs/identify).

## Event names

- **Name what happened, in snake_case**: `signup_completed`, `invoice_sent`, `report_exported`. An event name is at most 100 characters.
- **Use properties for detail**, not new names: `checkout_completed` with `{ plan: "pro" }`, not `checkout_completed_pro`.
- **Keep names stable.** Insights, funnels, alerts and experiment metrics refer to them; renaming one splits its history.
- **Choose one outcome event** that means a user got value. Funnels, experiments and setup checks measure against it.
- **Let autocapture handle page views and clicks**, and track the moments that matter to your product yourself.

See [Events and autocapture](https://apphelm.dev/docs/events).

## Sampling

| Data | Default | Recommendation |
| --- | --- | --- |
| Analytics events | All | Events you track are never sampled. Autocapture can be sampled or limited by path in the product's capture settings. |
| Errors and messages (`sampleRate`) | All (`1`) | Keep at `1`. Use `ignoreErrors` and `denyUrls` for noise instead. |
| Traces (`tracesSampleRate`) | Not set: automatic tracing off | Set it; start around `0.1` to `0.2` and raise it if you need more detail. Each kept transaction is an event. |
| Profiles (`profilesSampleRate`) | Off | A share of sampled transactions, so profiles = traces rate × profiles rate. |
| Browser replay (`replay.sampleRate`) | All consented sessions (`1`) | Lower it for busy sites, and set `onErrorSampleRate: 1` to keep the minute before every error. |

An incoming sampled `traceparent` is always followed, so a trace started in the browser continues on your servers whatever their rate. See [Tracing and performance](https://apphelm.dev/docs/tracing), [Profiling](https://apphelm.dev/docs/profiling) and [Session replay](https://apphelm.dev/docs/session-replay).

## Releases, source maps and symbols

- **Set `release`** in every SDK, to the version you deploy (`shop@2.4.0`, or a commit sha). Errors, release health and "resolve in next release" depend on it.
- **Upload source maps and debug symbols in CI**, for the same release, before or as you deploy. Browser bundles use the Vite, webpack or esbuild plugins (or `withApphelmConfig` for Next.js) and can delete the maps after upload so they are not served. iOS needs dSYMs, Android R8 mappings, native code Breakpad symbols.
- **Record deploys** (`apphelm deploy <product> <release> [environment]` or `POST /api/v1/deploys`), so the release appears on charts and issues can be resolved by release.
- **Build servers with line information** where the language needs it: Rust release profiles with `debug = "line-tables-only"`, Go with `-trimpath` to keep local paths out of stacks, .NET with PDBs.

See [Source maps and debug symbols](https://apphelm.dev/docs/source-maps) and [Releases and release health](https://apphelm.dev/docs/release-health).

## Environments

- **Set `environment` in every SDK** (`production`, `staging`, `development`). Issues can be searched by `environment:`.
- **Pass the environment to flag evaluation** if any flag uses per-environment rollouts. A flag with environment rollouts reads as off when the request names no environment.
- **Decide how to keep staging data apart**: one product with an environment label, or a separate staging product.

See [Keys, scopes and environments](https://apphelm.dev/docs/keys-and-environments#environments).

## Alerts

- **Alert on new and regressed issues** for production, and route them where your team works (Slack, email, PagerDuty and others).
- **Add metric alerts** for the numbers your team watches.
- **Add cron monitors** to scheduled jobs and an **uptime check** to your public site, so silence is noticed.

See [Alerts and notifications](https://apphelm.dev/docs/alerts) and [Cron monitors, uptime and status pages](https://apphelm.dev/docs/crons-and-uptime).

## Feature flags

- **Write the off path first.** Flags are off until they load, while server rendering without a bootstrap, and whenever anything fails.
- **Check a flag once**, where the behaviour forks, and pass the answer down.
- **Bootstrap flags on the server** for web apps (`getBootstrap`, `apphelmHandle`, the Nuxt server plugin), so the first render is right.
- **Evaluate locally on busy servers** with a `products:read` key: answers come from memory and do not count against the monthly evaluation cap.
- **Name flags for the feature**, and archive them when the rollout is done.

See [Feature flags](https://apphelm.dev/docs/feature-flags) and [Experiments](https://apphelm.dev/docs/experiments).

## Personal data

- **Keep personal data out of events.** No emails, names, phone numbers or free text in event names or properties. Use your internal ids.
- **Know what is scrubbed for you.** Every SDK, and the server again, replaces secrets (API keys, tokens, passwords, credentials in URLs) and email addresses in exceptions, messages, breadcrumbs, tags, span descriptions and logs. URL query strings are removed from spans. This cannot be turned off. Analytics event properties are yours: the browser SDK only drops a few keys (`value`, `innerText`, `textContent`, `password`, `email`, `innerHTML`).
- **Use `beforeSend`** to remove anything else. A hook that throws drops the event, so a broken scrubber never lets data through.
- **Mask replay.** Inputs are masked by default. Add `maskAllText: true` where pages show personal data, mark elements with `apphelm-mask` (masked), `apphelm-no-capture` (blocked) or `apphelm-ignore-input` (typing not recorded), and add paths to `blockedPaths`. Sign-in, sign-up, checkout, payment, billing and admin paths are never recorded.
- **Users are hashed.** Server and mobile SDKs send a user's id with errors only as a hash.

See [Privacy, consent and data controls](https://apphelm.dev/docs/privacy).

## Browser performance

- **Create one client per page load.** The framework providers do this for you; in plain JavaScript, create it once and share it.
- **Keep the core small.** The browser core is at most 7,680 bytes gzipped. Autocapture, error capture, tracing, replay, logs, surveys and the toolbar load as separate modules only when they are used.
- **Batch, do not flush per event.** Events are sent every 5 seconds or when 20 are queued, and once more when the page is hidden. Call `flush()` only before you navigate away yourself.
- **Pin the CDN version** in plain HTML (`@sansavision/apphelm@0.8.1/dist/browser.js`) so a new release never changes your site unannounced.
- **Allow trace headers in CORS.** With tracing on, `fetch` to `tracePropagationTargets` carries `traceparent` and `baggage`; your API must allow them.

## Shutdown

Server and job processes must send what is queued before they exit, or the last events are lost.

- Long-running servers: close the client after the HTTP server has stopped taking requests (Go `client.Close()`, Ruby `shutdown` in `at_exit` and Puma's `on_worker_shutdown`, .NET `await using` or `AddApphelm`, Rust `shutdown()`).
- Scripts, serverless functions and CLI tools: `flush` (or `close`) before returning. On Node, `await apphelm.close()`; in Go, `SyncMode: true` is an alternative.
- PHP: each event is sent during the call by default. In queue workers and long-running servers (Octane, RoadRunner, Swoole), call `$apphelm->flush()` after each job or request.

See [Install and configure](https://apphelm.dev/docs/install#flush-and-shut-down).

## Testing and staging

- **Turn sending off in automated tests** with `enabled: false` (`enabled=False`, `Enabled = false` …). Flags still read, so tests behave like production with flags off.
- **Do not rely on headless browsers to test collection.** Browser-key requests from headless browsers, automation tools and plain HTTP clients are filtered as bots and stored nowhere. Check collection from a normal browser.
- **Use `debug` in development**, never in production builds.
- **Press Check connection** in Setup after adding each platform, and work through the setup checks it lists.
- **Exclude your own team's traffic**: mark your office or VPN networks as internal in the workspace settings, or send `apphelm_internal: true` on events you do not want counted.

See [Troubleshooting](https://apphelm.dev/docs/troubleshooting).
