Chapter 24

Analytics

FormAnalytics turns form activity into a single stream of metadata-only events. Values, value lengths and error message text never appear in it, so a careless handler can't leak user data.

import { mountPixiForm, analyticsAdapters } from 'pixiform';

await mountPixiForm(host, {
  id: 'signup',
  fields: [/* … */],
  onSubmit,
  analytics: {
    onEvent: analyticsAdapters.ga4(),              // or .dataLayer(), .segment(), .posthog(), .plausible(), .beacon(url)
    consent: () => consentBanner.analyticsAllowed, // checked before every event; denied events are dropped
    sampleRate: 1,                                 // track this fraction of form views
    excludeFields: ['taxId'],                      // never named in events
  },
});

With a hand-built form: const tracking = new FormAnalytics(ui, form, { formId: 'signup', onEvent }), and call tracking.destroy() when done. With <pixi-form>, setting analytics: {} makes every event also bubble as a pixiform-analytics DOM event, which you can forward to Google Tag Manager:

el.addEventListener('pixiform-analytics', (e) => dataLayer.push({ event: `pixiform_${e.detail.type}`, pixiform: e.detail }));

24.1 Events#

Event When Extra fields
form_view ≥30% of the form is on screen (once) totalFields
form_start First trusted interaction
field_focus A field gains focus field, fieldType, group
field_complete A field loses focus after being edited durationMs, changes
field_error A field gets a new error errorCode
group_toggle A FieldGroup is folded or opened group, expanded
submit_attempt Submit pressed attempt, durationMs (since start)
submit_invalid Client validation failed errorCount, errorFields
submit_success / submit_failure Server answered attempt, durationMs / errorFields
submit_blocked Rate limit or bot check reason: 'rate_limit' or 'bot'
form_idle Secrets wiped after inactivity
form_abandon Page hidden or form destroyed after a start without success lastField, completedFields, totalFields, durationMs

Every event also carries formId, formSessionId (a random id per form instance, not a user id) and t (ms since mount). fieldType is text, email, tel, password, otp, checkbox, switch, combobox, radiogroup, slider and so on. errorCode is required, invalid, server (from fieldErrors) or your validator's code:

const noFreeMail: Validator<string> = Object.assign((v: string) => (/@(gmail|yahoo)\./.test(v) ? 'Use a work email.' : null), { code: 'free_email' });

From these you can build a funnel (view → start → attempt → success), find drop-off fields (form_abandon.lastField), spot the fields with the most errors (field_error by field and errorCode) and measure time per field.

24.2 Adapters#

Adapter Sends
ga4({ prefix?, gtag? }) gtag('event', 'pixiform_field_error', { form_id, field, error_code, … }). Prefixed so it doesn't clash with GA4's own form_start/form_submit.
dataLayer({ name?, prefix? }) dataLayer.push({ event: 'pixiform_submit_success', pixiform: {…} }) for Google Tag Manager.
segment(client?) analytics.track('PixiFormUI Field Error', props) (Segment, RudderStack, …).
posthog(client?) posthog.capture('pixiform_field_error', props).
plausible(fn?) plausible('pixiform_submit_success', { props }).
beacon(url) Batched JSON via navigator.sendBeacon to your endpoint (survives page unload).
combine(...handlers) Several destinations at once.

handle.analytics.track(type, data) sends custom events through the same consent, sampling and allow-list checks. Remember to add your analytics hosts to the CSP connect-src (and script-src for vendor scripts).