Real User Monitoring and Web Vitals
Capture LCP, FID, CLS, and custom performance marks as OTel metrics using the web-vitals library.
Real User Monitoring and Web Vitals is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The web-vitals Library
Google's web-vitals npm package provides a simple API for measuring Core Web Vitals in real user browsers. Install it with npm install web-vitals and import individual metric callbacks: onLCP, onFID (deprecated), onCLS, onFCP, onTTFB, and the modern onINP for Interaction to Next Paint.
Metric Callbacks and Metric Objects
Each callback (onLCP, onCLS, onINP, etc.) receives a metric object containing: name ('LCP'), value (milliseconds or unitless score), rating ('good', 'needs-improvement', 'poor'), and id (a unique identifier for deduplication). The callback fires once the metric value is finalized.
Reporting Web Vitals to an Analytics Endpoint
Use the Navigator.sendBeacon API or fetch to report metrics to your analytics endpoint. sendBeacon is preferred because it continues even after the page unloads — critical for metrics like CLS that finalize on page hide. Batch all metric reports into a single beacon when possible.
Sending Web Vitals as OTel Metrics
To integrate Web Vitals into OpenTelemetry, create Gauge instruments for each metric: const lcp = meter.createObservableGauge('web_vitals.lcp'). In each onLCP callback, record the value with relevant attributes (page path, device type). Your observability backend can then alert on P75 LCP regressions.
Custom React Performance Marks
For measuring React-specific performance not captured by Web Vitals, use the User Timing API: performance.mark('render-start') before rendering, performance.mark('render-end') after, then performance.measure('component-render', 'render-start', 'render-end'). These measures appear in Chrome DevTools Performance tab.
React Profiler API
The React Profiler component wraps a subtree and calls its onRender callback with timing data: id (Profiler id), phase ('mount' or 'update'), actualDuration (render time), baseDuration (estimated render time without memoization). Use this in development to identify slow components.
Error Tracking as OTel Spans
Unhandled errors and Promise rejections can be captured as OTel spans with error status. Set up global listeners: window.addEventListener('error', handler) and window.addEventListener('unhandledrejection', handler). Create a span, set span.setStatus({ code: SpanStatusCode.ERROR }), add the error message and stack trace as attributes, then end the span.
React Error Boundaries Integration
React Error Boundaries catch rendering errors in the component tree. In the componentDidCatch method, create an OTel span representing the error, set its status to ERROR, attach the error.message and componentStack as attributes, and call span.end(). This routes React rendering errors into your distributed trace pipeline.
Source Maps for Stack Trace Resolution
Minified production JavaScript produces unreadable stack traces like "a.b at bundle.min.js:1:84721". Upload source maps to Sentry, Datadog, or your OTel pipeline during your CI/CD build process so that raw stack traces are automatically resolved to the original TypeScript file names and line numbers.
Sampling RUM Sessions
Sending full trace data for every session is expensive. Implement session sampling: randomly assign a sessionSampled flag on first page load and store it in sessionStorage. Only initialize the OTel SDK and send traces for sampled sessions (e.g., 10%). Always sample sessions with errors at 100%.
Alerting on Web Vital Regressions
Web Vitals reported as OTel metrics enable SLO-based alerting: if P75 LCP exceeds 2.5 seconds for more than 5% of sessions over a 1-hour window, fire an alert. This gives engineering teams actionable signals tied directly to user experience rather than backend infrastructure metrics.
Web Vitals onLCP Callback
When does the onLCP callback from the web-vitals library fire?
Lesson Recap
The web-vitals library provides simple callbacks for measuring LCP, INP, CLS, FCP, and TTFB in real user browsers. Report these as OTel metrics for SLO-based alerting, capture errors via global listeners and React Error Boundaries as OTel spans, and upload source maps to resolve minified stack traces. Sample sessions to manage data volume while preserving 100% error coverage.
Frequently asked questions
Is the “Real User Monitoring and Web Vitals” lesson free?
Yes — the full text of “Real User Monitoring and Web Vitals” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Real User Monitoring and Web Vitals”?
Capture LCP, FID, CLS, and custom performance marks as OTel metrics using the web-vitals library. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Real User Monitoring and Web Vitals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Frontend Observability: What and Why
- OpenTelemetry Traces from the Browser
- Real User Monitoring and Web Vitals
- Correlating Frontend and Backend Traces