Skip to Content

Tracking

Web vitals

LCP, INP, CLS, FCP and TTFB from @obelism/numbers-sdk: rated on Google’s thresholds, by route and by release, with the slowest elements.

3 min read

The Web vitals page of the dashboard shows how fast your site is for real visitors: LCP, INP and CLS at the 75th percentile against Google's thresholds, by route and by release, with the elements that were slowest. It reads one event per metric, so any site can send them.

Turn them on

@obelism/numbers-sdk sends them from autoTrack. The web-vitals package comes with it, in its own chunk, so a site that leaves webVitals out downloads nothing extra.

import { createNumbers } from '@obelism/numbers-sdk'

const numbers = createNumbers({
	organizationId: 'org_…',
	spaceId: 'space_…',
})

numbers.autoTrack({
	webVitals: {
		release: COMMIT_SHA, // the deploy, e.g. process.env.NEXT_PUBLIC_COMMIT_SHA
		sampleRate: 0.5, // half the visits report
	},
})
  • release is the site's deploy, e.g. the commit it was built from. With it the report names the release that made a metric slower; without it, the release stays empty and the report only has routes.
  • sampleRate (0 to 1) is the part of the visits that report, decided once per tab and sent as sample. Percentiles don't change with it; it keeps the event volume down, and tells you how much of your traffic the numbers cover.
  • route grouping comes from the routes you gave createNumbers (or the script's data-routes): a path with an id in it reports as its route, so /content/[entryId] is one row instead of one per entry.

How each metric is counted

  • LCP, FCP and TTFB describe the page the visit landed on.
  • INP and CLS keep growing until the visit ends. They are filed under the page open when they arrive, so each route of a client-side router reports its own.
  • Elements are the largest element painted, the element that was interacted with, and the element that shifted most, as a CSS selector with ids made from a record shown as #[id].
  • Development: the metrics go to the environment the client was created with, as events do. Send them from develop on localhost, or leave webVitals out there, so a laptop's numbers don't land in production's.

In a component

There is no React or Vue package for this: it is one call in an effect, and the client already follows the router's navigation. In React (a client component in Next's App Router too):

'use client'

import { useEffect } from 'react'
import { numbers } from './numbers'

export const WebVitals = () => {
	useEffect(() => {
		void numbers.webVitals({
			release: process.env.NEXT_PUBLIC_COMMIT_SHA,
			sampleRate: 0.5,
		})
	}, [])

	return null
}

numbers.webVitals() starts the metrics alone, without the page views and clicks of autoTrack: use it when the site tracks its own, so nothing is counted twice. It resolves to a function that stops the reporting, which you only need if the component can unmount for good.

In Vue, with onMounted:

import { onMounted } from 'vue'
import { numbers } from './numbers'

onMounted(() => {
	void numbers.webVitals({ release: __COMMIT__, sampleRate: 0.5 })
})

The script tag

The script is one file, so it can't carry the web-vitals chunk. Pass its ES module as load; a CDN serves it:

<script defer src="https://numbers.obelism.studio/script.js" data-organization="org_…" data-space="space_…"></script>
<script type="module">
	numbers.webVitals({
		release: 'abc1234',
		sampleRate: 0.5,
		load: () => import('https://esm.sh/web-vitals@6/attribution'),
	})
</script>

Without the SDK

The whole contract is one event per metric, with the name of the metric as its scope. Whatever measures the metric can send it:

numbers.track('web_vital', 'LCP', {
	value: 2100, // milliseconds; CLS is unitless
	params: {
		rating: 'good', // good, needs-improvement or poor
		release: 'abc1234',
		element: 'main>img.hero', // optional
	},
})

A params.rating you send is kept, but the dashboard rates each metric itself on Google's thresholds (LCP good up to 2500 ms, INP 200, CLS 0.1, FCP 1800, TTFB 800), so the two agree only when you rate the same way.

On this page

Marks in the margin

  • Note
  • Watch out
  • Updated

Scribes marked doubtful lines in the margin. That practice is called obelism.