SDK reference

Accounts

The Accounts page groups users by the workspace or company they work in, and shows each account's activity, plan and billing.

app/providers.tsx
<DatabuddyProvider
  user={user ? { id: user.id, traits: { email: user.email } } : null}
  groups={{ workspace: session?.activeWorkspaceId ?? null }}
>
  {children}
</DatabuddyProvider>

Users you identify with an email trait are also grouped by their work email domain with no setup. Personal addresses and your own team's domain are left out.

Put users in an account

Pass groups to DatabuddyProvider. Flags evaluate with it from the first render.

<DatabuddyProvider groups={{ workspace: loading ? undefined : (workspace?.id ?? null) }} />
groups valueMeans
A keyJoins that account
nullIn no account of that type, such as signed out
undefinedNot known yet; keeps the account remembered from the last visit
Type left outLeaves that account

Later events carry group_workspace: <key>. A visitor is in one account of each type, and the SDK remembers it across visits until reset() or optOut(). Types use lowercase letters, digits and underscores; email_domain is reserved, and keys over 256 characters are skipped with a warning. getGroups() returns the visitor's accounts to send to your server.

When the workspace is in the URL, such as /w/[workspaceId], pass it on the first render. Every tab shares the remembered account, so a key that arrives late sends the first pageview to whichever workspace another tab used last.

Name the account and say how it pays

The browser can send display traits. Anyone can send requests with your client ID, so billing, dates and usage are accepted only from your server with an API key.

Browser
group("workspace", workspace.id, { name: workspace.name, domain: workspace.domain });
Server
await databuddy.group({
  groupType: "workspace",
  groupKey: workspace.id,
  traits: { billing_plan: "pro", billing_status: "active", billing_mrr: 49, billing_currency: "USD" },
});
TraitsWho can send them
name, domain, logo and other display traitsBrowser or server
billing_*, and the plan, status, mrr, trial_ends_at and customer_id keys they fall back toServer only
created_at, last_active_atServer only
plan_usage, plan_limit, usage_7d, usage_30d, usage_unitServer only

The browser's group() leaves server-only traits out with a warning. What each trait does is listed under the Node SDK's group(). Always send billing_currency with billing_mrr, or MRR shows as a plain number and isn't added up.

Billing from Stripe

Put databuddy_group_<type> in the subscription's metadata, or in subscription_data.metadata with Checkout, and Stripe keeps that account's plan, status, MRR, currency, trial end and customer current.

Server
await stripe.subscriptions.create({
  customer,
  items: [{ price: priceId }],
  metadata: { databuddy_group_workspace: workspace.id },
});
  1. Subscription names the account
  2. Stripe changes
  3. Account billing updates
SituationWhat happens
A subscription runsThe account shows billing_source: "stripe", and your server's billing traits are ignored. Other traits still apply.
It's canceled or pausedYour server's billing traits apply again
You stop using StripeSend billing_source: null with your billing traits
You already had subscriptionsConnect Stripe brings in the current ones; with a manual webhook, save the metadata or wait for the next change
One customer's subscriptions name the same accountThe account keeps the one it shows; a new one takes over when the old one ends
A group type over 24 charactersLeft out, because Stripe metadata keys are limited to 40 characters

getRevenueMetadata() also returns these keys, but a buyer can edit what their browser sends. Take only the visit IDs from the checkout request, as in Payments.

Target flags at accounts

Flags see the visitor's accounts as group_<type> properties, so a rule like group_workspace equals ws_123 targets everyone in that workspace. To roll out by account, choose its type under Accounts in the flag's Bucket by. On the server, pass groups in the user context, and pass the same groups to evaluateFlags() as to the provider.

Send what each kind of product needs

ProductSend
SaaS with workspacesgroups on the provider, and the same groups to evaluateFlags()
API-first or server-onlygroups on track(), and last_active_at for every account in use, from a periodic job
Several workspaces on one planThe same billing_customer on each workspace; a second group type, such as company, to see the company too
Usage-based plansplan_usage, plan_limit, usage_7d and usage_30d from a nightly job
Free trialsbilling_status: "trialing" and billing_trial_ends_at, or the Stripe metadata
Coming from PostHogThe PostHog adapter's group() and groupIdentify() work as before
jobs/usage.ts
await databuddy.group({
  groupType: "workspace",
  groupKey: workspace.id,
  traits: { plan_usage: usage.thisPeriod, plan_limit: plan.includedUnits, usage_30d: usage.last30Days, last_active_at: lastRequestAt },
});

How is this guide?