Integrations

React

Add analytics to a React app with one component. Pageviews, including React Router navigation, are tracked automatically.

Install

npm install @databuddy/sdk

Add the provider

src/App.tsx
import { DatabuddyProvider } from "@databuddy/sdk/react";

export default function App() {
  return (
    <DatabuddyProvider clientId={import.meta.env.VITE_DATABUDDY_CLIENT_ID}>
      <YourApp />
    </DatabuddyProvider>
  );
}
.env
VITE_DATABUDDY_CLIENT_ID=your-client-id

Wrapping your app also serves feature flags to useFlag. If you only want tracking, render <DatabuddyProvider /> without children.

Track an event

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

<button onClick={() => track("signup_clicked", { location: "hero" })}>Sign up</button>;

Or skip the handler with data-track attributes, after turning on autocapture={{ attributes: true }}:

<button data-track="signup_clicked" data-location="hero">Sign up</button>

Every other data- attribute on the element becomes a property.

Know who's signed in

<DatabuddyProvider
  clientId={import.meta.env.VITE_DATABUDDY_CLIENT_ID}
  user={session ? { id: session.user.id, traits: { plan: session.user.plan } } : null}
>
  <YourApp />
</DatabuddyProvider>

Pass null on sign-out. See Identify users.

Report errors

<DatabuddyProvider clientId={clientId} autocapture={{ errors: true, webVitals: true }} />

That captures uncaught errors and rejections. Report ones you catch with captureException(error) from @databuddy/sdk, for example in an error boundary's onError. Add source maps with the Vite plugin to see your original code.

Autocapture options

OptionDefaultRecords
attributesOffClicks on elements with data-track
errorsOffUncaught errors and unhandled rejections
webVitalsOffCore Web Vitals
outgoingLinksOffClicks on links to other websites
hashChangesOffHash changes as pageviews
interactionsOnTime on page, scroll depth, interaction counts

How is this guide?