Dev Blog

BLOG

Collecting Core Web Vitals from real users in Better Stack

We started collecting Core Web Vitals measured on real users' devices in Better Stack. Why we didn't use Vercel Speed Insights, how the values are sent, a dashboard built around the share of "good" results, and an issue where values never arrived when a page with a Service Worker was closed.

Published October 8, 2026

I started measuring how fast Metronome.fm loads on real users' devices in production. Core Web Vitals such as LCP, INP and CLS are sent to Better Stack, so I can compare them from one deploy to the next.

This started when I shipped a change meant to make pages load faster and had no way to tell whether it actually did. All I could do at the time was measure the HTML size locally. Whether users noticed any difference, I couldn't say.

This post covers why I chose this setup, how the values are sent, and how the Better Stack dashboard is built. Along the way I also found that values got lost when a page controlled by a Service Worker was closed, so that comes at the end.

Why not Vercel Speed Insights

Metronome.fm runs on Vercel, so Vercel Speed Insights was the first option. On the free tier, though, you only get the overall score (Real Experience Score). To see LCP, INP and CLS separately you need Speed Insights Plus, which is available only on the Pro and Enterprise plans. On Pro it costs $10 per project per month, plus a charge based on the number of events.

The Speed Insights pricing table in the Vercel docs. Number 1 marks Real Experience Score (RES) only in the Metrics row for free Speed Insights, number 2 marks $10 per project, per month on Pro in the Price row for Speed Insights Plus.
Speed Insights pricing (Vercel docs, captured on October 8, 2026)
  1. The free tier only shows the overall score
  2. Per-metric data needs Pro, from $10 a month

The free tier also caps you at 10,000 events over the last 30 days. If you go over, collection pauses for at least 14 days. For comparing each metric before and after a change, that wasn't enough.

I was already sending error logs to Better Stack, including a source (Better Stack's unit for receiving logs) for browser logs, so I sent the measurements there too.

Collecting with useReportWebVitals in Next.js

Next.js has useReportWebVitals, which hands you Core Web Vitals as they are measured. Under the hood it runs the web-vitals library (Next.js 16.3.6 bundles v4) and passes each metric to your callback.

I send five metrics: TTFB, FCP, LCP, INP and CLS. web-vitals v4 also reports FID, but FID was dropped from Core Web Vitals in favor of INP, so I throw it away.

Each value carries some extra fields for slicing the data later.

  • page: the page that was measured. Saved settings pages (/{settingId}) have a different URL for every user, so they are grouped as /[settingId]. This also avoids storing the URLs of other people's settings
  • commit: which deploy took the measurement, as the first 7 characters of the deploy's commit
  • viewportWidth: the screen width, used to tell phones from computers
  • effectiveType: a rough estimate of connection speed (Chrome-based browsers only)
const SETTING_PATH = /^\/[A-Za-z0-9_-]{21}$/;

export const pageOf = (pathname: string): string => {
  const trimmed =
    pathname.length > 1 && pathname.endsWith("/")
      ? pathname.slice(0, -1)
      : pathname;
  if (SETTING_PATH.test(trimmed)) return "/[settingId]";
  return trimmed.slice(0, MAX_PAGE_LENGTH);
};

The measured page comes from the URL in the navigation entry (performance.getEntriesByType("navigation")), not from location.pathname. INP and CLS arrive when you leave the page, and by then location may already point to a page you moved to without reloading.

I expected Better Stack's library (@logtail/next), which I use to send logs, to attach the commit for me. But the browser logs in production had no Vercel deploy information, such as the commit, at all. So next.config.ts now passes VERCEL_GIT_COMMIT_SHA from build time to the browser.

env: {
  NEXT_PUBLIC_COMMIT_SHA: (process.env.VERCEL_GIT_COMMIT_SHA ?? "").slice(0, 7),
},

Sending one batch when the tab is hidden

Values are queued and sent together with navigator.sendBeacon when the tab is hidden. sendBeacon is designed to work even while the page is being closed. Since each page view usually needs just one request, this also cuts down on traffic.

To send everything in one batch, the request has to go out after INP and CLS have arrived. web-vitals reports those two from a visibilitychange listener on document. Metronome.fm's code listens for the same event on window. The event travels from document up to window, so by the time the window listener runs, INP and CLS are already in the queue.

useEffect(() => {
  if (!enabled) return;
  const onVisibilityChange = () => {
    if (document.visibilityState === "hidden") queue.flush();
  };
  window.addEventListener("visibilitychange", onVisibilityChange);
  return () =>
    window.removeEventListener("visibilitychange", onVisibilityChange);
}, []);

I don't send on pagehide. When you leave a page, pagehide fires before visibilitychange, so sending there would leave INP and CLS, which arrive afterward, to go out in a separate request.

Viewing it in Better Stack

Better Stack dashboard charts can't read log lines directly. You first have to extract numbers and labels from the logs in the source settings (Extract metrics).

I split the numbers into web_vital_ms for the four metrics other than CLS (in milliseconds) and web_vital_cls for CLS (unitless). Mixing values with different units and scales into one metric wouldn't give a useful distribution. The labels are the metric name, page, commit, device (a screen narrower than 768 counts as a phone) and the rating that web-vitals assigns (good, needs-improvement, poor).

The web_vital_ms settings under Extract metrics in Better Stack. Number 1 marks the SQL expression that extracts fields.value when the message is Web vital and the metric is not CLS, number 2 marks Avg, Histogram under Aggregated with.
Extracting a number from the logs (web_vital_ms)
  1. Take the values other than CLS from the measurement logs
  2. Keep the average and the distribution (histogram)

Keeping the distribution lets the dashboard show p75 (the 75th percentile: the value 75 percent of the way up when the values are sorted). But that p75 is an estimate computed from the histogram buckets, and the buckets are set by Better Stack, so you can't change them. The table below compares it with the exact p75 calculated from the logs, using data from my local testing (not from real users).

Metric, device
Exact p75
p75 from the histogram
FCP, desktop
840 ms
909 ms
TTFB, mobile
619 ms
750 ms
INP, mobile
56 ms
75 ms
LCP, desktop
1,372 ms
1,375 ms
LCP, mobile
4,464 ms
4,375 ms

The averages all matched the exact values. p75, on the other hand, was off by 20 to 30 percent for some rows, such as TTFB and INP on mobile. That is too coarse to tell whether a change made things 10 percent faster.

So the dashboard focuses on the share of measurements rated good. It only counts events, so it is exact. The thresholds for good are also the same ones Google uses for Core Web Vitals (2.5 s for LCP, 200 ms for INP, 0.1 for CLS, and so on). p75 is still there, labeled as an estimate, as a rough guide to the trend.

The top row of the Better Stack dashboard. Number 1 marks the line chart of the share rated good per metric, number 2 marks the line chart of estimated p75 for TTFB, FCP, LCP and INP.
Top row of the dashboard (data from local testing)
  1. Share rated good. This is the main chart to watch
  2. Estimated p75, as a rough guide to the trend

Before-and-after comparisons happen in a table grouped by commit. Each deploy adds a row with the number of page views and the share rated good for each metric.

The first row of the per-commit comparison table in Better Stack. Number 1 marks the commit 1063d87, number 2 marks LCP good % at 63.2, INP good % at 100 and CLS good % at 100.
Per-commit comparison table (data from local testing)
  1. The deploy's commit
  2. Share rated good for each metric

When I need the exact p75, I can run quantileExact(0.75) against the logs. On the free plan, logs are kept for only 3 days, so longer comparisons rely on the dashboard numbers.

Values lost when closing a page with a Service Worker

While testing with a local production build, CLS sometimes didn't arrive after I interacted with the app (/app) and then navigated to another page, which closes the /app page. On the landing page and the docs it always arrived. The difference was the Service Worker: Metronome.fm uses one to cache the app in the browser so it opens offline.

In Playwright I repeated the same steps, changing only whether the Service Worker was allowed. With it blocked, CLS arrived all 4 times. With it allowed, it arrived 0 times out of 4. Switching from the fetch that @logtail/next used at first to sendBeacon barely changed this. sendBeacon returned true even on the runs that never arrived, so the browser said it had accepted the request, yet nothing reached the server. Sending by hand while the page stayed open worked, so only requests made while the page was closing were lost.

The Metronome.fm Service Worker does nothing with non-GET requests. Even so, the browser first routes every request from a page the Service Worker controls through the Service Worker's fetch event. My guess is that requests made while the page is closing get lost during that detour.

So I used the Service Worker Static Routing API to send POST requests straight to the network without firing the event.

sw.addEventListener("install", (event) => {
  const routes =
    event.addRoutes?.({
      condition: { requestMethod: "POST" },
      source: "network",
    }) ?? Promise.resolve();
  event.waitUntil(
    Promise.all([
      routes.catch(() => {
        // If registration fails, requests still pass through the fetch handler as before
      }),
      precache(),
    ]).then(() => sw.skipWaiting()),
  );
});

After that, all five metrics arrived on every one of 12 page views: 8 with the Service Worker and 4 without. addRoutes is supported from Chrome and Edge 123 and Safari 27, but not yet in Firefox (MDN compatibility data, checked on October 8, 2026). I only tested in Chromium.

Error logs are also sent in a batch when the page closes. Since I started using the Service Worker, errors right before closing /app were probably lost the same way. I expect the same change fixed that too, but I haven't verified it.

Share this post

Post on X