|
|
--- |
|
|
title: How to add analytics to your Next.js application |
|
|
nav_title: Analytics |
|
|
description: Measure and track page performance using Next.js Speed Insights |
|
|
--- |
|
|
|
|
|
{} |
|
|
|
|
|
Next.js has built-in support for measuring and reporting performance metrics. You can either use the [`useReportWebVitals`](/docs/app/api-reference/functions/use-report-web-vitals) hook to manage reporting yourself, or alternatively, Vercel provides a [managed service](https: |
|
|
|
|
|
## Client Instrumentation |
|
|
|
|
|
For more advanced analytics and monitoring needs, Next.js provides a `instrumentation-client.js|ts` file that runs before your application's frontend code starts executing. This is ideal for setting up global analytics, error tracking, or performance monitoring tools. |
|
|
|
|
|
To use it, create an `instrumentation-client.js` or `instrumentation-client.ts` file in your application's root directory: |
|
|
|
|
|
```js filename="instrumentation-client.js" |
|
|
|
|
|
console.log('Analytics initialized') |
|
|
|
|
|
|
|
|
window.addEventListener('error', (event) => { |
|
|
|
|
|
reportError(event.error) |
|
|
}) |
|
|
``` |
|
|
|
|
|
## 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: |
|
|
experience of a web page. The following web vitals are all included: |
|
|
|
|
|
- [Time to First Byte](https: |
|
|
- [First Contentful Paint](https: |
|
|
- [Largest Contentful Paint](https: |
|
|
- [First Input Delay](https: |
|
|
- [Cumulative Layout Shift](https: |
|
|
- [Interaction to Next Paint](https: |
|
|
|
|
|
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': { |
|
|
|
|
|
} |
|
|
case 'LCP': { |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}) |
|
|
|
|
|
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': { |
|
|
|
|
|
} |
|
|
case 'LCP': { |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}) |
|
|
} |
|
|
``` |
|
|
|
|
|
```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': { |
|
|
|
|
|
} |
|
|
case 'LCP': { |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}) |
|
|
} |
|
|
``` |
|
|
|
|
|
</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': |
|
|
|
|
|
break |
|
|
case 'Next.js-route-change-to-render': |
|
|
|
|
|
break |
|
|
case 'Next.js-render': |
|
|
|
|
|
break |
|
|
default: |
|
|
break |
|
|
} |
|
|
} |
|
|
``` |
|
|
|
|
|
These metrics work in all browsers that support the [User Timing API](https: |
|
|
|
|
|
</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: |
|
|
|
|
|
|
|
|
if (navigator.sendBeacon) { |
|
|
navigator.sendBeacon(url, body) |
|
|
} else { |
|
|
fetch(url, { body, method: 'POST', keepalive: true }) |
|
|
} |
|
|
}) |
|
|
``` |
|
|
|
|
|
> **Good to know**: If you use [Google Analytics](https: |
|
|
> `id` value can allow you to construct metric distributions manually (to calculate percentiles, |
|
|
> etc.) |
|
|
|
|
|
> ```js |
|
|
> useReportWebVitals((metric) => { |
|
|
> |
|
|
> |
|
|
> window.gtag('event', metric.name, { |
|
|
> value: Math.round( |
|
|
> metric.name === 'CLS' ? metric.value * 1000 : metric.value |
|
|
> ), |
|
|
> event_label: metric.id, |
|
|
> non_interaction: true, |
|
|
> }) |
|
|
> }) |
|
|
> ``` |
|
|
> |
|
|
> Read more about [sending results to Google Analytics](https: |
|
|
|