useReportWebVitals
useReportWebVitalsフックを使用すると、Core Web Vitalsをレポートでき、分析サービスと組み合わせて使用できます。
useReportWebVitalsに渡された新しい関数は、それまでに入手可能なメトリクスで呼び出されます。重複データのレポートを防ぐために、コールバック関数の参照が変更されないようにしてください(以下のコード例を参照)。
pages/_app.js
import { useReportWebVitals } from 'next/web-vitals'
const logWebVitals = (metric) => {
console.log(metric)
}
function MyApp({ Component, pageProps }) {
useReportWebVitals(logWebVitals)
return <Component {...pageProps} />
}useReportWebVitals
フックの引数として渡されるmetricオブジェクトは、いくつかのプロパティで構成されます。
id: 現在のページロードのコンテキストにおけるメトリクスのユニークな識別子。name: パフォーマンスメトリクスの名前。可能な値としては、Webアプリケーション固有のWeb Vitalsメトリクス(TTFB、FCP、LCP、FID、CLS)の名前が含まれます。delta: メトリクスの現在の値と以前の値との差。値は通常ミリ秒単位で、時間経過に伴うメトリクスの値の変化を表します。entries: メトリクスに関連付けられたPerformance 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'
const handleWebVitals = (metric) => {
switch (metric.name) {
case 'FCP': {
// handle FCP results
}
case 'LCP': {
// handle LCP results
}
// ...
}
}
function MyApp({ Component, pageProps }) {
useReportWebVitals(handleWebVitals)
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 handleCustomMetrics(metrics) {
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
}
}
function MyApp({ Component, pageProps }) {
useReportWebVitals(handleCustomMetrics)
return <Component {...pageProps} />
}これらのメトリクスは、User Timing APIをサポートするすべてのブラウザで機能します。
結果を外部システムに送信する
サイトの実際のユーザーパフォーマンスを測定および追跡するために、結果を任意のエンドポイントに送信できます。例:
function postWebVitals(metrics) {
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 })
}
}
useReportWebVitals(postWebVitals)知っておくと便利: 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 への結果送信 について続きを読む。
役に立ちましたか?