Tracking recipes
Feedback tracking
Record each rating as one event, from whichever side handles the submit.
"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:
"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
Track each rating in one place, not both, or it's counted twice.
Related
How is this guide?