next/script in a layout, and call identify() from a small client component. At sign-out, call reset() in the browser. This page covers the App Router and the Pages Router.
Before you start
- Your project key, from Settings → Setup & API Key. It is public, so it is safe in client code. If you keep it in an environment variable, the variable’s name must start with
NEXT_PUBLIC_to reach the browser. - A way to read the signed-in user in a server component or layout. Most auth setups give you one.
- About 10 minutes.
How to install HelpAlive in the App Router
1
Load the script in your layout
Add To load it only after sign-in, put the same
Script to app/layout.tsx to load HelpAlive on every page:app/layout.tsx
Script in your signed-in area’s layout instead, for example app/(app)/layout.tsx. Either way, the agent appears only for signed-in users.2
Add the helper module
Create
lib/helpalive.ts. It queues calls until the script loads, and the script replays them in order. It uses window only inside its functions, so you can safely import it during server rendering:lib/helpalive.ts
3
Call identify() from a client component
Create a client component that receives the user and calls Render it in your signed-in layout, and build its Everything you pass to a client component is sent to the browser. Never pass the session object with its tokens. Identify users describes each field.
identify() in an effect:components/helpalive-identify.tsx
user prop on the server from your session:app/(app)/layout.tsx
4
Call reset() in the browser at sign-out
Call If your sign-out is a Server Action or a Route Handler, keep it there. Call
resetUser() in the click or submit handler of your sign-out control, before your sign-out call:components/sign-out-button.tsx
resetUser() in the browser before your sign-out runs. Also call it wherever the browser learns a session has ended, for example when a fetch returns 401.How to install HelpAlive in the Pages Router
Load the script and identify the user inpages/_app.tsx, using the same lib/helpalive.ts module:
pages/_app.tsx
resetUser() in your sign-out handler, exactly as in the App Router.
Why use next/script instead of a plain script tag?
next/scriptloads the tag once and keeps it across client-side navigations.afterInteractiveloads it once the page has started hydrating, so it doesn’t delay your first render.- A plain
<script>added from a client component is not guaranteed to run.
Script in a layout or in _app. If you put it in a single page, HelpAlive loads only on that page.
How do I add the nonce for a Content Security Policy?
Pass your nonce toScript with the nonce prop, as your other scripts get it. Also add HelpAlive’s sources to your policy. Content Security Policy lists and explains the sources.
Do I need to tell HelpAlive about route changes?
No. HelpAlive records a page view whenever the Next.js router changes the address, including client-side navigations withLink and router.push().
Check that it works
Sign in to your app with a test account, then open Settings → Setup & API Key on the dashboard. The first step, Add HelpAlive to your app, gets a check once HelpAlive has seen your page and a signed-in user. Check it works explains each step. While you test, adddata-debug to Script to see what HelpAlive does on a page. Remove it before you ship.
Verify users
Verifying users is optional and recommended. Your server signs a token for the signed-in user, and your app passes it toidentify() in place of userId and tenantId. Verify users shows how to create the signing secret.
In the App Router, sign the token in a module that only your server code imports:
lib/helpalive-token.ts
NEXT_PUBLIC_: Next.js exposes those variables to the browser. Then sign the token in your signed-in layout, and pass it in place of userId and tenantId:
app/(app)/layout.tsx
exp, also pass refreshUserToken, which HelpAlive calls when a token expires. A Server Action can sign a fresh token:
app/actions/helpalive.ts
components/helpalive-identify.tsx
pageProps.user, for example in getServerSideProps. Pass it to identifyUser() as userToken.
If your server can’t make a token, for example after an error, pass userId and tenantId as before. Keep resetUser() at sign-out: it also forgets the token.
Next
Identify users
Every identify() field, switching company, and logout.
Verify users
Sign each user id on your server, with a Node.js example.
React
The same steps for React apps built with Vite.

