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:

javascript
1window.trackEvent('plan_upgraded', { 2 plan: 'pro', 3 billing: 'annual', 4 seats: 5 5});

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, or boolean.

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:

typescript
1// types/analytics.d.ts 2declare global { 3 interface Window { 4 trackEvent?: ( 5 name: string, 6 data?: Record<string, string | number | boolean | null> 7 ) => void; 8 optOutAnalytics?: () => void; 9 optInAnalytics?: () => void; 10 isOptedOut?: () => boolean; 11 } 12} 13 14export {};

Tracking recipes

1. SaaS onboarding & activation

typescript
1// Track user signup 2window.trackEvent?.('user_registered', { 3 method: 'github', 4 referral_campaign: 'spring2026' 5}); 6 7// Track feature activation 8window.trackEvent?.('report_exported', { 9 format: 'csv', 10 row_count: 500 11});

2. E-commerce & checkout

typescript
1// Add product to cart 2window.trackEvent?.('cart_item_added', { 3 product_id: 'prod_102', 4 category: 'footwear', 5 price: 89.99 6}); 7 8// Completed order 9window.trackEvent?.('order_completed', { 10 order_id: 'ord_9841', 11 total_amount: 179.98, 12 coupon_applied: true 13});

3. Media & content engagement

typescript
1window.trackEvent?.('video_milestone_reached', { 2 video_id: 'intro_walkthrough', 3 percentage: 75 4});

Automatic Core Web Vitals

The tracker listens to browser performance metrics via the PerformanceObserver API, with no external dependencies.

MetricWhat it measuresGoodNeeds improvementPoor
LCP (Largest Contentful Paint)Loading speed and perceived performance≤ 2.5s2.5s – 4.0s> 4.0s
CLS (Cumulative Layout Shift)Visual stability and unexpected layout jumps≤ 0.10.1 – 0.25> 0.25
INP (Interaction to Next Paint)UI responsiveness and interaction latency≤ 200ms200ms – 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:

javascript
1// Disable tracking for this visitor (clears local session storage) 2window.optOutAnalytics(); 3 4// Re-enable tracking if the visitor gives consent 5window.optInAnalytics(); 6 7// Check current opt-out status (returns boolean) 8if (window.isOptedOut()) { 9 console.log('Visitor has disabled analytics.'); 10}

When a visitor is opted out:

  • All pageview beacons are suppressed.
  • window.trackEvent() calls become safe no-ops.
  • Web Vitals performance observers are disconnected.

Next steps