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/sdkThat 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
"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
"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
Every other option, such as autocapture, batching, skipPaths, consent and replay, is in the configuration reference.
<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.
Related
How is this guide?