mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
### What? I found a small typo in the docs.(analytics.mdx) Missing ")" in the end of function. ### How? Add ")" in the end. --------- Co-authored-by: JJ Kasper <[email protected]>
221 lines
6.1 KiB
Plaintext
221 lines
6.1 KiB
Plaintext
---
|
|
title: Analytics
|
|
description: Measure and track page performance using Next.js Speed Insights
|
|
---
|
|
|
|
{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}
|
|
|
|
Next.js has built-in support for measuring and reporting performance metrics. You can either use the `useReportWebVitals` hook to manage reporting yourself, or alternatively, Vercel provides a [managed service](https://vercel.com/analytics?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) to automatically collect and visualize metrics for you.
|
|
|
|
## Build Your Own
|
|
|
|
<PagesOnly>
|
|
|
|
```jsx filename="pages/_app.js"
|
|
import { useReportWebVitals } from 'next/web-vitals'
|
|
|
|
function MyApp({ Component, pageProps }) {
|
|
useReportWebVitals((metric) => {
|
|
console.log(metric)
|
|
})
|
|
|
|
return <Component {...pageProps} />
|
|
}
|
|
```
|
|
|
|
View the [API Reference](/docs/pages/api-reference/functions/use-report-web-vitals) for more information.
|
|
|
|
</PagesOnly>
|
|
|
|
<AppOnly>
|
|
|
|
```jsx filename="app/_components/web-vitals.js"
|
|
'use client'
|
|
|
|
import { useReportWebVitals } from 'next/web-vitals'
|
|
|
|
export function WebVitals() {
|
|
useReportWebVitals((metric) => {
|
|
console.log(metric)
|
|
})
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/layout.js"
|
|
import { WebVitals } from './_components/web-vitals'
|
|
|
|
export default function Layout({ children }) {
|
|
return (
|
|
<html>
|
|
<body>
|
|
<WebVitals />
|
|
{children}
|
|
</body>
|
|
</html>
|
|
)
|
|
}
|
|
```
|
|
|
|
> Since the `useReportWebVitals` hook requires the `"use client"` directive, the most performant approach is to create a separate component that the root layout imports. This confines the client boundary exclusively to the `WebVitals` component.
|
|
|
|
View the [API Reference](/docs/app/api-reference/functions/use-report-web-vitals) for more information.
|
|
|
|
</AppOnly>
|
|
|
|
## Web Vitals
|
|
|
|
[Web Vitals](https://web.dev/vitals/) are a set of useful metrics that aim to capture the user
|
|
experience of a web page. The following web vitals are all included:
|
|
|
|
- [Time to First Byte](https://developer.mozilla.org/docs/Glossary/Time_to_first_byte) (TTFB)
|
|
- [First Contentful Paint](https://developer.mozilla.org/docs/Glossary/First_contentful_paint) (FCP)
|
|
- [Largest Contentful Paint](https://web.dev/lcp/) (LCP)
|
|
- [First Input Delay](https://web.dev/fid/) (FID)
|
|
- [Cumulative Layout Shift](https://web.dev/cls/) (CLS)
|
|
- [Interaction to Next Paint](https://web.dev/inp/) (INP)
|
|
|
|
You can handle all the results of these metrics using the `name` property.
|
|
|
|
<PagesOnly>
|
|
|
|
```jsx filename="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} />
|
|
}
|
|
```
|
|
|
|
</PagesOnly>
|
|
|
|
<AppOnly>
|
|
|
|
```tsx filename="app/_components/web-vitals.tsx" switcher
|
|
'use client'
|
|
|
|
import { useReportWebVitals } from 'next/web-vitals'
|
|
|
|
export function WebVitals() {
|
|
useReportWebVitals((metric) => {
|
|
switch (metric.name) {
|
|
case 'FCP': {
|
|
// handle FCP results
|
|
}
|
|
case 'LCP': {
|
|
// handle LCP results
|
|
}
|
|
// ...
|
|
}
|
|
})
|
|
}
|
|
```
|
|
|
|
```jsx filename="app/_components/web-vitals.js" switcher
|
|
'use client'
|
|
|
|
import { useReportWebVitals } from 'next/web-vitals'
|
|
|
|
export function WebVitals() {
|
|
useReportWebVitals((metric) => {
|
|
switch (metric.name) {
|
|
case 'FCP': {
|
|
// handle FCP results
|
|
}
|
|
case 'LCP': {
|
|
// handle LCP results
|
|
}
|
|
// ...
|
|
}
|
|
})
|
|
}
|
|
```
|
|
|
|
</AppOnly>
|
|
|
|
<PagesOnly>
|
|
|
|
## Custom Metrics
|
|
|
|
In addition to the core metrics listed above, there are some additional custom metrics that
|
|
measure the time it takes for the page to hydrate and render:
|
|
|
|
- `Next.js-hydration`: Length of time it takes for the page to start and finish hydrating (in ms)
|
|
- `Next.js-route-change-to-render`: Length of time it takes for a page to start rendering after a
|
|
route change (in ms)
|
|
- `Next.js-render`: Length of time it takes for a page to finish render after a route change (in ms)
|
|
|
|
You can handle all the results of these metrics separately:
|
|
|
|
```js
|
|
export function reportWebVitals(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
|
|
}
|
|
}
|
|
```
|
|
|
|
These metrics work in all browsers that support the [User Timing API](https://caniuse.com/#feat=user-timing).
|
|
|
|
</PagesOnly>
|
|
|
|
## Sending results to external systems
|
|
|
|
You can send results to any endpoint to measure and track
|
|
real user performance on your site. For example:
|
|
|
|
```js
|
|
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 })
|
|
}
|
|
})
|
|
```
|
|
|
|
> **Good to know**: If you use [Google Analytics](https://analytics.google.com/analytics/web/), using the
|
|
> `id` value can allow you to construct metric distributions manually (to calculate percentiles,
|
|
> etc.)
|
|
|
|
> ```js
|
|
> 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/pages/_app.js
|
|
> 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.
|
|
> })
|
|
> })
|
|
> ```
|
|
>
|
|
> Read more about [sending results to Google Analytics](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics).
|