Integrations

Next.js

Add analytics to a Next.js app with one component. Pageviews and route changes are tracked automatically.

Install

npm install @databuddy/sdk
.env.local
NEXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-id

Your Client ID is in the website's tracking setup at app.databuddy.cc. It's public; never put server API keys in NEXT_PUBLIC_ variables.

Add the provider once

app/layout.tsx
import { DatabuddyProvider } from "@databuddy/sdk/react";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <DatabuddyProvider clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!} />
        {children}
      </body>
    </html>
  );
}

The layout stays a Server Component. Wrap {children} with the provider instead if you use feature flags.

Deploy

Set NEXT_PUBLIC_DATABUDDY_CLIENT_ID in your hosting provider and rebuild; Next.js embeds it at build time. Visits from localhost aren't sent.

Track a signup

Track the click in the browser and the finished signup on the server, so a funnel shows who started and who finished.

components/signup-button.tsx
"use client";

import { track } from "@databuddy/sdk";

export function SignupButton() {
  return (
    <button onClick={() => track("signup_clicked", { location: "header" })} type="button">
      Sign up
    </button>
  );
}

Never send emails, passwords, tokens or whole form values as properties.

Turn on errors and Web Vitals

<DatabuddyProvider
  clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
  autocapture={{ webVitals: true, errors: true }}
/>

Error tracking needs the Hobby plan or higher. Add source maps with withDatabuddy() in next.config.ts to see your original code in stack traces.

Keep test data out

<DatabuddyProvider
  clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
  disabled={process.env.NEXT_PUBLIC_VERCEL_ENV !== "production"}
/>

NODE_ENV is production on most preview deployments too, so use your host's environment flag (NEXT_PUBLIC_VERCEL_ENV on Vercel), or give previews their own website.

Keep visitors who use ad blockers

Blockers often stop cdn.databuddy.cc and basket.databuddy.cc. Serve both from your own domain:

next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  async rewrites() {
    return [
      { source: "/_p/s.js", destination: "https://cdn.databuddy.cc/databuddy.js" },
      { source: "/_p/e/:path*", destination: "https://basket.databuddy.cc/:path*" },
    ];
  },
};

export default nextConfig;
<DatabuddyProvider
  clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
  scriptUrl="/_p/s.js"
  apiUrl="/_p/e"
/>
Proxy bothA proxied script alone still loses its events
Path prefixNeutral, like /_p. Blocklists match /analytics, /stats and /track
LocationsStay accurate: the rewrite forwards the visitor IP in X-Forwarded-For
Pinned script versionsRewrite /_p/s/:path* to https://cdn.databuddy.cc/:path* and set cdnUrl="/_p/s" instead of scriptUrl
Feature flagsStill load from api.databuddy.cc

No data?

SymptomFix
Nothing arrivesCheck the Client ID matches the website, and that disabled is false where you test
Requests blockedAllow https://cdn.databuddy.cc scripts and https://basket.databuddy.cc connections in your CSP, or proxy them as above
Pageviews counted twiceRemove a second script in a tag manager or nested layout, and any manual pageview tracking
Server event missingCall flush() before a route handler or server action returns

How is this guide?