Tracking Recipes
Form Tracking
Track form submissions to understand conversion rates, validation errors, and user behavior patterns.
Implementation
import { track } from "@databuddy/sdk";
import { useCallback } from "react";
export function useFormTracking(formName: string) {
const trackSubmit = useCallback(
(success: boolean, errors?: Record<string, string[]>) => {
track("form_submit", {
form_name: formName,
success,
error_count: errors ? Object.keys(errors).length : 0,
has_errors: !!errors && Object.keys(errors).length > 0,
});
},
[formName]
);
return { trackSubmit };
}Usage
function ContactForm() {
const { trackSubmit } = useFormTracking("contact");
const handleSubmit = async (data: FormData) => {
try {
await submitForm(data);
trackSubmit(true);
toast.success("Message sent!");
} catch (error) {
trackSubmit(false, { general: ["Failed to send message"] });
toast.error("Failed to send message");
}
};
return <form onSubmit={handleSubmit}>...</form>;
}What It Tracks
Benefits
Related
How is this guide?