Use Real User Monitoring (RUM) to collect Web Vitals from actual users. Tools like Google Analytics, Datadog, or custom solutions track metrics over time. Set up alerting for regressions, segment data by device/geography, and correlate with business metrics.
Monitoring Approaches:
Synthetic Monitoring:
Real User Monitoring (RUM):
Key Metrics:
Tools:
import { onLCP, onINP, onCLS, onFCP, onTTFB } from 'web-vitals';
// Send metrics to analytics
function sendToAnalytics(metric) {
const body = JSON.stringify({
name: metric.name,
value: metric.value,
rating: metric.rating, // 'good', 'needs-improvement', 'poor'
delta: metric.delta,
id: metric.id,
navigationType: metric.navigationType,
// Additional context
url: window.location.href,
userAgent: navigator.userAgent,
connection: navigator.connection?.effectiveType,
deviceMemory: navigator.deviceMemory,
timestamp: Date.now(),
});
// Use sendBeacon for reliability
if (navigator.sendBeacon) {
navigator.sendBeacon('/api/analytics', body);
} else {
fetch('/api/analytics', {
method: 'POST',
body,
keepalive: true,
});
}
}
// Collect all Core Web Vitals
onLCP(sendToAnalytics);
onINP(sendToAnalytics);
onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onTTFB(sendToAnalytics);
// Custom performance marks
performance.mark('app-interactive');
performance.measure('time-to-interactive', 'navigationStart', 'app-interactive');
const tti = performance.getEntriesByName('time-to-interactive')[0];
sendToAnalytics({ name: 'TTI', value: tti.duration });