<!-- Session Replay — Heystack docs. Rendered: https://heystack.dev/docs/replay · Index: https://heystack.dev/llms.txt -->
# Session Replay

See exactly what your users saw. Heystack records real user sessions — clicks,
scrolls, page transitions, and on-screen errors — so you can watch a replay when
something goes wrong, or to understand how people actually use your app.

## Turn it on

Replay has two pieces: a one-time bit of **browser** code, and a **toggle** in the
console. Nothing records until both are in place — the recorder asks Heystack on
every page load whether recording is on, so it's safe to ship the code before you
flip the switch (and to leave it shipped when you switch it back off).

### 1. Add the recorder to your web app

Call `instrumentWeb()` once in your app's **browser** entry — client-side, never in
server code. The rrweb recorder ships *inside* `@heystack/otel`, so there's nothing
else to install, and uploads work cross-origin out of the box (no CORS setup).

The `apiKey` is a **Browser key** (`hs_web_…`). It ships to the client, so it is
public by design — and write-only: it can send browser traces, logs, replays and bug
reports, but can't read data, manage your app or upload dSYMs. Mint one under your
app's **Connection & keys → Browser key** and put it in a public env var.
**Never use your server key (`sk_live_…`) in browser code** — if one already shipped
to a browser, switch to a Browser key and revoke + rotate the server key.

**Next.js (App Router)** — a tiny client component, mounted once in your root layout:

```tsx
// app/HeystackReplay.tsx
"use client";
import { useEffect } from "react";
import { instrumentWeb } from "@heystack/otel/web";

export function HeystackReplay() {
  useEffect(() => {
    const apiKey = process.env.NEXT_PUBLIC_HEYSTACK_WEB_KEY;
    if (!apiKey) return; // no key → no-op
    let stop: (() => void) | undefined;
    let cancelled = false;
    instrumentWeb({ apiKey, service: "my-web-app" })
      .then((s) => (cancelled ? s() : (stop = s)))
      .catch(() => {}); // replay is best-effort; never break the app
    return () => { cancelled = true; stop?.(); };
  }, []);
  return null;
}
```

```tsx
// app/layout.tsx — inside <body>
import { HeystackReplay } from "./HeystackReplay";
// …
<HeystackReplay />
```

**Vite / plain React** — call it once in your client entry (`main.tsx`):

```ts
import { instrumentWeb } from "@heystack/otel/web";

instrumentWeb({
  apiKey: import.meta.env.VITE_HEYSTACK_WEB_KEY,
  service: "my-web-app",
});
```

### 2. Set the browser key + enable it

- Add the public env var to your web app: `NEXT_PUBLIC_HEYSTACK_WEB_KEY=hs_web_…`
  (Next.js) or `VITE_HEYSTACK_WEB_KEY=hs_web_…` (Vite).
- In the console, open your app → **Settings → Data & privacy → Session replay**, toggle it on, and
  pick a **sample rate** (start small).

Recording begins on the next page load. New sessions show up under **Users & sessions**, where a replay is part of a visit.

## Browser errors & console logs

The same one line of setup also turns on **browser error collection** — no extra
code. Every uncaught error and unhandled promise rejection in the browser is
captured (with its message, type, and stack trace), plus `console.error` by
default, and shows up in **Explore › Logs**. Each error is stamped with the
page URL and the **session**, so it links straight to the replay where it happened
(and, if you turned on browser tracing, to the trace it occurred in).

It's on by default when you call `instrumentWeb()`. To tune it:

```ts
instrumentWeb({
  apiKey: import.meta.env.VITE_HEYSTACK_WEB_KEY,
  service: "my-web-app",
  version: "1.4.2",          // attribute browser errors to a release…
  build: "abc1234",          // …and a commit
  captureConsole: "warn",    // also capture console.warn (default: only console.error)
  // errors: false,          // or turn error capture off entirely
});
```

Console capture is deliberately conservative: it's **rate-limited** (a runaway
`console.error` in a render loop can't flood your logs — the overflow is dropped
and summarized) and never captures anything Heystack logs itself. Like replay and
tracing, error uploads never get caught in their own instrumentation.

## Privacy

Masking happens in the visitor's browser, before anything is uploaded. Pick a
mode under **Settings → Data & privacy → Session replay**:

- **Strict (the default).** All text on the page and everything typed into
  inputs is masked — each character is replaced with `*`, so the layout still
  reads but the words don't. Add `data-hs-unmask` to an element you trust (a
  navigation bar, a pricing table) to record it, and everything inside it, as-is.
- **Relaxed.** Page text and inputs are recorded, except text inside elements
  marked `data-hs-mask`.

In both modes:

- **Password fields are always masked**, even inside a `data-hs-unmask` area.
- `data-hs-block` leaves a region out of the recording entirely (it shows as an
  empty box).
- A `data-hs-mask` inside a `data-hs-unmask` area masks again — the closest
  marker wins.
- The page address recorded with a replay has secrets removed: sign-in codes,
  API keys and tokens in the address (for example `?code=…`, `?token=…`,
  `#access_token=…`) are replaced with `REDACTED`. Add your own parameter names
  with `redactQueryParams: ["invite"]` in `instrumentWeb()`.

Masking covers text and form values. Element attributes (tooltips, image alt
text, placeholders, link addresses) and images are recorded as they are, so wrap
anything sensitive of that kind in `data-hs-block`.

## Sampling

You control how much gets recorded from the console — no code changes or
redeploys. Turn it up during an incident, down when things are calm.

## Connected to your activity

Every replay links to the backend activity it triggered, so from a session you
can jump straight to what happened on the server — and back. The link between the
two is the **session**:

- **From a replay to the errors.** Open a replay and you'll see an **Errors in
  this session** list under the player. Click any error to open its full trace —
  spans, logs, and exceptions — so you can see exactly what failed.
- **From an error to the replay.** Anywhere an error, trace, issue, or crash is
  shown, a **Watch session replay** button appears when that session was recorded.
  One click opens the replay at that session, so you can watch what the user did
  right before things went wrong.

Replay searches are always limited to recent data. A session list without a valid
range defaults to the last 24 hours; playback and correlated session lookups cover
the retained 30-day replay period.

## Where replays live

A replay is part of a **visit** rather than a list of its own.
Open **Sessions** (or a person's page, or any error that shows a visit) and you get
the visit's story: the page loads, errors, AI calls, safety decisions, product
events and bug reports in time order, with a **Watch replay** button when the visit
was recorded. A visit with no replay still has a story, and it says so. If the same
visit id was used in more than one app, the story shows the app with the latest
activity and links to the others.

The same data is available over the API as `GET /api/sessions` and
`GET /api/sessions/{session_id}` (see the [API reference](https://heystack.dev/docs/api)). A read key
sees the last 7 days of a visit's bug reports, like every other read.
