Node.js
Create one client with an API key and track what happens on your server.
import { Databuddy } from "@databuddy/sdk/node";
export const databuddy = new Databuddy({
apiKey: process.env.DATABUDDY_API_KEY!,
websiteId: process.env.DATABUDDY_WEBSITE_ID,
});
await databuddy.track({ name: "invoice_paid", properties: { plan: "pro" } });The API key needs the track:events scope for the website, from API keys. The client never reads environment variables itself.
Send events from serverless functions
Events are batched, so call flush() before a function returns or they can be lost.
With batching on, track() usually only confirms the event was queued; flush() returns the delivery result. Delivery failures are returned, never thrown. Only the constructor throws, for a missing apiKey.
Call the client
Failed responses carry code (such as NETWORK_ERROR, VALIDATION_ERROR or QUEUE_FULL), why, fix, retryable and statusCode. A group type that isn't lowercase letters, digits and underscores, the reserved type email_domain, or a key over 256 characters fails with VALIDATION_ERROR.
import { parseTrackingParams } from "@databuddy/sdk/node";
import { databuddy } from "@/lib/databuddy";
export async function POST(request: Request) {
const { anonymousId, sessionId } = await request.json();
await databuddy.track({ name: "checkout_started", anonymousId, sessionId });
return Response.json({ ok: true });
}
export async function GET(request: Request) {
const ids = parseTrackingParams(new URL(request.url).searchParams);
await databuddy.track({ name: "signup_link_opened", ...ids });
return Response.json({ ok: true });
}parseTrackingParams() reads the IDs a cross-domain link carried, with null for any that is missing or isn't a Databuddy ID.
Configure the client
Retries send the same event IDs, so an event is stored once. Other 4xx answers, such as an invalid event or key, aren't retried. A failed send doesn't poison the deduplication cache, so you can retry with the same eventId.
Set account traits
await databuddy.group({
groupType: "workspace",
groupKey: workspace.id,
traits: {
name: workspace.name,
domain: "databuddy.cc",
billing_plan: "pro",
billing_status: "active",
billing_mrr: 49,
billing_currency: "USD",
usage_30d: 7920,
usage_unit: "API calls",
},
});People join an account through their events, not through group(). The traits the Accounts page reads are typed as AccountTraits; dates can be Date objects or ISO strings.
Record opt-outs from your server
await databuddy.optOut({ profileId: user.id });
await databuddy.optIn({ profileId: user.id });Name the visitor by profileId, the browser's anonymousId, or both. From then on Databuddy drops events naming either ID on that website, from any SDK.
optIn() clears what optOut() recorded; an opt-out made in the visitor's own browser stays until they opt in there. Opt-outs are per website and logged on your organization's audit log without the visitor's IDs. If Databuddy can't read its opt-out list within a second, it accepts the event rather than hold up ingestion.
Related
How is this guide?