Project: build a RUM dashboard
Build the course store's own RUM: web-vitals beacons with attribution, a collector that checks them, p75s by segment, a dashboard, and an alert.
- 01Send one beacon per visitUse web-vitals' attribution build to collect LCP, INP and CLS with what caused them, and send them once, when the page is hidden.
- 02Run a collector that keeps out bad dataCheck every beacon's origin, size and fields, keep only what you know, and store each visit as one line of JSON.
- 03Read p75s by segment, and alert on a regressionWork out percentiles for each device, connection and page, draw them on a dashboard, and compare each week with the last.
From a visit to an alert, in five files
Ravi fixed the Leaf and Kettle store in the last project. His report ended with a promise: each week, read the p75s from our RUM, and alert if one goes over its line. RUM, real user monitoring, means measuring real visits, in real browsers. But the store has no RUM of its own yet. The field numbers in that project came from the course's recorder.
So if the store gets slow again next month, who notices first? Probably a customer. Ravi wants to hear it from his own numbers, in the same week. What does he need to build?
Here is where the project ends. Three weeks after Ravi starts, a small script of his prints this:
Compared 17 p75s, skipped 3 with fewer than 20 visits.LCP, all visits: p75 484 ms last week, 876 ms this weekLCP, device phone: p75 540 ms last week, 1,680 ms this weekLCP, connection 4g: p75 392 ms last week, 544 ms this weekLCP, page /: p75 520 ms last week, 1,376 ms this week- Line 1 says how many numbers the script compared, and how many it skipped.
- Lines 2 to 5 each name a group of visits whose p75 got more than 20% worse than last week. The p75 is the 75th percentile: three visits in four were at that number or better (lesson 3.4).
- Line 3 is the loudest. For phones, the time until the biggest picture appeared, LCP, went from 540 ms to 1,680 ms.
Something went wrong in week 3, and Ravi's script noticed. This project builds that script, and everything it needs. Here is the whole of it:
A home weather station has a sensor in the garden. It measures where the weather happens, and radios each reading to a base station indoors. The base station logs every reading and draws a chart of the week. And when it gets colder than usual, an alarm beeps, so nobody has to watch the chart all night.
- In real life: The sensor in the garden
- In web performance: rum.js, measuring in the visitor's browser
- In real life: One reading, sent by radio
- In web performance: One beacon per visit
- In real life: The base station that logs each reading
- In web performance: collector.mjs and beacons.jsonl
- In real life: The chart of the week on the wall
- In web performance: dashboard.html
- In real life: The frost alarm
- In web performance: alert.mjs
A RUM pipeline has five parts. A page script sends one beacon per visit. A collector checks it and stores it. Queries turn the stored beacons into percentiles by segment. A dashboard draws them. An alert compares each week with the last.
The project has five steps, one file each:
- Step 1,
rum.js: the page sends one beacon per visit. - Step 2,
collector.mjs: a small server checks each beacon and stores it. - Step 3,
queries.js: percentiles for each group of visits. - Step 4,
dashboard.html: a page that draws them. - Step 5,
alert.mjs: a script that compares this week with last week.
Build each file on your own copy of the store, the one lesson 45.1 left. Every file is published, so you can compare yours after each step:
- Step 1: rum.js, and the store's new second page, about.html, with its styles.css.
- Step 2: collector.mjs. It's a Node.js server, so download it and run it on your computer.
- Step 3: queries.js, and beacons.jsonl, the 360 beacons the course stored.
- Step 4: dashboard.html. It works right here, over those beacons.
- Step 5: alert.mjs.
How the course recorded it
- The store: lesson 45.1's store as its project left it, with the RUM line on each page and a second page, Our story. It was served over HTTPS at a made-up address, shop.example.
- The collector: the published
collector.mjs, run as it is, behind HTTPS at rum.example. - The visits: three weeks, with 120 new visitors each week: 84 on phones and 36 on computers. Each got its own CPU speed and network, drawn the way lesson 13.1 drew them. One visit in four went to the Our story page. Every visit was made in Chrome 157. Chrome's own code turned each visit's round trip into its connection type: 272 ms or more is 3g.
- The servers: the store had three, taking turns. In week 3, the third one served the wrong branch.
- The clock: the course made the three weeks of visits in under an hour. For each week, it moved the collector's clock on by seven days. The collector's code didn't change.
Step 1: one beacon per visit
The first file runs in the visitor's browser. It measures the visit, and sends the numbers home once. A beacon is that one small message.
Get the library first. rum.js uses the web-vitals library's attribution build, which lesson 12.1 explained. Pin one version, as the course does: 6.2.3. From its npm package, copy dist/web-vitals.attribution.js next to rum.js. Served from your own site, it adds no other site to every visit (lesson 24.1).
Each page of the store gets one new line, at the end of the body:
}, { once: true }); </script> <script type="module" src="rum.js" async></script></body>- Lines 1 and 2 are the end of the chat button's script, from lesson 45.1.
- Line 3 is new.
type="module"letsrum.jsimport the library (lesson 23.2).asyncruns it as soon as it's ready, without making the page wait for it (lesson 23.1).
The Our story page gets the same line. It's a short page of text, with no button. It gives the store a second page to compare.
What the visit is
rum.js is 25 lines long. Here is its start:
// rum.js: sends one beacon per visit, with attribution, when the page is hidden.import { onCLS, onINP, onLCP } from "./web-vitals.attribution.js"; const COLLECTOR = "https://rum.example/beacon";const beacon = { page: location.pathname, device: matchMedia("(max-width: 600px)").matches ? "phone" : "desktop", connection: navigator.connection?.effectiveType ?? "unknown",};- Line 2 imports three functions from the library.
- Line 4 is where beacons go: the collector, at rum.example. That's another site.
- Lines 5 to 9 start the beacon with three facts about the visit. Line 6 is the page's path, with no query string, as lesson 12.3 advised. Line 7 is the device, from the screen's width. Line 8 is the browser's guess at the network.
Each of these is a segment: a way to split visits into groups, such as phones and computers (lesson 12.5). Why it matters: decide your segments now. A fact the page never sent can't be a segment later.
Only Chromium browsers report the network. Other browsers' beacons say "unknown":
What it measured, and what caused it
onLCP(({ value, attribution: a }) => { beacon.lcp = { value, element: a.target ?? null, parts: [a.timeToFirstByte, a.resourceLoadDelay, a.resourceLoadDuration, a.elementRenderDelay] };});onINP(({ value, attribution: a }) => { beacon.inp = { value, element: a.interactionTarget ?? null, parts: [a.inputDelay, a.processingDuration, a.presentationDelay] };});onCLS(({ value, attribution: a }) => { beacon.cls = { value, element: a.largestShiftTarget ?? null };});- Lines 1 to 3 keep LCP: its value in ms, the element, and its four parts (lesson 17.2).
- Lines 4 to 6 keep INP, how long a tap took to show its result: the element tapped, and its three parts (lesson 26.2).
- Lines 7 to 9 keep CLS, the score for how much the page jumped, and the first element the largest jump moved (lesson 30.4).
This extra detail is called attribution. Why it matters: it turns "LCP got worse" into "the picture took longer to download".
Send it once
let sent = false;addEventListener("visibilitychange", () => { if (document.visibilityState !== "hidden" || sent) return; sent = navigator.sendBeacon(COLLECTOR, JSON.stringify(beacon));});- Line 1 remembers whether the beacon has gone.
- Lines 2 and 3 wait for the page to be hidden: the visitor switched tabs, or left. Lesson 12.3 showed that it's the last moment a page can count on.
- Line 4 hands the beacon to the browser with
sendBeacon(). It gives backtrueonce the browser has queued it. If it gives backfalse, the next hide tries again.
The library listens for the same event, but in the capture phase, so its handlers run first. Its last numbers are already in beacon when line 4 sends it.
A hotel asks for one feedback card per stay. You tick the boxes as you go: the room, breakfast, the pool. You hand the card in at checkout. The hotel counts stays, not meals, because every stay gives one card.
- In real life: Ticking the boxes during the stay
- In web performance: web-vitals filling in the beacon
- In real life: Handing the card in at checkout
- In web performance: Sending the beacon when the page is hidden
- In real life: One card per stay, not one per meal
- In web performance: One beacon per visit, kept with the
sentflag
Visits to the Our story page have no tap, so their beacons have no INP. Here is one real beacon from week 3. Click through its body, the headers it travelled with, and the line the collector stored:
written by rum.js{
"page": "/",
"device": "phone",
"connection": "3g",
"lcp": {
"value": 2412,
"element": "#hero",
"parts": [
442.1000000014901,
35.5,
1477.5,
456.8999999985099
]
},
"inp": {
"value": 408,
"element": "#add",
"parts": [
0.30000000447034836,
222.39999999850988,
185.29999999701977
]
},
"cls": {
"value": 0.1369496950521829,
"element": "html>body>main"
}
}The beacon's body, as the collector received it, set out on several lines here so you can read it. Times are in ms.
The 360 beacons weighed 160 to 337 bytes each. That's far under the 64 KiB a page may queue (lesson 12.3).
Step 2: a collector that checks every beacon
The second file is a small Node.js server: collector.mjs. It listens at one endpoint, an address that takes requests: /beacon. Its job is to take beacons, check them, and store the good ones (lesson 12.4).
Anyone on the internet can post to that address: an old page, a broken script, a bot. Every bad line that gets into storage ends up in your percentiles. So the collector trusts nothing.
A ballot box has a slot that fits one folded paper, and nothing bigger. Only official papers count. A spoiled paper is put aside. Nobody writes their name on a ballot. And the counting happens later, from the papers in the box.
- In real life: The slot that fits one folded paper
- In web performance: The 2,048-byte limit
- In real life: Only official ballot papers count
- In web performance: Only the fields and values the collector knows
- In real life: No names on the ballot
- In web performance: No personal data in a beacon
- In real life: Counting happens later, from the papers
- In web performance: Queries read the stored lines later
The door
The first checks run before the collector reads a single byte of the body:
const answer = (status, headers = {}) => response.writeHead(status, headers).end();if (request.url !== "/beacon") return answer(404);if (request.method !== "POST") return answer(405, { allow: "POST" });if (request.headers.origin !== SITE) return answer(403);if (Number(request.headers["content-length"]) > MAX_BYTES) return answer(413);- Line 1 is a helper. It answers with a status code and an empty body.
- Line 2 answers
404to any other address. Line 3 answers405to anything but a POST. MDN says a 405 must name the allowed methods in anAllowheader, so it does. - Line 4 answers
403unless theOriginheader names the store. Browsers add it to every POST, and a page's script can't change it. - Line 5 answers
413 Content Too Largewhen the body says it's over 2,048 bytes.
A program outside a browser can send any Origin it likes. So line 4 stops other sites' pages, not a person with a script. The checks that follow are what protect the data.
The size limit
const chunks = [];let size = 0;for await (const chunk of request) { size += chunk.length; if (size <= MAX_BYTES) chunks.push(chunk);}if (size > MAX_BYTES) return answer(413);- Lines 3 to 6 read the body piece by piece, and count its bytes.
- Line 5 keeps at most 2,048 bytes in memory, however big the body is.
- Line 7 answers
413to a body that grew too big without saying its size first.
The real beacons were at most 337 bytes. A limit of 2,048 leaves room for a bigger page, and keeps a flood of data out.
Keep only what you know
// Builds the line to store from the fields it knows, or gives back null if something is wrong.function clean(body) { if (!DEVICES.includes(body?.device)) return null; const beacon = { page: PAGES.includes(body.page) ? body.page : "other", device: body.device, connection: CONNECTIONS.includes(body.connection) ? body.connection : "unknown", };- Line 3 gives up unless the device is
phoneordesktop. The server then answers400 Bad Request. - Line 4 starts a new object. Line 5 keeps the page only if it's one the store has. Any other path becomes
"other". Line 7 does the same for the connection.
The rest of clean() checks each metric the same way. Its value must be a number from 0 to a believable largest value. Its element must be empty or short text. Its parts must be numbers, and the right count.
Building a new object is the part that matters. Nothing from the body reaches the file unless clean() copied it on purpose.
Store it
if (!beacon) return answer(400); await appendFile(FILE, `${JSON.stringify({ time: Date.now(), ...beacon })}\n`);answer(204);- Line 1 answers
400whenclean()gave up, or the body wasn't JSON. - Line 3 adds one line to the file: the time, from the server's clock, then the checked fields, as JSON.
- Line 4 answers
204 No Content: stored, nothing to send back.
One JSON object per line is called JSON Lines. Lesson 12.4 chose it for raw events too: adding a line never touches the others. Node.js 22 also has a built-in database, node:sqlite. It works without a flag, but Node still marks it as experimental.
What the course sent it
After the visits, the course sent a second copy of the collector the requests a broken client or a bot might send. Read each row across:
| What the course sent | What the collector answered | Why |
|---|---|---|
| A GET request | 405 | Only POST is allowed. The answer names it in an Allow header. |
| Another path | 404 | The collector only listens at /beacon. |
| Another site's page | 403 | The Origin header isn't the store's. |
| A body of 5,000 bytes | 413 | The body said it was bigger than 2,048 bytes. |
| The same body, without saying its size | 413 | The body didn't say its size, and grew past 2,048 bytes. |
| Not JSON | 400 | The body isn't JSON. |
| An unknown device | 400 | tablet isn't a device rum.js sends. |
| A value that is text | 400 | An LCP must be a number. |
| An extra field with an email address | 204, and one line stored | Every known field passed. The email field was left out of the line. |
The last row matters most. The beacon was valid, so it was stored. But its email address never reached the file. Why it matters: your store holds only what you chose to keep.
Watch for CORS
The store is at shop.example, and the collector is at rum.example: another site. Browsers guard requests to another site with CORS, cross-origin resource sharing. For some requests, the browser first asks the other site for permission, with an OPTIONS request. That question is called a preflight.
rum.js sends a string. The Fetch standard sends a string as text/plain;charset=UTF-8, a type it lists as safe. So the Beacon standard sends it with no preflight, in no-cors mode. All 360 beacons arrived that way: one POST each, with Sec-Fetch-Mode: no-cors.
// A string: Chrome sends it as text/plain, straight away.navigator.sendBeacon(COLLECTOR, JSON.stringify(beacon)); // A Blob marked as JSON: Chrome asks the collector first, with an OPTIONS request.navigator.sendBeacon(COLLECTOR, new Blob([JSON.stringify(beacon)], { type: "application/json" }));- Line 2 is what rum.js does. Chrome sent it straight away.
- Line 5 wraps the same text in a
Blobmarked as JSON. Chrome sent anOPTIONSrequest first.
The collector has no rule for OPTIONS, so it answered 405. Chrome never sent the beacon itself. Yet sendBeacon() had given back true: queued is not the same as delivered. A keepalive fetch() with a JSON content type met the same preflight, and failed with TypeError: Failed to fetch.
Why it matters: send a string, and have the collector read the body as JSON, whatever its content type says. Then it needs no CORS rules at all.
Together, these are the collector's safety habits:
- Nothing personal. The page sends its path, not its query string. The collector stores no address, cookie or browser name. Here the beacons arrived with no cookie, and a
Refererof just the store's address. - Check every field. Known fields, known values, numbers in range. Copy them into a new object.
- Limit the size. Refuse a big body before reading it, and stop keeping bytes past the limit.
- Watch for CORS. A string body needs no preflight. A JSON content type does.
Step 3: percentiles by segment
After three weeks, beacons.jsonl held 360 lines, one per visit: 101,632 bytes. A file of lines doesn't answer anything yet. queries.js turns it into numbers. The dashboard and the alert both import it, so they always agree.
One method, named
A percentile can be worked out in several ways. Lesson 13.1 showed three. Pick one, and write down which. queries.js uses nearest rank: sort the values, then take the one at rank Math.ceil(p × n). It always gives back a real visit's value.
// The LCPs of the 8 computers that met the old build, in msconst lcps = [1068, 480, 628, 632, 1968, 828, 980, 768];const percentile = (values, p) => [...values].sort((a, b) => a - b)[Math.ceil(p * values.length) - 1]; console.log(`p75 of ${lcps.length} visits: ${percentile(lcps, 0.75)} ms`);- Line 2 holds the LCPs of the computers that met the old build in week 3, in the order they came.
- Line 3 is the same nearest rank as
queries.js. It sorts a copy, and picks rankMath.ceil(0.75 × 8), which is 6. - Line 5 prints
p75 of 8 visits: 980 ms
One week at a time
// One week of beacons: 0 is the 7 days up to `now`, 1 is the 7 days before those, and so on.
export const weekOf = (beacons, now, weeksAgo) =>
beacons.filter((beacon) => beacon.time <= now - weeksAgo * WEEK && beacon.time > now - (weeksAgo + 1) * WEEK);Line 3 keeps the beacons whose time falls in a seven-day window. weeksAgo 0 is the last seven days before now. 1 is the seven days before those.
Split by a segment
// One metric, split by a segment such as "device": a row for each value of it, the most visits first.export function bySegment(beacons, metric, segment) { const groups = {}; for (const beacon of beacons) { if (!beacon[metric]) continue; const key = segment ? beacon[segment] : "all"; (groups[key] ??= []).push(beacon[metric].value); }- Line 3 starts an empty set of groups.
- Line 5 skips beacons without this metric, such as the Our story page's INP.
- Line 6 picks the group: the beacon's device, connection or page. With no segment, every beacon goes in one group,
"all". - Line 7 adds the value to its group.
Then bySegment() works out each group's count, p50, p75 and p95, and the share of good and poor visits.
A head teacher looks at the whole school's exam results, and they look fine. Then she looks class by class. One class did much worse than last year, and the others did a little better, so the school's results hid it. She also checks how many pupils are in each class. A class of four can swing a lot by luck.
- In real life: The whole school's results
- In web performance: The p75 of all visits
- In real life: Each class's results
- In web performance: The p75 of each segment
- In real life: One class that struggled
- In web performance: Phones on the Mint tea page in week 3
- In real life: How many pupils in the class
- In web performance: The count next to each p75
Here are the recorded p75s, for each segment and each week. Pick a way to split the visits:
All visits| Metric | Segment | Week 1 | Week 2 | Week 3 |
|---|---|---|---|---|
| LCP | all | 528 ms (120) | 484 ms (120) | 876 ms (120) |
| INP | all | 56 ms (90) | 56 ms (90) | 96 ms (90) |
| CLS | all | 0.00 (120) | 0.00 (120) | 0.00 (120) |
Each cell is a week's p75 by nearest rank, and how many visits it came from. Week 3 is the week of the bad deploy.
Weeks 1 and 2 look alike. In week 3, phones' LCP went from 540 ms to 1,680 ms. Computers' barely moved: from 188 ms to 220 ms.
Now look at CLS. For all visits, the p75 in week 3 was still 0.00. For phones, it was 0.14. How? 30 of the 120 visits jumped: exactly a quarter. The p75 of all visits is the 90th value in line, and 90 visits didn't jump at all. On phones, 22 of 84 visits jumped, a little more than a quarter.
Why it matters: the p75 of all visits can hide a problem that one segment shows plainly.
Step 4: a dashboard page
dashboard.html is a static page: one HTML file, with no server code of its own. It imports queries.js, loads the stored beacons, and draws a row for each group.
const beacons = parse(await (await fetch("beacons.jsonl")).text());const now = Math.max(...beacons.map((beacon) => beacon.time));const week = document.querySelector("#week");const segment = document.querySelector("#segment"); // The beacons came from the internet, so every text from them is escaped before it's shown.const escape = (text) => String(text).replace(/[&<>"]/g, (c) => `&#${c.charCodeAt(0)};`);- Line 1 fetches the file of beacons, and turns each line back into an object.
- Line 2 takes
nowfrom the newest beacon. The weeks count back from it, so the page works on any copy of the data. - Lines 3 and 4 find the two pickers: which week, and which segment.
- Line 7 escapes text: it turns
<,>,&and"into codes that show as plain characters.
Why escape? The element names came from the internet. The collector checks their length, not what's inside them. A "name" full of HTML would run inside your dashboard. OWASP's advice is to encode such text before you put it in a page.
Each row shows the visits, the p75, a bar against Google's good and poor lines, and the share of good, needs improvement, and poor visits. That last bar is the distribution: how the visits spread out (lesson 3.4). Under each metric, it names the element the slow visits point at most. Here is the INP part, last week and this week:
Phones' INP p75 went from 56 ms to 280 ms, past the good line. All 17 taps over the line name #add, the Add to cart button. Open the published dashboard to try every week and split yourself.
Here is LCP for every segment, last week against this week:
Every segment got slower, but by very different amounts. Why it matters: a dashboard that only showed all visits would have shown a smaller rise, and no clue where it came from. Splits like these are where lesson 40.1's audit started.
Step 5: an alert when a week gets worse
A dashboard helps only when someone looks at it. alert.mjs looks for Ravi. He runs it once a week. It compares this week's p75 with last week's, for every group, and prints the ones that got worse. Here is the heart of it:
const before = bySegment(lastWeek, metric, segment);for (const after of bySegment(thisWeek, metric, segment)) { const last = before.find((row) => row.value === after.value); if (!last || last.count < MIN_VISITS || after.count < MIN_VISITS) { skipped++; continue; } checked++; if (after.p75 > last.p75 * (1 + WORSE_BY)) {- Line 1 works out last week's rows. Line 2 goes through this week's, one by one.
- Line 3 finds the same group in last week.
- Lines 4 to 7 skip the group when either week has fewer than 20 visits in it. A p75 of a few visits is mostly luck, as lesson 39.3 showed.
- Line 9 fires when this week's p75 is more than 20% worse: bigger than last week's times 1.2.
At the end, the script prints its findings. It also ends with exit code 1 when something fired, and 0 when nothing did. A scheduler or a CI job can read that code, and send Ravi a message.
A smoke alarm that goes off for every slice of burnt toast soon loses its battery. One that waits for thick smoke is too late for a real fire. You set it somewhere in between, and you still check the kitchen when it beeps.
- In real life: Burnt toast
- In web performance: A small change from luck, or from who visited
- In real life: A real fire
- In web performance: A real regression
- In real life: How sensitive the alarm is
- In web performance: The threshold, WORSE_BY
- In real life: Taking the battery out
- In web performance: Ignoring alerts that fire too often
Week 2: quiet
Compared 17 p75s, skipped 6 with fewer than 20 visits.
No alerts.Nothing changed in the store that week, and nothing fired. Every p75 it compared was the same or better. It ended with exit code 0.
Week 3: something broke
Compared 17 p75s, skipped 3 with fewer than 20 visits.LCP, all visits: p75 484 ms last week, 876 ms this weekLCP, device phone: p75 540 ms last week, 1,680 ms this weekLCP, connection 4g: p75 392 ms last week, 544 ms this weekLCP, page /: p75 520 ms last week, 1,376 ms this weekLCP, page /about.html: p75 396 ms last week, 604 ms this weekINP, all visits: p75 56 ms last week, 96 ms this weekINP, device phone: p75 56 ms last week, 280 ms this weekINP, device desktop: p75 40 ms last week, 88 ms this weekINP, connection 4g: p75 56 ms last week, 88 ms this weekINP, page /: p75 56 ms last week, 96 ms this weekCLS, device phone: p75 0.00 last week, 0.14 this weekCLS, page /: p75 0.00 last week, 0.07 this week- Line 1: it skipped the three groups for 3g. Last week had only 14 visits on 3g.
- Lines 2 to 13 are 12 alerts. Three name phones, and three the Mint tea page,
/. Most of the rest are bigger groups that hold those visits too. - Line 12 is CLS for phones: from 0 to 0.14. No CLS line fired for all visits: its p75 stayed at 0.
It ended with exit code 1. So Ravi gets the message in week 3, not when a customer complains.
His report promised an alert when a p75 goes over its line. Comparing with last week caught more. Phones' LCP p75 of 1,680 ms is still under Google's 2,500 ms line, yet it more than tripled.
One line is odd. Line 6 names the Our story page, and no deploy touched it. Split its visits by connection. 6 of its 30 visits came on a slow connection this week, against 2 last week. Its visits on 4g got faster: 356 ms to 308 ms. The page didn't change. Its visitors did. Lesson 13.1 called this a change in the traffic mix.
Why it matters: an alert is a reason to look, not a verdict.
Where to set the threshold
Move the threshold, and watch both weeks:
17 checked, 6 skippedNo alerts.
- Kept quiet by the minimum: LCP, connection 3g, 1,024 ms to 1,236 ms, from 16 and 14 visits.
17 checked, 3 skipped- LCP, all visits484 ms to 876 ms
- LCP, device phone540 ms to 1,680 ms
- LCP, connection 4g392 ms to 544 ms
- LCP, page /520 ms to 1,376 ms
- LCP, page /about.html396 ms to 604 ms
- INP, all visits56 ms to 96 ms
- INP, device phone56 ms to 280 ms
- INP, device desktop40 ms to 88 ms
- INP, connection 4g56 ms to 88 ms
- INP, page /56 ms to 96 ms
- CLS, device phone0.00 to 0.14
- CLS, page /0.00 to 0.07
- Kept quiet by the minimum: LCP, connection 3g, 1,236 ms to 2,096 ms, from 14 and 23 visits.
- Kept quiet by the minimum: INP, connection 3g, 56 ms to 288 ms, from 12 and 17 visits.
- Kept quiet by the minimum: CLS, connection 3g, 0.00 to 0.14, from 14 and 23 visits.
Nothing changed in the store between weeks 1 and 2, so any alert there would be a false alarm. A group is only compared when it has at least 20 visits in both weeks.
Week 2 never fires. But look at its 3g visits. Their LCP p75 rose 21%, from 1,024 ms to 1,236 ms, from only 14 visits. With no minimum, a 20% rule would have fired, in a week when nothing changed. The playground lists groups like that one as kept quiet by the minimum.
In week 3, the minimum also kept the 3g groups quiet, though they really had got worse. And a higher threshold quiets the Our story page. It also quiets real alerts, such as LCP on 4g. Why it matters: every rule misses something. Pick one, and then read what it tells you.
From the alert to the cause
The alert says where. The attribution says why. Here is what the dashboard named for this week's slow visits:
- LCP: all 12 visits over the good line name
#hero, the picture. On every phone that met the old build, the biggest of LCP's four parts was the download. - INP: all 17 name
#add, the Add to cart button. - CLS: all 22 name
html>body>main, the main area, pushed down.
Those are the three problems lesson 45.1 fixed: the heavy picture, the slow button and the late promotion. So the old store was back. Ravi checks the deploy log, and finds the wrong branch on the third server. He deploys the right one.
Next time he could find it faster. If rum.js also sent which build served the page, as lesson 39.3 did with the release, the dashboard could split by build.
Which part does it?
Read this table across: each row is one file of the project.
| File | What does it do? | Which lessons taught the idea? |
|---|---|---|
rum.js | Sends one beacon per visit, with attribution, when the page is hidden. | Lessons 12.1, 12.3 and 12.5. |
collector.mjs | Checks each beacon's origin, size and fields, and stores one line. | Lessons 12.3 and 12.4. |
queries.js | Works out each segment's percentiles, by nearest rank. | Lessons 3.4, 13.1 and 12.5. |
dashboard.html | Draws each segment's p75 and spread against Google's lines. | Lessons 3.1, 3.4 and 39.3. |
alert.mjs | Compares each p75 with last week's, and skips small groups. | Lessons 12.4 and 39.3. |
Now sort each job into the part of the pipeline that does it:
- Reports the page's path, without its query string
- Sends the visit's numbers once, when the page is hidden
- Drops an email address someone put in a beacon
- Answers 413 to a body of 5,021 bytes
- Picks the 75th percentile by nearest rank
- Escapes element names before it shows them
- Skips a segment with only 14 visits last week
- Ends with exit code 1 when something got worse
Sort each job into the part of the pipeline that does it.
Common mix-ups
- "If sendBeacon gives back true, the beacon arrived." No. True means the browser queued it. The JSON Blob beacon gave back true, and never reached the collector.
- "A JSON content type is the proper way to send JSON." No. To another site, it needs a preflight, and your collector must answer it. A plain string needs none.
- "Checking the Origin header keeps out fake beacons." No. It stops other sites' pages. A script outside a browser can send any Origin. Checking every field is what protects the data.
- "The p75 of all visits shows every problem." No. In week 3, CLS for all visits stayed at 0, while phones' rose to 0.14.
- "Every alert is a regression." No. The Our story page fired because more of its visitors came on slow connections. The page hadn't changed.
- "You need a big analytics product to do RUM." No. Five small files collected, stored, charted and watched 360 visits here.
Practice exercises
Answer first, then use the hints. Some use the playgrounds above, and one uses the published dashboard.
These are the Our story page's visits on 3g in week 3. What does line 5 print?
// The LCPs of the Our story page's 6 visits on 3g in week 3, in ms
const lcps = [1680, 1980, 876, 2276, 648, 744];
const percentile = (values, p) => [...values].sort((a, b) => a - b)[Math.ceil(p * values.length) - 1];
console.log(`p75 of ${lcps.length} visits: ${percentile(lcps, 0.75)} ms`);p75 of 6 visits: 1980 ms. Sorted, the fifth of six values is the p75 by nearest rank.
In the beacon playground, how many bytes did that beacon's body weigh?
311 bytes: the body exactly as rum.js sent it, before the collector added the time.
This is the collector's rule for two fields, on one odd body. What does line 7 print?
const PAGES = ["/", "/about.html"];
const CONNECTIONS = ["slow-2g", "2g", "3g", "4g"];
const body = { page: "/teas?id=5", device: "phone", connection: "5g" };
const page = PAGES.includes(body.page) ? body.page : "other";
const connection = CONNECTIONS.includes(body.connection) ? body.connection : "unknown";
console.log(`${page}, ${connection}`);other, unknown. Neither value is on its list, so each gets the collector's safe default.
Ravi adds this line to the collector, to make sure every body is JSON. Which status code does every beacon from rum.js get now?
// Added after the Origin check in collector.mjs
if (request.headers["content-type"] !== "application/json") return answer(415);415, for every beacon. rum.js sends a string, which travels as text/plain. The new line refuses it, so nothing is stored.
What is the lowest threshold at which the Our story page's LCP line no longer fires in week 3?
55%. Its p75 rose 53%, so any threshold above that leaves it out. LCP on 4g has gone quiet by then too.
Open the published dashboard. How many visits came on 3g in week 1?
16. Fewer than 20, so alert.mjs skipped 3g after week 2.
Check your understanding
For each question, ask yourself which file it's about.
Question 1 of 7Why does rum.js send its beacon when the page becomes hidden, and only once?
Choose an answer to see the explanation.
Question 2 of 7rum.js sends a string with sendBeacon to another site. Why does Chrome send it without asking the collector first?
Choose an answer to see the explanation.
Question 3 of 7A beacon arrives with every field rum.js sends, plus an email field. What does the collector store?
Choose an answer to see the explanation.
Question 4 of 7Why does alert.mjs skip a segment with fewer than 20 visits in either week?
Choose an answer to see the explanation.
Question 5 of 7What does this nearest-rank program print?
Read the code, then predict// Eight made-up LCPs, in ms const lcps = [400, 600, 500, 900, 700, 800, 300, 1200]; const percentile = (values, p) => [...values].sort((a, b) => a - b)[Math.ceil(p * values.length) - 1]; console.log(`p75 of ${lcps.length} visits: ${percentile(lcps, 0.75)} ms`);Choose an answer to see the explanation.
Question 6 of 7In week 3, the alert fired for the Our story page, which no deploy had touched. What explains it?
Choose an answer to see the explanation.
Question 7 of 7Why does the dashboard escape element names before it puts them on the page?
Choose an answer to see the explanation.
Key takeaways
- Send one beacon per visit, with attribution, when the page is hidden.
- Send the beacon as a string: it needs no CORS preflight.
- Check the origin and the size, keep only the fields you know, and store nothing personal.
- Name your percentile method, and work it out for each segment, with its count.
- Escape anything from a beacon before a page shows it.
- Compare each week with the last, skip small groups, and treat an alert as a reason to look.
Remember the one-liner.
Send one beacon per visit, check it at the door, keep it raw, read p75s by segment, and alert when a week gets worse.
Sources
The facts on this page come from these standards, docs and source files:
- web-vitals, Google Chrome on GitHub.
- getVisibilityWatcher.ts (web-vitals 6.2.3), Google Chrome on GitHub.
- Navigator: sendBeacon() method, MDN.
- Beacon, W3C.
- Fetch Standard: CORS-safelisted request-header, WHATWG.
- Fetch Standard: extracting a body, WHATWG.
- Sec-Fetch-Mode header, MDN.
- Origin header, MDN.
- 413 Content Too Large, MDN.
- 405 Method Not Allowed, MDN.
- HTTP: http.createServer(), Node.js docs.
- SQLite, Node.js docs.
- JSON Lines, jsonlines.org.
- Cross Site Scripting Prevention Cheat Sheet, OWASP.
- network_state_notifier.cc: SetNetworkConnectionInfoOverride (Chromium 157.0.8103.52), Chromium source.
- Web Vitals, web.dev.
Coming next: Project: rescue a slow app's INP. You'll find the slowest taps in a heavy app, and make them fast.