Skip to content
DocsNext.js

Install on Next.js

A plain deferred script in the root layout. No npm package and no client component; it stays in the server-rendered HTML, which is what the installation check reads.

Steps

  1. In Mayfly, open Install for the website, choose Next.js and copy the script. It is written in JSX.
  2. In app/layout.tsx, put it inside the root <head>. Keep it out of page components so it loads once.
  3. Build and deploy as usual.
  4. Back in Mayfly, choose I’ve added the script. Check installation.
app/layout.tsx
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <script
          defer
          src={"https://getmayfly.com/mayfly.js"}
          data-site={"YOUR_SITE_ID"}
          data-verify={"YOUR_VERIFICATION_CODE"}
        />
      </head>
      <body>{children}</body>
    </html>
  );
}

Use your own script from Install: the site ID and verification code above are placeholders.

Good to know

  • Client-side navigation is tracked automatically: the script follows the History API.
  • Don’t also add it through a tag manager. Install it once only.
  • On the Pages Router, the head lives in pages/_document.tsx. The repository installer handles both routers.

Something here doesn’t match what you see? Write to kirylapps@gmail.com.