Install guides / Next.js

Add a support chat widget to Next.js

The Product.tech chat answers your visitors from your help center, hands the questions it can't answer to your team, and shows what's new. Here's where its line goes on Next.js.

Before you start

Copy your own line from your dashboard, on the Chat widget page. The examples here use acme-invoices, our demo product, so swap in yours. The button stays hidden until you publish an article, so it's safe to add the line first.

Add it to Next.js

Add the script to your root layout with next/script. The lazyOnload strategy loads it once the page is idle, which is what Next.js suggests for chat widgets:

app/layout.tsx

import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script src="https://product.tech/widget.js" data-product="acme-invoices" strategy="lazyOnload" />
      </body>
    </html>
  );
}

With the Pages Router, put the same <Script> in pages/_app.tsx, next to <Component {...pageProps} />.

Good to know

  • next/script passes data-product through to the script tag, and leaves out async, which it handles itself.
  • Put it in the root layout rather than a page. The layout stays mounted as people move between pages, so the button stays put and an open chat stays open.
  • If you set a Content-Security-Policy, allow https://product.tech in script-src, connect-src and frame-src.
  • If you list the sites allowed to show your chat, on the Chat widget page, add every address this site is served from, staging addresses included. With no list, any site can show it.

Check that it works

Run next dev, open any page and wait a moment: the button appears in the bottom corner. Follow a link and it stays.

No button? Open the browser's console. A message starting with [product.tech] says what's wrong, such as a missing data-product or a site that isn't on your list of allowed sites. With nothing there, check that your help center has at least one published article.

Tell the chat who is signed in

When someone is signed in to your product, the chat can show their email address on the conversation in your inbox, so you can write back, and answer them from your private articles. A page can claim to be anyone, so the address comes with a signature your server makes with a key only you hold.

Create a key in your dashboard, under Customer access, then Your site's sign-in. Keep it on your server as PRODUCTTECH_WIDGET_KEY, never in code that reaches the browser.

In the root layout, which runs on the server, sign the user's email address and set window.ProductTechUser before the widget loads:

app/layout.tsx

import Script from "next/script";
import { createHmac } from "node:crypto";

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const user = await getCurrentUser(); // however your app reads the signed-in user
  const visitor = user
    ? {
        email: user.email,
        name: user.name ?? "",
        hash: createHmac("sha256", process.env.PRODUCTTECH_WIDGET_KEY!).update(user.email.toLowerCase()).digest("hex"),
      }
    : null;

  return (
    <html lang="en">
      <body>
        {children}
        <script
          dangerouslySetInnerHTML={{
            __html: `window.ProductTechUser = ${JSON.stringify(visitor).replace(/</g, "\\u003c")};`,
          }}
        />
        <Script src="https://product.tech/widget.js" data-product="acme-invoices" strategy="lazyOnload" />
      </body>
    </html>
  );
}

If people sign in or out without a full page load, also call window.ProductTechWidget?.identify(visitor) from the client, or identify(null) on sign-out. If your app signs in with Firebase, Clerk, Auth0 or Supabase, you can skip the key: name your provider on the same page, and it gives you the one line to add instead.

Options

Add any of these to the line, next to data-product. Each one wins over what you picked on the Chat widget page.

data-position="left"
The corner for the button and the chat: left or right.
data-color="#0a7d5a"
The button's colour, as a hex value.
data-label="Need help?"
The button's words.
data-theme="dark"
Light, dark or system, which follows each visitor's device.

To open the chat from a link or button of your own, call ProductTechWidget.open().

Other platforms

New to Product.tech? It writes your help center from your live app, and from your code once you connect GitHub, then drafts release notes from merged pull requests. See how it works.

Add a support chat widget to Next.js · Product.tech