Next.js
Databuddy works with both the App Router and Pages Router. Add it once near the root of your app; page views and client-side route changes are tracked automatically.
Start with pageviews and one custom event, then use the same data for goals and funnels. The Free plan includes 10,000 events per month; error tracking starts on Hobby. See current plans and limits.
Before you start
NEXT_PUBLIC_DATABUDDY_CLIENT_ID=your-client-idInstall
bun add @databuddy/sdkYou can also install with npm or yarn:
npm install @databuddy/sdk
yarn add @databuddy/sdkApp Router
Add <Databuddy /> to app/layout.tsx:
import { Databuddy } from "@databuddy/sdk/react";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<Databuddy
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
/>
{children}
</body>
</html>
);
}The SDK's React entry point includes the client boundary, so your root layout can stay a Server Component. Custom event handlers belong in a Client Component, as shown below.
Pages Router
Add <Databuddy /> to pages/_app.tsx:
import { Databuddy } from "@databuddy/sdk/react";
import type { AppProps } from "next/app";
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<Databuddy
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
/>
<Component {...pageProps} />
</>
);
}Databuddy sends the first page view when it loads and listens for route changes. Do not add your own router.events or usePathname screen-view tracking unless you intentionally disabled Databuddy's automatic route tracking.
Script Tag Setup
If you prefer not to use the React component, add the script in pages/_document.tsx:
import { Head, Html, Main, NextScript } from "next/document";
export default function Document() {
return (
<Html lang="en">
<Head>
<script
async
crossOrigin="anonymous"
data-client-id={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID}
src="https://cdn.databuddy.cc/databuddy.js"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}Use either the React component or the script tag, not both.
Optional errors and Web Vitals
Add trackWebVitals to the React component to collect performance measurements. Add trackErrors if your plan includes error tracking. Both are off by default, and the collected measurements count toward your event allowance.
<Databuddy
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
trackWebVitals
trackErrors
/>For the script tag, the equivalent attributes are data-track-web-vitals and data-track-errors.
Track Custom Events
Use the SDK helper from client components:
"use client";
import { track } from "@databuddy/sdk";
export function SubscribeButton() {
return (
<button
onClick={() =>
track("subscribe_clicked", {
location: "header",
})
}
type="button"
>
Subscribe
</button>
);
}This records a click, not a completed subscription. Record outcomes such as account creation or payment after the backend confirms success, using the Node SDK. Send each completed outcome from one place so a browser call and server call do not double-count it.
Use stable event names and small properties such as the button location. Do not send emails, passwords, tokens, or entire form values.
Verify your first events
Verify on your deployed site. For a local test, the standard tracker skips localhost: temporarily add debug to <Databuddy /> to load the debug bundle, or use https://cdn.databuddy.cc/databuddy-debug.js in your script tag. Use a separate Databuddy website and Client ID for local test data, and remove debug mode before deploying.
Databuddy DevTools can help inspect the tracker and custom events while you develop.
Production Setup
Add NEXT_PUBLIC_DATABUDDY_CLIENT_ID to your deployment provider before building. On Vercel, choose the deployment environments that should collect data. Next.js embeds public environment variables at build time, so rebuild and redeploy after changing the Client ID.
To avoid test data, disable tracking outside production:
<Databuddy
clientId={process.env.NEXT_PUBLIC_DATABUDDY_CLIENT_ID!}
disabled={process.env.NODE_ENV !== "production"}
/>NODE_ENV is also production for many preview deployments. If previews should stay out of production analytics, use your deployment provider's environment indicator for disabled, or give previews a separate Databuddy website and Client ID.
Replace an existing tracker
If you briefly run both providers, compare the same dates and definitions. Different session, consent, and bot-filtering rules can produce different totals; an exact count match is not a setup requirement.
Troubleshooting
No data in the dashboard
Pageviews work, but an outcome is missing
Confirm the event call runs after the actual outcome, uses the same website, and is not skipped by an error or redirect. If you send it from a serverless route handler, flush the Node SDK before the handler exits. Check the custom event helpers and server event setup.
Related
How is this guide?