Tracking recipes

Form tracking

Track form submissions to understand conversion rates, validation errors, and user behavior patterns.

Implementation

tsx
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

tsx
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

  • Event: form_submit
  • Properties:
    • form_name: Name identifier for the form
    • success: Whether submission was successful
    • error_count: Number of validation errors
    • has_errors: Boolean indicating if errors occurred

Track both successful and failed submissions so you can compare completion and error rates.

Choose the completion event before instrumenting the form. The conversion funnel comparison explains how a form fits into a multi-step journey, while the UTM tracking walkthrough covers the campaign context for that conversion.

How is this guide?