Custom Events, Web Vitals & Privacy APIs
Beyond pageviews, diy-analytics gives you a client-side API for tracking custom actions — signups, purchases, clicks — plus automatic Core Web Vitals monitoring and privacy consent controls.
Tracking custom events
Call window.trackEvent(name, data) anywhere in your client code, once the tracker has loaded:
Parameters
name(string, required) — a descriptive identifier for the event (e.g.signup_completed,checkout_started,video_played). Max 64 characters.data(object, optional) — a flat key-value object of metadata. Values must be scalar:string,number, orboolean.
Keep event payloads flat. Scalar values are extracted and aggregated automatically for breakdown charts in your dashboard. Payloads over 8 KB are sanitized and truncated on ingestion.
TypeScript definitions
For full type safety in a Next.js or TypeScript frontend, add this ambient declaration to globals.d.ts or types/analytics.d.ts:
Tracking recipes
1. SaaS onboarding & activation
2. E-commerce & checkout
3. Media & content engagement
Event names starting with a double underscore (__cls, __lcp, __inp) are reserved for internal metric telemetry. Don't prefix custom events with __.
Automatic Core Web Vitals
The tracker listens to browser performance metrics via the PerformanceObserver API, with no external dependencies.
| Metric | What it measures | Good | Needs improvement | Poor |
|---|---|---|---|---|
| LCP (Largest Contentful Paint) | Loading speed and perceived performance | ≤ 2.5s | 2.5s – 4.0s | > 4.0s |
| CLS (Cumulative Layout Shift) | Visual stability and unexpected layout jumps | ≤ 0.1 | 0.1 – 0.25 | > 0.25 |
| INP (Interaction to Next Paint) | UI responsiveness and interaction latency | ≤ 200ms | 200ms – 500ms | > 500ms |
Vitals are aggregated and reported at the 75th percentile (P75) on your project dashboard, under Web Vitals.
Visitor opt-out & privacy APIs
If you run a custom cookie or privacy preferences modal, diy-analytics gives you methods to respect visitor choices:
When a visitor is opted out:
- All pageview beacons are suppressed.
window.trackEvent()calls become safe no-ops.- Web Vitals performance observers are disconnected.