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

@bbc/web-vitals

v2.0.1

Published

Generic hook for reporting web vitals metrics to an endpoint

Downloads

3,862

Readme

Web-Vitals

The WebVitals utility provides a configurable hook which will collect device capability metrics and Web Vitals metrics for each page view, and then send those metrics to a reporting endpoint when the user leaves the page.

Metrics we report on

Core Web-Vitals

  • CLS (Cumulative Layout Shift)
  • LCP (Largest Contentful Paint)
  • FID (First Input Delay)

Other data (useful for categorising the web-vitals data received)

  • FCP (First Contentful Paint)

  • TTFB (Time To First Byte)

  • INP (Interaction to Next Paint)

  • Device Memory

  • Device CPU

  • Effective Connection Type

Installation

npm install @bbc/web-vitals --save

Install yarn

The Web Vitals project uses yarn for package management. It is recommended to install yarn through the npm package manager, which comes bundled with Node.js when you install it on your system. To install Yarn, run this command:

npm install --global yarn

Props

| Argument | Type | Required | Default | Example | | ----------------- | -------- | -------- | ----------- | ------------------------------------------------------------- | | enabled | Boolean | No | false | { enabled: true } | | reportingEndpoint | String | Yes | N/A | { reportingEndpoint: 'https://url.to.report.to/analytics' } | | loggerCallback | Function | No | () => {} | { loggerCallback: (error) => console.error(error) } | | sampleRate | Integer | No | 100 | { sampleRate: 5 } | | reportParams | Object | No | undefined | { reportParams: { pageType: 'STY' } |

Usage

The hook can be configured in a number of ways:

  • enabled - setting this to true will enable sending these metrics to the reporting endpoint. This is so that the reporting functionality can be turned on only when users have agreed to the appropriate privacy and tracking settings where appropriate.
  • reportingEndpoint - the endpoint you wish to send your metrics to
  • loggerCallback - a function that will be called whenever an attempt to send the metrics fails.
  • sampleRate - providing a number between 1-100 will set the percentage of sampling, to prevent overloading the backend reporting server. e.g. a sampleRate of 5 will mean that metrics for 5% of requests will be sent to the reporting server.
  • reportParams - an object that will define query string parameters appended to the reporting endpoint. As an example, providing { pageType: 'STY' } will cause ?pageType=STY to be appended to the reporting endpoint.
import useWebVitals from "@bbc/web-vitals";
import logger from "./logger";

const Page = props => {
  const enabled = isOnClient() && hasUserAcceptedTracking();
  useWebVitals({
    enabled,
    reportingEndpoint: 'https://url.to.report.to/analytics',
    loggerCallback = logger.error,
    sampleRate: 50,
    reportParams: { pageType: 'STY' },
  });

  return (
    <PageComponents />
  );
};

When to use this utility

This utility should ideally be used as close to the top-level of your app as is practical. You should follow due diligence in regards to checking if your site visitor needs to accept any personalisation or tracking permissions under relevant local laws, and use this in determining the value of the enabled prop.

useEffect hooks are only run client-side, but when using this hook in combination with Server Side Rendering, you may wish to also include a check of whether the code is running client-side in order to ensure that you do not get inconsistencies between your server's capabilities and the user's.

When not to use this utility

In non-page-level components, especially where these components may be added multiple times to a page.

In situations where users have not provided permission for the relevant personalisation or tracking permissions that may be required under relevant local laws.

Contributing

Web Vitals is completely open source. We are grateful for any contributions, whether they be new utilities, bug fixes or general improvements. Please see our primary contributing guide which can be found at the root of the Web Vitals repository.

Code of Conduct

We welcome feedback and help on this work. By participating in this project, you agree to abide by the code of conduct. Please take a moment to read it.

License

Web-Vitals is Apache 2.0 licensed.