How do you use the useReportWebVitals hook to collect performance data from real visitors on your Next.js site?
Short answer
You call the useReportWebVitals hook inside a client component, providing a callback function that receives each performance metric as it becomes available, letting you send that data to an analytics service to understand how your site actually performs for real visitors across…
Advertisement
AModel answer
You call the useReportWebVitals hook inside a client component, providing a callback function that receives each performance metric as it becomes available, letting you send that data to an analytics service to understand how your site actually performs for real visitors across different devices and network conditions.
'use client';
import { useReportWebVitals } from 'next/web-vitals';
export function WebVitals() {
useReportWebVitals((metric) => {
console.log(metric);
sendToAnalytics(metric);
});
return null;
}Real-world example
A company sends real user performance data to their analytics dashboard using the useReportWebVitals hook, discovering that their mobile visitors experience significantly slower load times than desktop visitors.
Likely follow-ups
- What metrics does the callback function actually receive?
- Where should this component be placed in the application to capture data on every page?
Related topics