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 Vitalsメトリック(TTFB、FCP、LCP、FID、CLS)の名前や、Webアプリケーションに固有の名前が含まれます。delta
: メトリックの現在値と前回値の差。値は通常ミリ秒単位で、時間の経過に伴うメトリック値の変化を表します。entries
: メトリックに関連付けられたパフォーマンスエントリの配列。これらのエントリは、メトリックに関連するパフォーマンスイベントに関する詳細情報を提供します。navigationType
: メトリック収集のトリガーとなったナビゲーションの種類を示します。可能な値は、"navigate"
、"reload"
、"back_forward"
、および"prerender"
です。rating
: メトリック値の定性的な評価であり、パフォーマンスの評価を提供します。可能な値は、"good"
、"needs-improvement"
、および"poor"
です。評価は通常、メトリック値を許容できるパフォーマンスまたは最適とは言えないパフォーマンスを示す定義済みのしきい値と比較することによって決定されます。value
: パフォーマンスエントリの実際の値または期間。通常はミリ秒単位です。値は、メトリックによって追跡されているパフォーマンスの側面の定量的な尺度を提供します。値のソースは、測定されている特定のメトリックによって異なり、さまざまなパフォーマンス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 アナリティクスを使用する場合、`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 アナリティクスに結果を送信する方法の詳細をご覧ください。
これは役に立ちましたか?