SDK reference

React / Next.js

DatabuddyProvider loads tracking, identifies the signed-in user and serves feature flags, in React 18+ and Next.js 13+.

bun add @databuddy/sdk
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 can stay a Server Component; the SDK's React entry supplies its own client boundary.

That records pageviews. A provider without children only loads tracking; wrap your app with it to identify users or read flags. For a full walkthrough, see the Next.js guide.

Track events

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

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

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

track, identify, group, reset, getIds and the other functions import from @databuddy/sdk; see Track events. @databuddy/sdk/react exports the provider and the flag hooks. A click records intent, so send confirmed outcomes, such as a paid signup, from your server.

Identify signed-in users

app/providers.tsx
"use client";

import { DatabuddyProvider } from "@databuddy/sdk/react";
import { useSession } from "@/lib/auth-client";

export function Providers({ children }: { children: React.ReactNode }) {
  const { data: session, isPending } = useSession();
  const user = session?.user;

  return (
    <DatabuddyProvider
      user={user ? { id: user.id, traits: { email: user.email, name: user.name } } : null}
      userPending={isPending}
    >
      {children}
    </DatabuddyProvider>
  );
}

The provider identifies the user on sign-in. It resets on sign-out, or when another user signs in, so people sharing a device don't share visitor IDs. Put email in traits to store it on the profile; a top-level email targets flags only. See Identify users.

Put users in their workspace

<DatabuddyProvider
  user={user ? { id: user.id } : null}
  userPending={isPending}
  groups={{ workspace: session?.activeWorkspaceId ?? null }}
>
  {children}
</DatabuddyProvider>

A key joins the account, null leaves it, and undefined keeps the remembered one while it loads. Flags evaluate with it from the first render. To name the account, call group("workspace", id, { name }) alongside the prop.

Provider props

PropTypeDefaultWhat it does
clientIdstringNEXT_PUBLIC_DATABUDDY_CLIENT_IDYour website's client ID
userDatabuddyUser | nullThe signed-in user, or null. Omit it to call identify() yourself.
userPendingbooleanfalseThe user is loading; identity and flags wait
groupsRecord<string, string | null | undefined>The user's accounts. Omit it to call group() yourself.
flagsFlagsOptionsFeature flag settings
disabledbooleanfalseSends nothing, and flags serve their defaults

Every other option, such as autocapture, batching, skipPaths, consent and replay, is in the configuration reference.

app/layout.tsx
<DatabuddyProvider
  autocapture={{ webVitals: true, errors: true, attributes: true }}
  disabled={process.env.NODE_ENV === "development"}
  skipPaths={["/admin/**"]}
/>

With attributes: true, <button data-track="signup_clicked" data-plan="pro"> sends signup_clicked with { plan: "pro" } on click.

How is this guide?