Course contentsShow
← React
Lesson 2260 of 2,39649. Performance: Identifying ProblemsFree lesson

Reporting Metrics to Analytics

Send web-vitals data to Google Analytics, Vercel Analytics, or custom endpoints for aggregation and analysis.

Reporting Metrics to Analytics

What you'll learn: How to send web-vitals data to analytics platforms so you can monitor real-world performance over time.

Collecting Core Web Vitals with the web-vitals library is only half the story—the real power comes from aggregating those metrics across all your users and analyzing trends. You need to send that data somewhere it can be stored, queried, and visualized.

Why Report Metrics?

Think of performance metrics like temperature readings from a thermometer. One reading tells you how hot it is right now, but tracking readings over time tells you if there's a fever, a pattern, or seasonal changes. Analytics platforms aggregate thousands of user sessions so you can spot regressions, compare demographics, or validate optimizations.

Sending to Google Analytics

import { getCLS, getFID, getLCP } from 'web-vitals';

function sendToGoogleAnalytics({ name, delta, id }) {
  // Assumes Google Analytics is loaded as `gtag`
  gtag('event', name, {
    event_category: 'Web Vitals',
    value: Math.round(name === 'CLS' ? delta * 1000 : delta),
    event_label: id,
    non_interaction: true,
  });
}

getCLS(sendToGoogleAnalytics);
getFID(sendToGoogleAnalytics);
getLCP(sendToGoogleAnalytics);

Sending to Vercel Analytics or Custom Endpoints

function sendToVercel(metric) {
  const body = JSON.stringify(metric);
  const url = 'https://vitals.vercel-analytics.com/v1/vitals';
  
  navigator.sendBeacon(url, body);
}

getCLS(sendToVercel);

Or send to your own API:

function sendToCustomAPI(metric) {
  fetch('/api/analytics', {
    method: 'POST',
    body: JSON.stringify(metric),
    keepalive: true, // Ensures request completes even if page unloads
  });
}

The Pattern

Every analytics solution uses the same flow: capture the metric callback, format the data, and send it via fetch() or navigator.sendBeacon(). The destination changes, but the pattern stays consistent.

Key Takeaway: Reporting web-vitals to analytics platforms turns local measurements into actionable insights by aggregating real user performance data across your entire audience.