Install guides / Astro

Add a support chat widget to Astro

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 Astro.

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 Astro

Paste your line into the layout your pages share, just before </body>, and add is:inline so Astro leaves the tag as it is:

src/layouts/Layout.astro

<html lang="en">
  <head>
    <title>Acme Invoices</title>
  </head>
  <body>
    <slot />
    <script is:inline src="https://product.tech/widget.js" data-product="acme-invoices" async></script>
  </body>
</html>

If your layout uses view transitions (<ClientRouter />), Astro swaps the page's body on each link, and the button would go with it. Take the widget down before each swap and let the line run again after it:

src/layouts/Layout.astro

<script is:inline>
  document.addEventListener("astro:before-swap", () => window.ProductTechWidget?.destroy());
</script>
<script is:inline data-astro-rerun src="https://product.tech/widget.js" data-product="acme-invoices" async></script>

Good to know

  • Without is:inline Astro treats the tag as a script of its own to process. With it, the tag reaches the page exactly as written.
  • 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 astro dev, open a page and wait a moment: the button appears in the bottom corner.

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.

On a page rendered on demand, sign the user's email address in the layout's frontmatter and pass it to the page with define:vars. A prerendered page is the same for everyone, so it can't say who is reading.

src/layouts/Layout.astro

---
import { createHmac } from "node:crypto";

const user = Astro.locals.user; // however your app keeps the signed-in user
const visitor = user
  ? {
      email: user.email,
      name: user.name ?? "",
      hash: createHmac("sha256", import.meta.env.PRODUCTTECH_WIDGET_KEY).update(user.email.toLowerCase()).digest("hex"),
    }
  : null;
---
<script is:inline define:vars={{ visitor }}>
  window.ProductTechUser = visitor;
</script>
<script is:inline src="https://product.tech/widget.js" data-product="acme-invoices" async></script>

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 Astro · Product.tech