Package detail

@nuxtjs/web-vitals

nuxt-modules15.5kMIT0.2.7

Web Vitals for Nuxt.js

nuxt, performance, metrics, vitals

readme

Nuxt Web Vitals

Web Vitals: Essential module for a healthy Nuxt

Web Vitals is an initiative by Google to provide unified guidance for quality signals that are essential to delivering a great user experience on the web.

This module will gather those metrics on each page view, and send them to a provider using either Navigator.sendBeacon() or fetch()

Installation

npx nuxi@latest module add web-vitals

Add @nuxtjs/web-vitals to the modules section of your nuxt.config.js

export default defineNuxtConfig({
  modules: [
    '@nuxtjs/web-vitals'
  ]
})

:warning: If you are using Nuxt < v2.9 you have to install the module as a dependency (no --save-dev or --dev flags). If you are Nuxt 2.9+ (but not Nuxt 3) you should add the module to buildModules instead of modules.

Options

export default defineNuxtConfig({
  webVitals: {
    // provider: '', // auto detectd
    debug: false,
    disabled: false
  }
})

Providers

Google Analytics

Report WebVitals to GA

Create a GA property and get trackingID

Either provide GOOGLE_ANALYTICS_ID environement variable or set inside nuxt.config:

(Top level googleAnalytics.id is supported for backward compatibility as fallback)

export default defineNuxtConfig({
  webVitals: {
    ga: { id: 'UA-XXXXXXXX-X' }
  }
})

Behavior > Events > Overview > Event Category > Event Action

Events Actions

Report WebVitals to GTM

Create a GTM property and add the tag manager to your site.

export default defineNuxtConfig({
  webVitals: {
    gtm: {}
  }
})

Vercel Analytics

Report WebVitals to Vercel

Works without configuration

Basic logger

Report WebVitals to Console

Output metrics to the console insead of sending them to a remote provider

export default defineNuxtConfig({
  webVitals: {
    provider: 'log',
    debug: true, // debug enable metrics reporting on dev environments
    disabled: false
  }
})

:warning: this provider does not send WebVitals trough network, issues with navigator extensions can not be deteced with this method.

Logging to custom api

Report WebVitals to a custom api endpoint

export default defineNuxtConfig({
  webVitals: {
    provider: 'api',
    api: { url: '/api/web-vitals' }
    debug: true // debug enable metrics reporting on dev environments
  })

Example body:

export default defineNuxtConfig({
  href: 'http://localhost:3000/',
  name: 'LCP',
  value: 303.599,
  rating: 'good',
  delta: 303.599,
  entries: [
    {
      name: '',
      entryType: 'largest-contentful-paint',
      startTime: 303.599,
      duration: 0,
      size: 5698,
      renderTime: 303.599,
      loadTime: 0,
      firstAnimatedFrameTime: 0,
      id: '',
      url: ''
    }
  ],
  id: 'v3-1669725914225-9792921995831',
  navigationType: 'reload'
})

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.2.7 (2024-04-22)

Features

Bug Fixes

  • use non-deprecated werb-vitals functions (eb37fd6)

0.2.6 (2023-05-17)

Features

Bug Fixes

  • don't default to gtm provider (9af31c8)
  • only run sendVitals once on initial load (#66) (d0e408d)

0.2.5 (2023-04-29)

0.2.4 (2023-03-16)

0.2.3 (2023-03-16)

Bug Fixes

0.2.2 (2022-11-29)

Features

Bug Fixes

  • handle when localstorage is unavailable (#58) (0d0c423), closes #56

0.2.1 (2022-11-29)

Bug Fixes

  • resolve ufo for nuxt 2 support (192b409)

0.2.0 (2022-11-28)

⚠ BREAKING CHANGES

  • nuxt 3 compatibility (#59)

Features

Bug Fixes

  • nuxt 2 plugin syntax usage (f241654)

0.1.10 (2022-07-26)

Bug Fixes

0.1.9 (2022-07-26)

Bug Fixes

  • handle nuxt 3 and vue-router 4x api (#49) (a1ac506)

0.1.8 (2021-08-24)

Bug Fixes

  • switch to .js context for resolving relative imports (#32) (158d63f)

0.1.7 (2021-08-12)

Bug Fixes

  • de-default web-vitals import (ac169af)

0.1.6 (2021-08-12)

Bug Fixes

  • add esm workaround for nuxt < 2.16 (42840cc)
  • use .mjs for vitals.client (e52e73c)

0.1.5 (2021-08-12)

Bug Fixes

  • correct url encoding for ga provider (#26) (945b1cf)
  • ga: use ufo to encode params (resolves #25) (2b5d489)

0.1.4 (2021-02-25)

Bug Fixes

  • utils: remove send debug log (resolves #18) (d2eabad)

0.1.3 (2021-02-04)

Bug Fixes

0.1.2 (2021-02-04)

Features

  • auto switch to log in dev if debug enabled and no provider set (f38ea59)

0.1.1 (2021-02-04)

Bug Fixes

  • early return if no provider is set (08c25e8)