Tracking recipes

Feature usage tracking

Wrap track() in a hook named after the feature, and every step reports the same event.

hooks/use-feature-tracking.ts
import { track } from "@databuddy/sdk";
import { useCallback } from "react";

export function useFeatureTracking(feature: string) {
  return useCallback(
    (action: string, properties?: Record<string, unknown>) =>
      track("feature_used", { feature, action, ...properties }),
    [feature]
  );
}
components/export-button.tsx
export function ExportButton() {
  const trackExport = useFeatureTracking("export_data");

  async function handleExport() {
    trackExport("started", { format: "csv" });
    try {
      await exportData();
      trackExport("completed", { format: "csv" });
    } catch {
      trackExport("failed", { format: "csv" });
    }
  }

  return <button onClick={handleExport}>Export</button>;
}

What it sends

EventPropertyValue
feature_usedfeatureThe feature's name, such as export_data
actionstarted, completed or failed
anything elseThe properties you pass

Compare started with completed to see how often a feature finishes. To report the error itself, call captureException(error) in the catch.

How is this guide?