npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guardian/core-web-vitals

v8.0.0

Published

Methods to help with the implementation of Google's Core Web Vitals

Downloads

4,611

Readme

@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;
};