Skip to main content
Installing HelpAlive in Next.js means three things. Load our script with 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 Script to app/layout.tsx to load HelpAlive on every page:
app/layout.tsx
To load it only after sign-in, put the same 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 identify() in an effect:
components/helpalive-identify.tsx
Render it in your signed-in layout, and build its user prop on the server from your session:
app/(app)/layout.tsx
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.
4

Call reset() in the browser at sign-out

Call resetUser() in the click or submit handler of your sign-out control, before your sign-out call:
components/sign-out-button.tsx
If your sign-out is a Server Action or a Route Handler, keep it there. Call 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 in pages/_app.tsx, using the same lib/helpalive.ts module:
pages/_app.tsx
Call resetUser() in your sign-out handler, exactly as in the App Router.

Why use next/script instead of a plain script tag?

  • next/script loads the tag once and keeps it across client-side navigations.
  • afterInteractive loads 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.
Keep 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 to Script 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 with Link 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, add data-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 to identify() 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
Never start the secret’s variable name with 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
If your tokens have an exp, also pass refreshUserToken, which HelpAlive calls when a token expires. A Server Action can sign a fresh token:
app/actions/helpalive.ts
Then pass it from your client component:
components/helpalive-identify.tsx
In the Pages Router, sign the token where you build 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.
Last modified on November 25, 2026