@guardian/core-web-vitals
TypeScript icon, indicating that this package has built-in type declarations

8.0.1 • Public • Published

@guardian/core-web-vitals

npm (scoped) ES version npm type definitions

Installation

Generic badge

yarn add @guardian/core-web-vitals

or

npm install @guardian/core-web-vitals

then

import {
	initCoreWebVitals,
	bypassCoreWebVitalsSampling,
} from '@guardian/core-web-vitals';

Bundling

This package uses ES2020.

If your target environment does not support that, make sure you transpile this package when bundling your application.

Usage

By default, a sampling rate is set at 1% for which Core Web Vitals will be gathered and sent. It is possible to set this sampling to a different value at initialisation or bypass it asynchronously (see below).

import { initCoreWebVitals, getCookie } from '@guardian/core-web-vitals';

// browserId & pageViewId are needed to join up the data downstream.
const init: InitCoreWebVitalsOptions = {
	browserId: getCookie({ name: 'bwid', shouldMemoize: true }),
	pageViewId: guardian.config.ophan.pageViewId,

	// Whether to use CODE or PROD endpoints.
	isDev: window.location.hostname !== 'www.theguardian.com',
};

initCoreWebVitals(init);

init.sampling

Sets a sampling rate for which to send data to the logging endpoint.

Defaults to 1 / 100.

const init: InitCoreWebVitalsOptions = {
	isDev: false,

	// Send data for 20% of page views. Inform Data Tech team about expected
	// spikes in data ingestion
	sampling: 20 / 100,
};

initCoreWebVitals(init);

init.team

Optional team name to log whether the payload has been successfully queued for transfer.

const init: InitCoreWebVitalsOptions = {
	isDev: false,
	sampling: 100 / 100,
	team: 'dotcom',
};

initCoreWebVitals(init);

// should call log('dotcom', 'Core Web Vitals payload successfully queued […]')

bypassCoreWebVitalsSampling

Allows to asynchronously bypass the sampling rate.

Takes an optional team name for which to print logs for.

/* … after having called initCoreWebVitals() … */

addEventListener('some-event', () => {
	// CWV will be sent for all page views where `some-event` was triggered
	bypassCoreWebVitalsSampling();
});

Types

CoreWebVitalsPayload

type CoreWebVitalsPayload = {
	page_view_id: string | null;
	browser_id: string | null;
	fid: null | number;
	cls: null | number;
	lcp: null | number;
	fcp: null | number;
	ttfb: null | number;
};

InitCoreWebVitalsOptions

type InitCoreWebVitalsOptions = {
	isDev: boolean;

	browserId?: string | null;
	pageViewId?: string | null;

	sampling?: number;
	team?: TeamName;
};

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
8.0.1129latest
0.0.0-canary-2025011417555736canary

Version History

VersionDownloads (Last 7 Days)Published
0.0.0-canary-2025011417555736
0.0.0-canary-2025011414425140
8.0.1129
8.0.03
0.0.0-canary-202409170829091
0.0.0-canary-202409170816591
0.0.0-canary-202409121104510
0.0.0-canary-202409111512360
0.0.0-canary-202409111409130
0.0.0-canary-202409111308480
0.0.0-canary-202409111252540
0.0.0-canary-202409111229452
0.0.0-canary-202409111205170
0.0.0-canary-202409111143320
0.0.0-canary-202409111120461
0.0.0-canary-202409111103163
0.0.0-canary-202409090923374
0.0.0-canary-202409021155581
0.0.0-canary-202409021116164
0.0.0-canary-202408291102470
0.0.0-canary-202408291023240
0.0.0-canary-202408290716550
0.0.0-canary-202408270847100
0.0.0-canary-202408190842062
0.0.0-canary-202407170857350
7.0.0763
0.0.0-canary-202407020921461
0.0.0-canary-202407020901030
0.0.0-canary-202407020858550
0.0.0-canary-202407011710140
0.0.0-canary-202407011545421
0.0.0-canary-202407011543080
0.0.0-canary-202407011530391
0.0.0-canary-202407011513520
0.0.0-canary-202407011043180
0.0.0-canary-202407011031150
0.0.0-canary-202407011030530
0.0.0-canary-202406271229020
6.0.10
0.0.0-canary-202405071603320
0.0.0-canary-202405071559110
0.0.0-canary-202405022246201
0.0.0-canary-202405021636200
0.0.0-canary-202405021628060
0.0.0-canary-202404291553500
6.0.056
5.1.08
5.0.00
4.0.00
3.0.00
2.0.30
2.0.22
2.0.12
2.0.02
1.0.12
1.0.02

Package Sidebar

Install

npm i @guardian/core-web-vitals

Weekly Downloads

1,068

Version

8.0.1

License

none

Unpacked Size

28.2 kB

Total Files

9

Last publish

Collaborators

  • guardian-developers
  • akash1810
  • reetta
  • sndrs
  • ashcorrguardian