npm and Next.js

Keep the script tag, then import typed helpers instead of calling window.pagebee.

Next.js App Router

app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <Script defer src="https://api.pagebee.ai/script.js" data-site="pb_pk_your_site_key" />
      </head>
      <body>{children}</body>
    </html>
  );
}

Route changes through next/link and router.push are counted as pageviews automatically.

Typed helpers

ts
import { track } from "@pagebee/sdk-web";

track("Exported report", { props: { format: "csv" } });

Keeping previews out of production

Vercel preview URLs (*.vercel.app) count as preview by default, so you can ship the same tag to every deploy. To be explicit, set data-env from your build:

tsx
<Script defer src="https://api.pagebee.ai/script.js" data-site="pb_pk_your_site_key"
  data-env={process.env.VERCEL_ENV === "production" ? "production" : "preview"} />