Skip to main content

Next.js — Server tracking

Track events from the server so they appear in the same session as client events. Use your shared signal client and pass sessionId and windowId from request headers into every signal.capture() or signal.identify() call.

App Router: API routes

Use getSessionInfo(request) with the route’s Request:

App Router: Server Actions

Read headers with headers() from next/headers and pass the same identifiers:

Pages Router: API routes

Use req.headers and a small helper:

Pages Router: getServerSideProps

Read headers from context.req and pass them into signal.capture():

Enabling tracing headers on the client

For the server to receive x-signal-session-id, x-signal-window-id, and x-signal-distinct-id, the client must send them. Set addTracingHeaders: true (or an array of hostnames) in your SignalNextProvider options when you use server-side tracking. See Configuration.

Advanced server helpers

The server package also exports:
  • trackServerEvent(options) — Convenience that uses the singleton from createSignalServer to capture one event (e.g. in a one-off handler). Options: distinctId, event, properties.
  • getSignalServerSideProps(getServerSidePropsFunc, options) — Wraps your getServerSideProps to inject Signal props (e.g. distinctId, sessionId) from getDistinctId / getSessionId callbacks. Use when you want Signal context in page props.
  • withSignalConfig(nextConfig) — Wraps your Next.js config to inject Signal env vars (NEXT_PUBLIC_SIGNAL_ENDPOINT, NEXT_PUBLIC_SIGNAL_PROJECT_ID). Use if you prefer config over .env.local.

See also