UNPKG

49.3 kBMarkdownView Raw
1# `web-vitals`
2
3- [Overview](#overview)
4- [Install and load the library](#installation)
5 - [From npm](#import-web-vitals-from-npm)
6 - [From a CDN](#load-web-vitals-from-a-cdn)
7- [Usage](#usage)
8 - [Basic usage](#basic-usage)
9 - [Report the value on every change](#report-the-value-on-every-change)
10 - [Report only the delta of changes](#report-only-the-delta-of-changes)
11 - [Send the results to an analytics endpoint](#send-the-results-to-an-analytics-endpoint)
12 - [Send the results to Google Analytics](#send-the-results-to-google-analytics)
13 - [Send the results to Google Tag Manager](#send-the-results-to-google-tag-manager)
14 - [Send attribution data](#send-attribution-data)
15 - [Batch multiple reports together](#batch-multiple-reports-together)
16- [Build options](#build-options)
17 - [Which build is right for you?](#which-build-is-right-for-you)
18- [API](#api)
19 - [Types](#types)
20 - [Functions](#functions)
21 - [Rating Thresholds](#rating-thresholds)
22 - [Attribution](#attribution)
23- [Browser Support](#browser-support)
24- [Limitations](#limitations)
25- [Development](#development)
26- [Integrations](#integrations)
27- [License](#license)
28
29## Overview
30
31The `web-vitals` library is a tiny (~2K, brotli'd), modular library for measuring all the [Web Vitals](https://web.dev/articles/vitals) metrics on real users, in a way that accurately matches how they're measured by Chrome and reported to other Google tools (e.g. [Chrome User Experience Report](https://developers.google.com/web/tools/chrome-user-experience-report), [Page Speed Insights](https://developers.google.com/speed/pagespeed/insights/), [Search Console's Speed Report](https://webmasters.googleblog.com/2019/11/search-console-speed-report.html)).
32
33The library supports all of the [Core Web Vitals](https://web.dev/articles/vitals#core_web_vitals) as well as a number of other metrics that are useful in diagnosing [real-user](https://web.dev/articles/user-centric-performance-metrics) performance issues.
34
35### Core Web Vitals
36
37- [Cumulative Layout Shift (CLS)](https://web.dev/articles/cls)
38- [Interaction to Next Paint (INP)](https://web.dev/articles/inp)
39- [Largest Contentful Paint (LCP)](https://web.dev/articles/lcp)
40
41### Other metrics
42
43- [First Contentful Paint (FCP)](https://web.dev/articles/fcp)
44- [Time to First Byte (TTFB)](https://web.dev/articles/ttfb)
45- [First Input Delay (FID)](https://web.dev/articles/fid)
46
47> [!CAUTION]
48> FID is deprecated and will be removed in the next major release.
49
50<a name="installation"><a>
51<a name="load-the-library"><a>
52
53## Install and load the library
54
55<a name="import-web-vitals-from-npm"><a>
56
57The `web-vitals` library uses the `buffered` flag for [PerformanceObserver](https://developer.mozilla.org/docs/Web/API/PerformanceObserver/observe), allowing it to access performance entries that occurred before the library was loaded.
58
59This means you do not need to load this library early in order to get accurate performance data. In general, this library should be deferred until after other user-impacting code has loaded.
60
61### From npm
62
63You can install this library from npm by running:
64
65```sh
66npm install web-vitals
67```
68
69> [!NOTE]
70> If you're not using npm, you can still load `web-vitals` via `<script>` tags from a CDN like [unpkg.com](https://unpkg.com). See the [load `web-vitals` from a CDN](#load-web-vitals-from-a-cdn) usage example below for details.
71
72There are a few different builds of the `web-vitals` library, and how you load the library depends on which build you want to use.
73
74For details on the difference between the builds, see <a href="#which-build-is-right-for-you">which build is right for you</a>.
75
76**1. The "standard" build**
77
78To load the "standard" build, import modules from the `web-vitals` package in your application code (as you would with any npm package and node-based build tool):
79
80```js
81import {onLCP, onINP, onCLS} from 'web-vitals';
82
83onCLS(console.log);
84onINP(console.log);
85onLCP(console.log);
86```
87
88> [!NOTE]
89> In version 2, these functions were named `getXXX()` rather than `onXXX()`. They've [been renamed](https://github.com/GoogleChrome/web-vitals/pull/222) in version 3 to reduce confusion (see [#217](https://github.com/GoogleChrome/web-vitals/pull/217) for details) and will continue to be available using the `getXXX()` until at least version 4. Users are encouraged to switch to the new names, though, for future compatibility.
90
91<a name="attribution-build"><a>
92
93**2. The "attribution" build**
94
95Measuring the Web Vitals scores for your real users is a great first step toward optimizing the user experience. But if your scores aren't _good_, the next step is to understand why they're not good and work to improve them.
96
97The "attribution" build helps you do that by including additional diagnostic information with each metric to help you identify the root cause of poor performance as well as prioritize the most important things to fix.
98
99The "attribution" build is slightly larger than the "standard" build (by about 600 bytes, brotli'd), so while the code size is still small, it's only recommended if you're actually using these features.
100
101To load the "attribution" build, change any `import` statements that reference `web-vitals` to `web-vitals/attribution`:
102
103```diff
104- import {onLCP, onINP, onCLS} from 'web-vitals';
105+ import {onLCP, onINP, onCLS} from 'web-vitals/attribution';
106```
107
108Usage for each of the imported function is identical to the standard build, but when importing from the attribution build, the [metric](#metric) objects will contain an additional [`attribution`](#attribution) property.
109
110See [Send attribution data](#send-attribution-data) for usage examples, and the [`attribution` reference](#attribution) for details on what values are added for each metric.
111
112<a name="load-web-vitals-from-a-cdn"><a>
113
114### From a CDN
115
116The recommended way to use the `web-vitals` package is to install it from npm and integrate it into your build process. However, if you're not using npm, it's still possible to use `web-vitals` by requesting it from a CDN that serves npm package files.
117
118The following examples show how to load `web-vitals` from [unpkg.com](https://unpkg.com/browse/web-vitals/). It is also possible to load this from [jsDelivr](https://www.jsdelivr.com/package/npm/web-vitals), and [cdnjs](https://cdnjs.com/libraries/web-vitals).
119
120_**Important!** The [unpkg.com](https://unpkg.com), [jsDelivr](https://www.jsdelivr.com/), and [cdnjs](https://cdnjs.com) CDNs are shown here for example purposes only. `unpkg.com`, `jsDelivr`, and `cdnjs` are not affiliated with Google, and there are no guarantees that loading the library from those CDNs will continue to work in the future. Self-hosting the built files rather than loading from the CDN is better for security, reliability, and performance reasons._
121
122**Load the "standard" build** _(using a module script)_
123
124```html
125<!-- Append the `?module` param to load the module version of `web-vitals` -->
126<script type="module">
127 import {onCLS, onINP, onLCP} from 'https://unpkg.com/web-vitals@4?module';
128
129 onCLS(console.log);
130 onINP(console.log);
131 onLCP(console.log);
132</script>
133```
134
135**Load the "standard" build** _(using a classic script)_
136
137```html
138<script>
139 (function () {
140 var script = document.createElement('script');
141 script.src = 'https://unpkg.com/web-vitals@4/dist/web-vitals.iife.js';
142 script.onload = function () {
143 // When loading `web-vitals` using a classic script, all the public
144 // methods can be found on the `webVitals` global namespace.
145 webVitals.onCLS(console.log);
146 webVitals.onINP(console.log);
147 webVitals.onLCP(console.log);
148 };
149 document.head.appendChild(script);
150 })();
151</script>
152```
153
154**Load the "attribution" build** _(using a module script)_
155
156```html
157<!-- Append the `?module` param to load the module version of `web-vitals` -->
158<script type="module">
159 import {
160 onCLS,
161 onINP,
162 onLCP,
163 } from 'https://unpkg.com/web-vitals@4/dist/web-vitals.attribution.js?module';
164
165 onCLS(console.log);
166 onINP(console.log);
167 onLCP(console.log);
168</script>
169```
170
171**Load the "attribution" build** _(using a classic script)_
172
173```html
174<script>
175 (function () {
176 var script = document.createElement('script');
177 script.src =
178 'https://unpkg.com/web-vitals@4/dist/web-vitals.attribution.iife.js';
179 script.onload = function () {
180 // When loading `web-vitals` using a classic script, all the public
181 // methods can be found on the `webVitals` global namespace.
182 webVitals.onCLS(console.log);
183 webVitals.onINP(console.log);
184 webVitals.onLCP(console.log);
185 };
186 document.head.appendChild(script);
187 })();
188</script>
189```
190
191## Usage
192
193### Basic usage
194
195Each of the Web Vitals metrics is exposed as a single function that takes a `callback` function that will be called any time the metric value is available and ready to be reported.
196
197The following example measures each of the Core Web Vitals metrics and logs the result to the console once its value is ready to report.
198
199_(The examples below import the "standard" build, but they will work with the "attribution" build as well.)_
200
201```js
202import {onCLS, onINP, onLCP} from 'web-vitals';
203
204onCLS(console.log);
205onINP(console.log);
206onLCP(console.log);
207```
208
209Note that some of these metrics will not report until the user has interacted with the page, switched tabs, or the page starts to unload. If you don't see the values logged to the console immediately, try reloading the page (with [preserve log](https://developer.chrome.com/docs/devtools/console/reference/#persist) enabled) or switching tabs and then switching back.
210
211Also, in some cases a metric callback may never be called:
212
213- FID and INP are not reported if the user never interacts with the page.
214- CLS, FCP, FID, and LCP are not reported if the page was loaded in the background.
215
216In other cases, a metric callback may be called more than once:
217
218- CLS and INP should be reported any time the [page's `visibilityState` changes to hidden](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden).
219- All metrics are reported again (with the above exceptions) after a page is restored from the [back/forward cache](https://web.dev/articles/bfcache).
220
221> [!WARNING]
222> Do not call any of the Web Vitals functions (e.g. `onCLS()`, `onINP()`, `onLCP()`) more than once per page load. Each of these functions creates a `PerformanceObserver` instance and registers event listeners for the lifetime of the page. While the overhead of calling these functions once is negligible, calling them repeatedly on the same page may eventually result in a memory leak.
223
224### Report the value on every change
225
226In most cases, you only want the `callback` function to be called when the metric is ready to be reported. However, it is possible to report every change (e.g. each larger layout shift as it happens) by setting `reportAllChanges` to `true` in the optional, [configuration object](#reportopts) (second parameter).
227
228> [!IMPORTANT]
229> `reportAllChanges` only reports when the **metric changes**, not for each **input to the metric**. For example, a new layout shift that does not increase the CLS metric will not be reported even with `reportAllChanges` set to `true` because the CLS metric has not changed. Similarly, for INP, each interaction is not reported even with `reportAllChanges` set to `true`—just when an interaction causes an increase to INP.
230
231This can be useful when debugging, but in general using `reportAllChanges` is not needed (or recommended) for measuring these metrics in production.
232
233```js
234import {onCLS} from 'web-vitals';
235
236// Logs CLS as the value changes.
237onCLS(console.log, {reportAllChanges: true});
238```
239
240### Report only the delta of changes
241
242Some analytics providers allow you to update the value of a metric, even after you've already sent it to their servers (overwriting the previously-sent value with the same `id`).
243
244Other analytics providers, however, do not allow this, so instead of reporting the new value, you need to report only the delta (the difference between the current value and the last-reported value). You can then compute the total value by summing all metric deltas sent with the same ID.
245
246The following example shows how to use the `id` and `delta` properties:
247
248```js
249import {onCLS, onINP, onLCP} from 'web-vitals';
250
251function logDelta({name, id, delta}) {
252 console.log(`${name} matching ID ${id} changed by ${delta}`);
253}
254
255onCLS(logDelta);
256onINP(logDelta);
257onLCP(logDelta);
258```
259
260> [!NOTE]
261> The first time the `callback` function is called, its `value` and `delta` properties will be the same.
262
263In addition to using the `id` field to group multiple deltas for the same metric, it can also be used to differentiate different metrics reported on the same page. For example, after a back/forward cache restore, a new metric object is created with a new `id` (since back/forward cache restores are considered separate page visits).
264
265### Send the results to an analytics endpoint
266
267The following example measures each of the Core Web Vitals metrics and reports them to a hypothetical `/analytics` endpoint, as soon as each is ready to be sent.
268
269The `sendToAnalytics()` function uses the [`navigator.sendBeacon()`](https://developer.mozilla.org/docs/Web/API/Navigator/sendBeacon) method (if available), but falls back to the [`fetch()`](https://developer.mozilla.org/docs/Web/API/Fetch_API) API when not.
270
271```js
272import {onCLS, onINP, onLCP} from 'web-vitals';
273
274function sendToAnalytics(metric) {
275 // Replace with whatever serialization method you prefer.
276 // Note: JSON.stringify will likely include more data than you need.
277 const body = JSON.stringify(metric);
278
279 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
280 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
281 fetch('/analytics', {body, method: 'POST', keepalive: true});
282}
283
284onCLS(sendToAnalytics);
285onINP(sendToAnalytics);
286onLCP(sendToAnalytics);
287```
288
289### Send the results to Google Analytics
290
291Google Analytics does not support reporting metric distributions in any of its built-in reports; however, if you set a unique event parameter value (in this case, the metric_id, as shown in the example below) on every metric instance that you send to Google Analytics, you can create a report yourself by first getting the data via the [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1) or via [BigQuery export](https://support.google.com/analytics/answer/9358801) and then visualizing it any charting library you choose.
292
293[Google Analytics 4](https://support.google.com/analytics/answer/10089681) introduces a new Event model allowing custom parameters instead of a fixed category, action, and label. It also supports non-integer values, making it easier to measure Web Vitals metrics compared to previous versions.
294
295```js
296import {onCLS, onINP, onLCP} from 'web-vitals';
297
298function sendToGoogleAnalytics({name, delta, value, id}) {
299 // Assumes the global `gtag()` function exists, see:
300 // https://developers.google.com/analytics/devguides/collection/ga4
301 gtag('event', name, {
302 // Built-in params:
303 value: delta, // Use `delta` so the value can be summed.
304 // Custom params:
305 metric_id: id, // Needed to aggregate events.
306 metric_value: value, // Optional.
307 metric_delta: delta, // Optional.
308
309 // OPTIONAL: any additional params or debug info here.
310 // See: https://web.dev/articles/debug-performance-in-the-field
311 // metric_rating: 'good' | 'needs-improvement' | 'poor',
312 // debug_info: '...',
313 // ...
314 });
315}
316
317onCLS(sendToGoogleAnalytics);
318onINP(sendToGoogleAnalytics);
319onLCP(sendToGoogleAnalytics);
320```
321
322For details on how to query this data in [BigQuery](https://cloud.google.com/bigquery), or visualise it in [Looker Studio](https://lookerstudio.google.com/), see [Measure and debug performance with Google Analytics 4 and BigQuery](https://web.dev/articles/vitals-ga4).
323
324### Send the results to Google Tag Manager
325
326While `web-vitals` can be called directly from Google Tag Manager, using a pre-defined custom template makes this considerably easier. Some recommended templates include:
327
328- [Core Web Vitals](https://tagmanager.google.com/gallery/#/owners/gtm-templates-simo-ahava/templates/core-web-vitals) by [Simo Ahava](https://www.simoahava.com/). See [Track Core Web Vitals in GA4 with Google Tag Manager](https://www.simoahava.com/analytics/track-core-web-vitals-in-ga4-with-google-tag-manager/) for usage and installation instructions.
329- [Web Vitals Template for Google Tag Manager](https://github.com/google-marketing-solutions/web-vitals-gtm-template) by The Google Marketing Solutions team. See the [README](https://github.com/google-marketing-solutions/web-vitals-gtm-template?tab=readme-ov-file#web-vitals-template-for-google-tag-manager) for usage and installation instructions.
330
331### Send attribution data
332
333When using the [attribution build](#attribution-build), you can send additional data to help you debug _why_ the metric values are they way they are.
334
335This example sends an additional `debug_target` param to Google Analytics, corresponding to the element most associated with each metric.
336
337```js
338import {onCLS, onINP, onLCP} from 'web-vitals/attribution';
339
340function sendToGoogleAnalytics({name, delta, value, id, attribution}) {
341 const eventParams = {
342 // Built-in params:
343 value: delta, // Use `delta` so the value can be summed.
344 // Custom params:
345 metric_id: id, // Needed to aggregate events.
346 metric_value: value, // Optional.
347 metric_delta: delta, // Optional.
348 };
349
350 switch (name) {
351 case 'CLS':
352 eventParams.debug_target = attribution.largestShiftTarget;
353 break;
354 case 'INP':
355 eventParams.debug_target = attribution.interactionTarget;
356 break;
357 case 'LCP':
358 eventParams.debug_target = attribution.element;
359 break;
360 }
361
362 // Assumes the global `gtag()` function exists, see:
363 // https://developers.google.com/analytics/devguides/collection/ga4
364 gtag('event', name, eventParams);
365}
366
367onCLS(sendToGoogleAnalytics);
368onINP(sendToGoogleAnalytics);
369onLCP(sendToGoogleAnalytics);
370```
371
372> [!NOTE]
373> This example relies on custom [event parameters](https://support.google.com/analytics/answer/11396839) in Google Analytics 4.
374
375See [Debug performance in the field](https://web.dev/articles/debug-performance-in-the-field) for more information and examples.
376
377### Batch multiple reports together
378
379Rather than reporting each individual Web Vitals metric separately, you can minimize your network usage by batching multiple metric reports together in a single network request.
380
381However, since not all Web Vitals metrics become available at the same time, and since not all metrics are reported on every page, you cannot simply defer reporting until all metrics are available.
382
383Instead, you should keep a queue of all metrics that were reported and flush the queue whenever the page is backgrounded or unloaded:
384
385```js
386import {onCLS, onINP, onLCP} from 'web-vitals';
387
388const queue = new Set();
389function addToQueue(metric) {
390 queue.add(metric);
391}
392
393function flushQueue() {
394 if (queue.size > 0) {
395 // Replace with whatever serialization method you prefer.
396 // Note: JSON.stringify will likely include more data than you need.
397 const body = JSON.stringify([...queue]);
398
399 // Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
400 (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) ||
401 fetch('/analytics', {body, method: 'POST', keepalive: true});
402
403 queue.clear();
404 }
405}
406
407onCLS(addToQueue);
408onINP(addToQueue);
409onLCP(addToQueue);
410
411// Report all available metrics whenever the page is backgrounded or unloaded.
412addEventListener('visibilitychange', () => {
413 if (document.visibilityState === 'hidden') {
414 flushQueue();
415 }
416});
417```
418
419> [!NOTE]
420> See [the Page Lifecycle guide](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#legacy-lifecycle-apis-to-avoid) for an explanation of why `visibilitychange` is recommended over events like `beforeunload` and `unload`.
421
422<a name="bundle-versions"><a>
423
424## Build options
425
426The `web-vitals` package includes both "standard" and "attribution" builds, as well as different formats of each to allow developers to choose the format that best meets their needs or integrates with their architecture.
427
428The following table lists all the builds distributed with the `web-vitals` package on npm.
429
430<table>
431 <tr>
432 <td width="35%">
433 <strong>Filename</strong> <em>(all within <code>dist/*</code>)</em>
434 </td>
435 <td><strong>Export</strong></td>
436 <td><strong>Description</strong></td>
437 </tr>
438 <tr>
439 <td><code>web-vitals.js</code></td>
440 <td><code>pkg.module</code></td>
441 <td>
442 <p>An ES module bundle of all metric functions, without any attribution features.</p>
443 This is the "standard" build and is the simplest way to consume this library out of the box.
444 </td>
445 </tr>
446 <tr>
447 <td><code>web-vitals.umd.cjs</code></td>
448 <td><code>pkg.main</code></td>
449 <td>
450 A UMD version of the <code>web-vitals.js</code> bundle (exposed on the <code>self.webVitals.*</code> namespace).
451 </td>
452 </tr>
453 <tr>
454 <td><code>web-vitals.iife.js</code></td>
455 <td>--</td>
456 <td>
457 An IIFE version of the <code>web-vitals.js</code> bundle (exposed on the <code>self.webVitals.*</code> namespace).
458 </td>
459 </tr>
460 <tr>
461 <td><code>web-vitals.attribution.js</code></td>
462 <td>--</td>
463 <td>
464 An ES module version of all metric functions that includes <a href="#attribution-build">attribution</a> features.
465 </td>
466 </tr>
467 <tr>
468 <td><code>web-vitals.attribution.umd.cjs</code></td>
469 <td>--</td>
470 <td>
471 A UMD version of the <code>web-vitals.attribution.js</code> build (exposed on the <code>self.webVitals.*</code> namespace).
472 </td>
473 </tr>
474 </tr>
475 <tr>
476 <td><code>web-vitals.attribution.iife.js</code></td>
477 <td>--</td>
478 <td>
479 An IIFE version of the <code>web-vitals.attribution.js</code> build (exposed on the <code>self.webVitals.*</code> namespace).
480 </td>
481 </tr>
482</table>
483
484<a name="which-build-is-right-for-you"><a>
485
486### Which build is right for you?
487
488Most developers will generally want to use "standard" build (via either the ES module or UMD version, depending on your bundler/build system), as it's the easiest to use out of the box and integrate into existing tools.
489
490However, if you'd lke to collect additional debug information to help you diagnose performance bottlenecks based on real-user issues, use the ["attribution" build](#attribution-build).
491
492For guidance on how to collect and use real-user data to debug performance issues, see [Debug performance in the field](https://web.dev/debug-performance-in-the-field/).
493
494## API
495
496### Types:
497
498#### `Metric`
499
500All metrics types inherit from the following base interface:
501
502```ts
503interface Metric {
504 /**
505 * The name of the metric (in acronym form).
506 */
507 name: 'CLS' | 'FCP' | 'FID' | 'INP' | 'LCP' | 'TTFB';
508
509 /**
510 * The current value of the metric.
511 */
512 value: number;
513
514 /**
515 * The rating as to whether the metric value is within the "good",
516 * "needs improvement", or "poor" thresholds of the metric.
517 */
518 rating: 'good' | 'needs-improvement' | 'poor';
519
520 /**
521 * The delta between the current value and the last-reported value.
522 * On the first report, `delta` and `value` will always be the same.
523 */
524 delta: number;
525
526 /**
527 * A unique ID representing this particular metric instance. This ID can
528 * be used by an analytics tool to dedupe multiple values sent for the same
529 * metric instance, or to group multiple deltas together and calculate a
530 * total. It can also be used to differentiate multiple different metric
531 * instances sent from the same page, which can happen if the page is
532 * restored from the back/forward cache (in that case new metrics object
533 * get created).
534 */
535 id: string;
536
537 /**
538 * Any performance entries relevant to the metric value calculation.
539 * The array may also be empty if the metric value was not based on any
540 * entries (e.g. a CLS value of 0 given no layout shifts).
541 */
542 entries: PerformanceEntry[];
543
544 /**
545 * The type of navigation.
546 *
547 * This will be the value returned by the Navigation Timing API (or
548 * `undefined` if the browser doesn't support that API), with the following
549 * exceptions:
550 * - 'back-forward-cache': for pages that are restored from the bfcache.
551 * - 'back_forward' is renamed to 'back-forward' for consistency.
552 * - 'prerender': for pages that were prerendered.
553 * - 'restore': for pages that were discarded by the browser and then
554 * restored by the user.
555 */
556 navigationType:
557 | 'navigate'
558 | 'reload'
559 | 'back-forward'
560 | 'back-forward-cache'
561 | 'prerender'
562 | 'restore';
563}
564```
565
566Metric-specific subclasses:
567
568##### `CLSMetric`
569
570```ts
571interface CLSMetric extends Metric {
572 name: 'CLS';
573 entries: LayoutShift[];
574}
575```
576
577##### `FCPMetric`
578
579```ts
580interface FCPMetric extends Metric {
581 name: 'FCP';
582 entries: PerformancePaintTiming[];
583}
584```
585
586##### `FIDMetric`
587
588> [!CAUTION]
589> This interface is deprecated and will be removed in the next major release.
590
591```ts
592interface FIDMetric extends Metric {
593 name: 'FID';
594 entries: PerformanceEventTiming[];
595}
596```
597
598##### `INPMetric`
599
600```ts
601interface INPMetric extends Metric {
602 name: 'INP';
603 entries: PerformanceEventTiming[];
604}
605```
606
607##### `LCPMetric`
608
609```ts
610interface LCPMetric extends Metric {
611 name: 'LCP';
612 entries: LargestContentfulPaint[];
613}
614```
615
616##### `TTFBMetric`
617
618```ts
619interface TTFBMetric extends Metric {
620 name: 'TTFB';
621 entries: PerformanceNavigationTiming[];
622}
623```
624
625#### `MetricRatingThresholds`
626
627The thresholds of metric's "good", "needs improvement", and "poor" ratings.
628
629- Metric values up to and including [0] are rated "good"
630- Metric values up to and including [1] are rated "needs improvement"
631- Metric values above [1] are "poor"
632
633| Metric value | Rating |
634| --------------- | ------------------- |
635| ≦ [0] | "good" |
636| > [0] and ≦ [1] | "needs improvement" |
637| > [1] | "poor" |
638
639```ts
640type MetricRatingThresholds = [number, number];
641```
642
643_See also [Rating Thresholds](#rating-thresholds)._
644
645#### `ReportOpts`
646
647```ts
648interface ReportOpts {
649 reportAllChanges?: boolean;
650 durationThreshold?: number;
651}
652```
653
654#### `LoadState`
655
656The `LoadState` type is used in several of the metric [attribution objects](#attribution).
657
658```ts
659/**
660 * The loading state of the document. Note: this value is similar to
661 * `document.readyState` but it subdivides the "interactive" state into the
662 * time before and after the DOMContentLoaded event fires.
663 *
664 * State descriptions:
665 * - `loading`: the initial document response has not yet been fully downloaded
666 * and parsed. This is equivalent to the corresponding `readyState` value.
667 * - `dom-interactive`: the document has been fully loaded and parsed, but
668 * scripts may not have yet finished loading and executing.
669 * - `dom-content-loaded`: the document is fully loaded and parsed, and all
670 * scripts (except `async` scripts) have loaded and finished executing.
671 * - `complete`: the document and all of its sub-resources have finished
672 * loading. This is equivalent to the corresponding `readyState` value.
673 */
674type LoadState =
675 | 'loading'
676 | 'dom-interactive'
677 | 'dom-content-loaded'
678 | 'complete';
679```
680
681### Functions:
682
683#### `onCLS()`
684
685```ts
686function onCLS(callback: (metric: CLSMetric) => void, opts?: ReportOpts): void;
687```
688
689Calculates the [CLS](https://web.dev/articles/cls) value for the current page and calls the `callback` function once the value is ready to be reported, along with all `layout-shift` performance entries that were used in the metric value calculation. The reported value is a [double](https://heycam.github.io/webidl/#idl-double) (corresponding to a [layout shift score](https://web.dev/articles/cls#layout_shift_score)).
690
691If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called as soon as the value is initially determined as well as any time the value changes throughout the page lifespan (Note [not necessarily for every layout shift](#report-the-value-on-every-change)).
692
693> [!IMPORTANT]
694> CLS should be continually monitored for changes throughout the entire lifespan of a page—including if the user returns to the page after it's been hidden/backgrounded. However, since browsers often [will not fire additional callbacks once the user has backgrounded a page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden), `callback` is always called when the page's visibility state changes to hidden. As a result, the `callback` function might be called multiple times during the same page load (see [Reporting only the delta of changes](#report-only-the-delta-of-changes) for how to manage this).
695
696#### `onFCP()`
697
698```ts
699function onFCP(callback: (metric: FCPMetric) => void, opts?: ReportOpts): void;
700```
701
702Calculates the [FCP](https://web.dev/articles/fcp) value for the current page and calls the `callback` function once the value is ready, along with the relevant `paint` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
703
704#### `onFID()`
705
706> [!CAUTION]
707> This function is deprecated and will be removed in the next major release.
708
709```ts
710function onFID(callback: (metric: FIDMetric) => void, opts?: ReportOpts): void;
711```
712
713Calculates the [FID](https://web.dev/articles/fid) value for the current page and calls the `callback` function once the value is ready, along with the relevant `first-input` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
714
715> [!IMPORTANT]
716> Since FID is only reported after the user interacts with the page, it's possible that it will not be reported for some page loads.
717
718#### `onINP()`
719
720```ts
721function onINP(callback: (metric: INPMetric) => void, opts?: ReportOpts): void;
722```
723
724Calculates the [INP](https://web.dev/articles/inp) value for the current page and calls the `callback` function once the value is ready, along with the `event` performance entries reported for that interaction. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
725
726A custom `durationThreshold` [configuration option](#reportopts) can optionally be passed to control what `event-timing` entries are considered for INP reporting. The default threshold is `40`, which means INP scores of less than 40 are reported as 0. Note that this will not affect your 75th percentile INP value unless that value is also less than 40 (well below the recommended [good](https://web.dev/articles/inp#what_is_a_good_inp_score) threshold).
727
728If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called as soon as the value is initially determined as well as any time the value changes throughout the page lifespan (Note [not necessarily for every interaction](#report-the-value-on-every-change)).
729
730> [!IMPORTANT]
731> INP should be continually monitored for changes throughout the entire lifespan of a page—including if the user returns to the page after it's been hidden/backgrounded. However, since browsers often [will not fire additional callbacks once the user has backgrounded a page](https://developer.chrome.com/blog/page-lifecycle-api/#advice-hidden), `callback` is always called when the page's visibility state changes to hidden. As a result, the `callback` function might be called multiple times during the same page load (see [Reporting only the delta of changes](#report-only-the-delta-of-changes) for how to manage this).
732
733#### `onLCP()`
734
735```ts
736function onLCP(callback: (metric: LCPMetric) => void, opts?: ReportOpts): void;
737```
738
739Calculates the [LCP](https://web.dev/articles/lcp) value for the current page and calls the `callback` function once the value is ready (along with the relevant `largest-contentful-paint` performance entry used to determine the value). The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
740
741If the `reportAllChanges` [configuration option](#reportopts) is set to `true`, the `callback` function will be called any time a new `largest-contentful-paint` performance entry is dispatched, or once the final value of the metric has been determined.
742
743#### `onTTFB()`
744
745```ts
746function onTTFB(
747 callback: (metric: TTFBMetric) => void,
748 opts?: ReportOpts,
749): void;
750```
751
752Calculates the [TTFB](https://web.dev/articles/ttfb) value for the current page and calls the `callback` function once the page has loaded, along with the relevant `navigation` performance entry used to determine the value. The reported value is a [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp).
753
754Note, this function waits until after the page is loaded to call `callback` in order to ensure all properties of the `navigation` entry are populated. This is useful if you want to report on other metrics exposed by the [Navigation Timing API](https://w3c.github.io/navigation-timing/).
755
756For example, the TTFB metric starts from the page's [time origin](https://www.w3.org/TR/hr-time-2/#sec-time-origin), which means it includes time spent on DNS lookup, connection negotiation, network latency, and server processing time.
757
758```js
759import {onTTFB} from 'web-vitals';
760
761onTTFB((metric) => {
762 // Calculate the request time by subtracting from TTFB
763 // everything that happened prior to the request starting.
764 const requestTime = metric.value - metric.entries[0].requestStart;
765 console.log('Request time:', requestTime);
766});
767```
768
769> [!NOTE]
770> Browsers that do not support `navigation` entries will fall back to using `performance.timing` (with the timestamps converted from epoch time to [`DOMHighResTimeStamp`](https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp)). This ensures code referencing these values (like in the example above) will work the same in all browsers.
771
772### Rating Thresholds:
773
774The thresholds of each metric's "good", "needs improvement", and "poor" ratings are available as [`MetricRatingThresholds`](#metricratingthresholds).
775
776Example:
777
778```ts
779import {CLSThresholds, INPThresholds, LCPThresholds} from 'web-vitals';
780
781console.log(CLSThresholds); // [ 0.1, 0.25 ]
782console.log(INPThresholds); // [ 200, 500 ]
783console.log(LCPThresholds); // [ 2500, 4000 ]
784```
785
786> [!NOTE]
787> It's typically not necessary (or recommended) to manually calculate metric value ratings using these thresholds. Use the [`Metric['rating']`](#metric) instead.
788
789### Attribution:
790
791The following objects contain potentially-helpful debugging information that can be sent along with the metric values for the current page visit in order to help identify issues happening to real-users in the field.
792
793When using the attribution build, these objects are found as an `attribution` property on each metric.
794
795See the [attribution build](#attribution-build) section for details on how to use this feature.
796
797#### `CLSAttribution`
798
799```ts
800interface CLSAttribution {
801 /**
802 * A selector identifying the first element (in document order) that
803 * shifted when the single largest layout shift contributing to the page's
804 * CLS score occurred.
805 */
806 largestShiftTarget?: string;
807 /**
808 * The time when the single largest layout shift contributing to the page's
809 * CLS score occurred.
810 */
811 largestShiftTime?: DOMHighResTimeStamp;
812 /**
813 * The layout shift score of the single largest layout shift contributing to
814 * the page's CLS score.
815 */
816 largestShiftValue?: number;
817 /**
818 * The `LayoutShiftEntry` representing the single largest layout shift
819 * contributing to the page's CLS score. (Useful when you need more than just
820 * `largestShiftTarget`, `largestShiftTime`, and `largestShiftValue`).
821 */
822 largestShiftEntry?: LayoutShift;
823 /**
824 * The first element source (in document order) among the `sources` list
825 * of the `largestShiftEntry` object. (Also useful when you need more than
826 * just `largestShiftTarget`, `largestShiftTime`, and `largestShiftValue`).
827 */
828 largestShiftSource?: LayoutShiftAttribution;
829 /**
830 * The loading state of the document at the time when the largest layout
831 * shift contribution to the page's CLS score occurred (see `LoadState`
832 * for details).
833 */
834 loadState?: LoadState;
835}
836```
837
838#### `FCPAttribution`
839
840```ts
841interface FCPAttribution {
842 /**
843 * The time from when the user initiates loading the page until when the
844 * browser receives the first byte of the response (a.k.a. TTFB).
845 */
846 timeToFirstByte: number;
847 /**
848 * The delta between TTFB and the first contentful paint (FCP).
849 */
850 firstByteToFCP: number;
851 /**
852 * The loading state of the document at the time when FCP `occurred (see
853 * `LoadState` for details). Ideally, documents can paint before they finish
854 * loading (e.g. the `loading` or `dom-interactive` phases).
855 */
856 loadState: LoadState;
857 /**
858 * The `PerformancePaintTiming` entry corresponding to FCP.
859 */
860 fcpEntry?: PerformancePaintTiming;
861 /**
862 * The `navigation` entry of the current page, which is useful for diagnosing
863 * general page load issues. This can be used to access `serverTiming` for example:
864 * navigationEntry?.serverTiming
865 */
866 navigationEntry?: PerformanceNavigationTiming;
867}
868```
869
870#### `FIDAttribution`
871
872> [!CAUTION]
873> This interface is deprecated and will be removed in the next major release.
874
875```ts
876interface FIDAttribution {
877 /**
878 * A selector identifying the element that the user interacted with. This
879 * element will be the `target` of the `event` dispatched.
880 */
881 eventTarget: string;
882 /**
883 * The time when the user interacted. This time will match the `timeStamp`
884 * value of the `event` dispatched.
885 */
886 eventTime: number;
887 /**
888 * The `type` of the `event` dispatched from the user interaction.
889 */
890 eventType: string;
891 /**
892 * The `PerformanceEventTiming` entry corresponding to FID.
893 */
894 eventEntry: PerformanceEventTiming;
895 /**
896 * The loading state of the document at the time when the first interaction
897 * occurred (see `LoadState` for details). If the first interaction occurred
898 * while the document was loading and executing script (e.g. usually in the
899 * `dom-interactive` phase) it can result in long input delays.
900 */
901 loadState: LoadState;
902}
903```
904
905#### `INPAttribution`
906
907```ts
908interface INPAttribution {
909 /**
910 * A selector identifying the element that the user first interacted with
911 * as part of the frame where the INP candidate interaction occurred.
912 * If this value is an empty string, that generally means the element was
913 * removed from the DOM after the interaction.
914 */
915 interactionTarget: string;
916 /**
917 * A reference to the HTML element identified by `interactionTarget`.
918 * NOTE: for attribution purpose, a selector identifying the element is
919 * typically more useful than the element itself. However, the element is
920 * also made available in case additional context is needed.
921 */
922 interactionTargetElement: Node | undefined;
923 /**
924 * The time when the user first interacted during the frame where the INP
925 * candidate interaction occurred (if more than one interaction occurred
926 * within the frame, only the first time is reported).
927 */
928 interactionTime: DOMHighResTimeStamp;
929 /**
930 * The best-guess timestamp of the next paint after the interaction.
931 * In general, this timestamp is the same as the `startTime + duration` of
932 * the event timing entry. However, since `duration` values are rounded to
933 * the nearest 8ms, it can sometimes appear that the paint occurred before
934 * processing ended (which cannot happen). This value clamps the paint time
935 * so it's always after `processingEnd` from the Event Timing API and
936 * `renderStart` from the Long Animation Frame API (where available).
937 * It also averages the duration values for all entries in the same
938 * animation frame, which should be closer to the "real" value.
939 */
940 nextPaintTime: DOMHighResTimeStamp;
941 /**
942 * The type of interaction, based on the event type of the `event` entry
943 * that corresponds to the interaction (i.e. the first `event` entry
944 * containing an `interactionId` dispatched in a given animation frame).
945 * For "pointerdown", "pointerup", or "click" events this will be "pointer",
946 * and for "keydown" or "keyup" events this will be "keyboard".
947 */
948 interactionType: 'pointer' | 'keyboard';
949 /**
950 * An array of Event Timing entries that were processed within the same
951 * animation frame as the INP candidate interaction.
952 */
953 processedEventEntries: PerformanceEventTiming[];
954 /**
955 * If the browser supports the Long Animation Frame API, this array will
956 * include any `long-animation-frame` entries that intersect with the INP
957 * candidate interaction's `startTime` and the `processingEnd` time of the
958 * last event processed within that animation frame. If the browser does not
959 * support the Long Animation Frame API or no `long-animation-frame` entries
960 * are detect, this array will be empty.
961 */
962 longAnimationFrameEntries: PerformanceLongAnimationFrameTiming[];
963 /**
964 * The time from when the user interacted with the page until when the
965 * browser was first able to start processing event listeners for that
966 * interaction. This time captures the delay before event processing can
967 * begin due to the main thread being busy with other work.
968 */
969 inputDelay: number;
970 /**
971 * The time from when the first event listener started running in response to
972 * the user interaction until when all event listener processing has finished.
973 */
974 processingDuration: number;
975 /**
976 * The time from when the browser finished processing all event listeners for
977 * the user interaction until the next frame is presented on the screen and
978 * visible to the user. This time includes work on the main thread (such as
979 * `requestAnimationFrame()` callbacks, `ResizeObserver` and
980 * `IntersectionObserver` callbacks, and style/layout calculation) as well
981 * as off-main-thread work (such as compositor, GPU, and raster work).
982 */
983 presentationDelay: number;
984 /**
985 * The loading state of the document at the time when the interaction
986 * corresponding to INP occurred (see `LoadState` for details). If the
987 * interaction occurred while the document was loading and executing script
988 * (e.g. usually in the `dom-interactive` phase) it can result in long delays.
989 */
990 loadState: LoadState;
991}
992```
993
994#### `LCPAttribution`
995
996```ts
997interface LCPAttribution {
998 /**
999 * The element corresponding to the largest contentful paint for the page.
1000 */
1001 element?: string;
1002 /**
1003 * The URL (if applicable) of the LCP image resource. If the LCP element
1004 * is a text node, this value will not be set.
1005 */
1006 url?: string;
1007 /**
1008 * The time from when the user initiates loading the page until when the
1009 * browser receives the first byte of the response (a.k.a. TTFB). See
1010 * [Optimize LCP](https://web.dev/articles/optimize-lcp) for details.
1011 */
1012 timeToFirstByte: number;
1013 /**
1014 * The delta between TTFB and when the browser starts loading the LCP
1015 * resource (if there is one, otherwise 0). See [Optimize
1016 * LCP](https://web.dev/articles/optimize-lcp) for details.
1017 */
1018 resourceLoadDelay: number;
1019 /**
1020 * The total time it takes to load the LCP resource itself (if there is one,
1021 * otherwise 0). See [Optimize LCP](https://web.dev/articles/optimize-lcp) for
1022 * details.
1023 */
1024 resourceLoadDuration: number;
1025 /**
1026 * The delta between when the LCP resource finishes loading until the LCP
1027 * element is fully rendered. See [Optimize
1028 * LCP](https://web.dev/articles/optimize-lcp) for details.
1029 */
1030 elementRenderDelay: number;
1031 /**
1032 * The `navigation` entry of the current page, which is useful for diagnosing
1033 * general page load issues. This can be used to access `serverTiming` for example:
1034 * navigationEntry?.serverTiming
1035 */
1036 navigationEntry?: PerformanceNavigationTiming;
1037 /**
1038 * The `resource` entry for the LCP resource (if applicable), which is useful
1039 * for diagnosing resource load issues.
1040 */
1041 lcpResourceEntry?: PerformanceResourceTiming;
1042 /**
1043 * The `LargestContentfulPaint` entry corresponding to LCP.
1044 */
1045 lcpEntry?: LargestContentfulPaint;
1046}
1047```
1048
1049#### `TTFBAttribution`
1050
1051```ts
1052export interface TTFBAttribution {
1053 /**
1054 * The total time from when the user initiates loading the page to when the
1055 * page starts to handle the request. Large values here are typically due
1056 * to HTTP redirects, though other browser processing contributes to this
1057 * duration as well (so even without redirect it's generally not zero).
1058 */
1059 waitingDuration: number;
1060 /**
1061 * The total time spent checking the HTTP cache for a match. For navigations
1062 * handled via service worker, this duration usually includes service worker
1063 * start-up time as well as time processing `fetch` event listeners, with
1064 * some exceptions, see: https://github.com/w3c/navigation-timing/issues/199
1065 */
1066 cacheDuration: number;
1067 /**
1068 * The total time to resolve the DNS for the requested domain.
1069 */
1070 dnsDuration: number;
1071 /**
1072 * The total time to create the connection to the requested domain.
1073 */
1074 connectionDuration: number;
1075 /**
1076 * The total time from when the request was sent until the first byte of the
1077 * response was received. This includes network time as well as server
1078 * processing time.
1079 */
1080 requestDuration: number;
1081 /**
1082 * The `navigation` entry of the current page, which is useful for diagnosing
1083 * general page load issues. This can be used to access `serverTiming` for
1084 * example: navigationEntry?.serverTiming
1085 */
1086 navigationEntry?: PerformanceNavigationTiming;
1087}
1088```
1089
1090## Browser Support
1091
1092The `web-vitals` code has been tested and will run without error in all major browsers as well as Internet Explorer back to version 9. However, some of the APIs required to capture these metrics are currently only available in Chromium-based browsers (e.g. Chrome, Edge, Opera, Samsung Internet).
1093
1094Browser support for each function is as follows:
1095
1096- `onCLS()`: Chromium
1097- `onFCP()`: Chromium, Firefox, Safari
1098- `onFID()`: Chromium, Firefox _(Deprecated)_
1099- `onINP()`: Chromium
1100- `onLCP()`: Chromium, Firefox
1101- `onTTFB()`: Chromium, Firefox, Safari
1102
1103## Limitations
1104
1105The `web-vitals` library is primarily a wrapper around the Web APIs that measure the Web Vitals metrics, which means the limitations of those APIs will mostly apply to this library as well. More details on these limitations is available in [this blog post](https://web.dev/articles/crux-and-rum-differences).
1106
1107The primary limitation of these APIs is they have no visibility into `<iframe>` content (not even same-origin iframes), which means pages that make use of iframes will likely see a difference between the data measured by this library and the data available in the Chrome User Experience Report (which does include iframe content).
1108
1109For same-origin iframes, it's possible to use the `web-vitals` library to measure metrics, but it's tricky because it requires the developer to add the library to every frame and `postMessage()` the results to the parent frame for aggregation.
1110
1111> [!NOTE]
1112> Given the lack of iframe support, the `onCLS()` function technically measures [DCLS](https://github.com/wicg/layout-instability#cumulative-scores) (Document Cumulative Layout Shift) rather than CLS, if the page includes iframes).
1113
1114## Development
1115
1116### Building the code
1117
1118The `web-vitals` source code is written in TypeScript. To transpile the code and build the production bundles, run the following command.
1119
1120```sh
1121npm run build
1122```
1123
1124To build the code and watch for changes, run:
1125
1126```sh
1127npm run watch
1128```
1129
1130### Running the tests
1131
1132The `web-vitals` code is tested in real browsers using [webdriver.io](https://webdriver.io/). Use the following command to run the tests:
1133
1134```sh
1135npm test
1136```
1137
1138To test any of the APIs manually, you can start the test server
1139
1140```sh
1141npm run test:server
1142```
1143
1144Then navigate to `http://localhost:9090/test/<view>`, where `<view>` is the basename of one the templates under [/test/views/](/test/views/).
1145
1146You'll likely want to combine this with `npm run watch` to ensure any changes you make are transpiled and rebuilt.
1147
1148## Integrations
1149
1150- [**Web Vitals Connector**](https://goo.gle/web-vitals-connector): Data Studio connector to create dashboards from [Web Vitals data captured in BiqQuery](https://web.dev/articles/vitals-ga4).
1151- [**Core Web Vitals Custom Tag template**](https://www.simoahava.com/custom-templates/core-web-vitals/): Custom GTM template tag to [add measurement handlers](https://www.simoahava.com/analytics/track-core-web-vitals-in-ga4-with-google-tag-manager/) for all Core Web Vitals metrics.
1152- [**`web-vitals-reporter`**](https://github.com/treosh/web-vitals-reporter): JavaScript library to batch `callback` functions and send data with a single request.
1153
1154## License
1155
1156[Apache 2.0](/LICENSE)