Nuxt Web Vitals
Web Vitals:用於健全 Nuxt 的基本模組
Web Vitals 是 Google 的一項倡議,旨在為提供良好的網路使用者體驗所必需的品質訊號提供統一的指導方針。
此模組將收集每個頁面檢視上的這些指標,並使用 Navigator.sendBeacon()
或 fetch()
將它們傳送到提供者。
安裝
npx nuxi@latest module add web-vitals
將 @nuxtjs/web-vitals
新增至您的 nuxt.config.js
的 modules
區段
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
將 WebVitals 報告至 GA
建立 GA 資源並取得 trackingID
提供 GOOGLE_ANALYTICS_ID
環境變數,或在 nuxt.config
內設定
(為了向後相容,支援頂層 googleAnalytics.id
作為後備)
export default defineNuxtConfig({
webVitals: {
ga: { id: 'UA-XXXXXXXX-X' }
}
})
行為 > 事件 > 總覽 > 事件類別 > 事件動作
將 WebVitals 報告至 GTM
建立 GTM 資源並將標籤管理工具新增至您的網站。
export default defineNuxtConfig({
webVitals: {
gtm: {}
}
})
Vercel Analytics
將 WebVitals 報告至 Vercel
無需設定即可運作
基本記錄器
將 WebVitals 報告至主控台
將指標輸出至主控台,而不是將其傳送到遠端提供者
export default defineNuxtConfig({
webVitals: {
provider: 'log',
debug: true, // debug enable metrics reporting on dev environments
disabled: false
}
})
⚠️ 這個提供者不會透過網路傳送 WebVitals,使用此方法無法偵測到導覽器擴充功能的問題。
記錄至自訂 API
將 WebVitals 報告至自訂 API 端點
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://127.0.0.1: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'
})