web-vitals
@nuxtjs/web-vitals

適用於 Nuxt 的 Web Vitals

Nuxt Web Vitals

Web Vitals:一個健康的 Nuxt 的核心模組

Web Vitals 是 Google 推出的一項倡議,旨在為對在網路上提供卓越使用者體驗至關重要的質量訊號提供統一指導。

此模組將在每次頁面瀏覽時收集這些指標,並使用 Navigator.sendBeacon()fetch() 將它們傳送給提供商

安裝

npx nuxi@latest module add web-vitals

@nuxtjs/web-vitals 新增到 nuxt.config.jsmodules 部分

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

⚠️ 如果您使用的是 Nuxt < v2.9,則必須將模組安裝為 dependency(不帶 --save-dev--dev 標誌)。如果您是 Nuxt 2.9+(但不是 Nuxt 3),則應將模組新增到 buildModules 而不是 modules

選項

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

提供商

Google Analytics(分析)

向 GA 報告 WebVitals

建立 GA 媒體資源並獲取 trackingID

提供 GOOGLE_ANALYTICS_ID 環境變數或在 nuxt.config 中設定

(頂級 googleAnalytics.id 支援向後相容作為備用)

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

行為 > 事件 > 概述 > 事件類別 > 事件操作

Events Actions

向 GTM 報告 WebVitals

建立 GTM 媒體資源並將標籤管理器新增到您的網站。

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

Vercel Analytics

向 Vercel 報告 WebVitals

無需配置即可工作

基本日誌記錄器

向控制檯報告 WebVitals

將指標輸出到控制檯而不是傳送到遠端提供商

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

⚠️ 此提供商不透過網路傳送 WebVitals,使用此方法無法檢測到導航器擴充套件問題。

記錄到自定義 API

向自定義 API 端點報告 WebVitals

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

示例正文

export default defineNuxtConfig({
  href: 'https://: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'
})

許可證

MIT