Collect Web Vitals RUM
CrUX gives you an origin-level p75 once a month; your own RUM gives you the failing route, the device class and the element. Own the pipeline so you can segment it.
Procedure
Install the library and subscribe to every metric, not just LCP:
import {onLCP, onINP, onCLS, onFCP, onTTFB} from 'web-vitals/attribution';
Include attribution so a regression names the cause, not just the number:
onLCP(m => send(m), true);
function send(m) {
navigator.sendBeacon('/rum', JSON.stringify({
name: m.name, value: m.value, delta: m.delta, id: m.id,
route: location.pathname, conn: navigator.connection?.effectiveType,
attribution: m.attribution
}));
}
Sample at high traffic (for example 10%) rather than dropping to aggregate-only; a fixed sample keeps query cost predictable.
Include the connection type, device memory and the app version so a deploy that regresses a route is attributable within minutes.
Send on visibilitychange to hidden as a fallback, since some metrics only finalise when the page is backgrounded.
Store raw rows with a TTL; compute p75 per route per day in a batch job rather than on the write path.
Pitfalls
- Sending one beacon per metric and per delta, flooding the endpoint; debounce and batch.
- Counting hard navigate + bfcache restore as two samples for the same
id, skewing toward fast reloads.
- Computing a mean instead of a p75; Core Web Vitals are explicitly p75-defined and a mean hides the bad tail.
- Losing samples when the beacon fires during unload; use
sendBeacon or fetch(..., {keepalive:true}), not XMLHttpRequest.
- No app version in the payload, so you cannot tell whether a regression started before or after the release.
Verification
curl -s 'http://localhost:8080/rum' -H 'content-type: application/json' \
-d '{"name":"LCP","value":2100,"id":"v1-1","route":"/p/1"}' -o /dev/null -w '%{http_code}\n'
A 204 from the collector means the beacon is accepted. Report the routes receiving samples and the p75 LCP per device class.
1---2name: collect-web-vitals-rum3description: Use when you need per-route, per-device field vitals you own. Instruments Core Web Vitals with attribution and ships them to your own endpoint.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/collect-web-vitals-rum6---78# Collect Web Vitals RUM910CrUX gives you an origin-level p75 once a month; your own RUM gives you the failing route, the device class and the element. Own the pipeline so you can segment it.1112## Procedure13141. Install the library and subscribe to every metric, not just LCP:1516 import {onLCP, onINP, onCLS, onFCP, onTTFB} from 'web-vitals/attribution';17182. Include attribution so a regression names the cause, not just the number:1920 onLCP(m => send(m), true);21 function send(m) {22 navigator.sendBeacon('/rum', JSON.stringify({23 name: m.name, value: m.value, delta: m.delta, id: m.id,24 route: location.pathname, conn: navigator.connection?.effectiveType,25 attribution: m.attribution26 }));27 }28293. Sample at high traffic (for example 10%) rather than dropping to aggregate-only; a fixed sample keeps query cost predictable.304. Include the connection type, device memory and the app version so a deploy that regresses a route is attributable within minutes.315. Send on `visibilitychange` to hidden as a fallback, since some metrics only finalise when the page is backgrounded.326. Store raw rows with a TTL; compute p75 per route per day in a batch job rather than on the write path.3334## Pitfalls3536- Sending one beacon per metric and per delta, flooding the endpoint; debounce and batch.37- Counting hard navigate + bfcache restore as two samples for the same `id`, skewing toward fast reloads.38- Computing a mean instead of a p75; Core Web Vitals are explicitly p75-defined and a mean hides the bad tail.39- Losing samples when the beacon fires during unload; use `sendBeacon` or `fetch(..., {keepalive:true})`, not `XMLHttpRequest`.40- No app version in the payload, so you cannot tell whether a regression started before or after the release.4142## Verification4344 curl -s 'http://localhost:8080/rum' -H 'content-type: application/json' \45 -d '{"name":"LCP","value":2100,"id":"v1-1","route":"/p/1"}' -o /dev/null -w '%{http_code}\n'4647A 204 from the collector means the beacon is accepted. Report the routes receiving samples and the p75 LCP per device class.