SDK reference

Vanilla JavaScript

Add one script tag before </body> and pageviews are tracked from then on.

index.html
<script
  src="https://cdn.databuddy.cc/databuddy.js"
  data-client-id="your-client-id"
  data-track-web-vitals
  data-track-errors
  async
></script>

Every data-* option is in the script tag reference. Using a bundler? init() from @databuddy/sdk loads the same script, and its functions replace the window.databuddy calls below.

Track events

window.databuddy.track("signup_clicked", { source: "header" });

Or with no JavaScript, add data-track-attributes to the script tag and mark elements:

<button data-track="signup_clicked" data-plan="pro">Sign up</button>

A click sends signup_clicked with { plan: "pro" }. window.db is a shorter alias for window.databuddy.

Call the tracker

MethodWhat it does
track(name, properties?)Sends a custom event
captureException(error)Reports an error you caught to the Errors page, even without data-track-errors
identify(profileId, traits?)Links this browser to your user ID. See Identify users.
setGlobalProperties(properties)Adds properties to every later event
setGroupTraits(type, key, traits)Names an account
screenView(properties?)Records a pageview yourself
flush()Sends queued events now
clear()Forgets the user and starts new anonymous and session IDs
clearProfile()Forgets the user but keeps the visitor
getTrackingIds(){ anonId, sessionId } for this visitor
stop()Detaches the tracker and drops unsent events
disabledReasonnull while tracking, or why not: config, opt_out, gpc, storage, consent, bot, localhost or stopped

To put visitors in an account, set window.databuddy.setGlobalProperties({ group_workspace: workspace.id }) on every page.

Let visitors opt out

window.databuddyOptOut();
window.databuddyOptIn();

The opt-out is remembered across visits. The tracker fires a databuddy:consent window event, "denied" or "granted", whenever tracking is withdrawn or allowed again. See opt-outs for what an opt-out stops.

Decide what is sent

Set hooks on window.databuddyConfig before the script loads. consent is read before every event and request, and beforeSend sees every event.

index.html
<script>
  window.databuddyConfig = {
    consent: () => !document.cookie.includes("site_consent=denied"),
    beforeSend: (event) => {
      if (event.type === "error") delete event.stack;
      return true;
    },
  };
</script>
<script src="https://cdn.databuddy.cc/databuddy.js" data-client-id="your-client-id" async></script>

To record session replays, add the recorder next to the tracker:

<script src="https://cdn.databuddy.cc/replay.js" data-client-id="your-client-id" async></script>

How is this guide?