Tracking recipes

Feedback tracking

Record each rating as one event, from whichever side handles the submit.

components/feedback.tsx
"use client";

import { track } from "@databuddy/sdk";
import { usePathname } from "next/navigation";

export function Feedback() {
  const pathname = usePathname();

  function rate(opinion: "good" | "bad", message = "") {
    track("feedback_submitted", {
      url: pathname,
      opinion,
      has_message: message.trim().length > 0,
      message_length: message.trim().length,
    });
  }

  return (
    <div>
      <button onClick={() => rate("good")}>Helpful</button>
      <button onClick={() => rate("bad")}>Not helpful</button>
    </div>
  );
}

When a server action saves the feedback, track it there instead, so a blocked tracker doesn't lose it:

app/actions/rate.ts
"use server";

import { Databuddy } from "@databuddy/sdk/node";

const databuddy = new Databuddy({
  apiKey: process.env.DATABUDDY_API_KEY!,
  websiteId: process.env.DATABUDDY_WEBSITE_ID,
});

export async function rate(url: string, opinion: "good" | "bad", message: string) {
  await databuddy.track({
    name: "feedback_submitted",
    properties: { url, opinion, has_message: message.trim().length > 0, message_length: message.trim().length },
  });
  await databuddy.flush();
}

What it sends

EventPropertyValue
feedback_submittedurlThe page rated
opiniongood or bad
has_messageWhether a message was written
message_lengthIts length; the text itself isn't sent

Track each rating in one place, not both, or it's counted twice.

How is this guide?