Tracking recipes
Form tracking
Call one function after each submission to record whether it worked.
import { track } from "@databuddy/sdk";
import { useCallback } from "react";
export function useFormTracking(formName: string) {
return useCallback(
(success: boolean, errors: Record<string, string[]> = {}) => {
const errorCount = Object.keys(errors).length;
track("form_submit", {
form_name: formName,
success,
error_count: errorCount,
has_errors: errorCount > 0,
});
},
[formName]
);
}export function ContactForm() {
const trackSubmit = useFormTracking("contact");
async function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
try {
await sendMessage(new FormData(event.currentTarget));
trackSubmit(true);
} catch {
trackSubmit(false, { general: ["Couldn't send"] });
}
}
return <form onSubmit={handleSubmit}>{/* fields */}</form>;
}What it sends
Track failed submissions too, so you can compare completion and error rates. With autocapture.interactions on, the tracker also reports the last field touched in abandoned forms.
Related
How is this guide?