useReportWebVitals
useReportWebVitals
フックを使用すると、Core Web Vitalsをレポートでき、分析サービスと組み合わせて使用できます。
pages/_app.js
import { useReportWebVitals } from 'next/web-vitals'
function MyApp({ Component, pageProps }) {
useReportWebVitals((metric) => {
console.log(metric)
})
return <Component {...pageProps} />
}
useReportWebVitals
フックの引数として渡されるmetric
オブジェクトには、いくつかのプロパティが含まれています
id
: 現在のページ読み込みのコンテキストにおけるメトリクスのユニークな識別子name
: パフォーマンスメトリクスの名前。指定可能な値には、Webアプリケーション固有のWeb Vitalsメトリクス(TTFB、FCP、LCP、FID、CLS)の名前が含まれます。delta
: メトリクスの現在の値と前の値との差。この値は通常ミリ秒単位であり、時間の経過に伴うメトリクス値の変化を表します。entries
: メトリクスに関連付けられたパフォーマンスエントリの配列。これらのエントリは、メトリクスに関連するパフォーマンスイベントに関する詳細情報を提供します。navigationType
: メトリクス収集をトリガーしたナビゲーションのタイプを示します。指定可能な値は"navigate"
、"reload"
、"back_forward"
、および"prerender"
です。rating
: メトリクス値の定性的な評価で、パフォーマンスのアセスメントを提供します。指定可能な値は"good"
、"needs-improvement"
、および"poor"
です。評価は通常、メトリクス値を、許容できるパフォーマンスまたは最適とは言えないパフォーマンスを示す事前定義されたしきい値と比較することによって決定されます。value
: パフォーマンスエントリの実際の値または期間で、通常はミリ秒単位です。この値は、メトリクスによって追跡されているパフォーマンス側面を定量的に測定するものです。この値のソースは、測定される特定のメトリクスによって異なり、さまざまなPerformance APIから取得できます。
Web Vitals
Web Vitalsは、ウェブページのユーザーエクスペリエンスを把握することを目的とした、一連の有用な指標です。以下のWeb Vitalsがすべて含まれています
- Time to First Byte (TTFB)
- First Contentful Paint (FCP)
- Largest Contentful Paint (LCP)
- First Input Delay (FID)
- Cumulative Layout Shift (CLS)
- Interaction to Next Paint (INP)
これらのメトリクスすべての結果は、name
プロパティを使用して処理できます。
pages/_app.js
import { useReportWebVitals } from 'next/web-vitals'
function MyApp({ Component, pageProps }) {
useReportWebVitals((metric) => {
switch (metric.name) {
case 'FCP': {
// handle FCP results
}
case 'LCP': {
// handle LCP results
}
// ...
}
})
return <Component {...pageProps} />
}
カスタムメトリクス
上記のコアメトリクスに加えて、ページのハイドレーションとレンダリングにかかる時間を測定するいくつかのカスタムメトリクスがあります
Next.js-hydration
: ページのハイドレーションが開始してから完了するまでの時間 (ミリ秒単位)Next.js-route-change-to-render
: ルート変更後にページのレンダリングが開始されるまでの時間 (ミリ秒単位)Next.js-render
: ルート変更後にページのレンダリングが完了するまでの時間 (ミリ秒単位)
これらのメトリクスすべての結果は個別に処理できます
pages/_app.js
import { useReportWebVitals } from 'next/web-vitals'
function MyApp({ Component, pageProps }) {
useReportWebVitals((metric) => {
switch (metric.name) {
case 'Next.js-hydration':
// handle hydration results
break
case 'Next.js-route-change-to-render':
// handle route-change to render results
break
case 'Next.js-render':
// handle render results
break
default:
break
}
})
return <Component {...pageProps} />
}
これらのメトリクスは、User Timing APIをサポートするすべてのブラウザで動作します。
Vercelでの使用法
Vercel Speed InsightsはuseReportWebVitals
ではなく、@vercel/speed-insights
パッケージを使用します。useReportWebVitals
フックは、ローカル開発時や、Web Vitals収集に別のサービスを使用している場合に役立ちます。
結果を外部システムへ送信する
サイトの実際のユーザーパフォーマンスを測定および追跡するために、任意のエンドポイントに結果を送信できます。例:
useReportWebVitals((metric) => {
const body = JSON.stringify(metric)
const url = 'https://example.com/analytics'
// Use `navigator.sendBeacon()` if available, falling back to `fetch()`.
if (navigator.sendBeacon) {
navigator.sendBeacon(url, body)
} else {
fetch(url, { body, method: 'POST', keepalive: true })
}
})
参考: Google Analyticsを使用している場合、
id
値を使用することで、メトリクス分布を手動で構築(パーセンタイルなどを計算)できます。
useReportWebVitals(metric => { // Use `window.gtag` if you initialized Google Analytics as this example: // https://github.com/vercel/next.js/blob/canary/examples/with-google-analytics window.gtag('event', metric.name, { value: Math.round(metric.name === 'CLS' ? metric.value * 1000 : metric.value), // values must be integers event_label: metric.id, // id unique to current page load non_interaction: true, // avoids affecting bounce rate. }); }
Google Analyticsに結果を送信するについて、さらに詳しく読む。
お役に立ちましたか?