Juan R Labrada

How to send web-vitals analytics

https://github.com/GoogleChrome/web-vitals#load-web-vitals-from-a-cdn import {getCLS, getFID, getLCP} from 'web-vitals';

function sendToAnalytics(metric) { // Replace with whatever serialization method you prefer. // Note: JSON.stringify will likely include more data than you need. const body = JSON.stringify(metric);

// Use navigator.sendBeacon() if available, falling back to fetch(). (navigator.sendBeacon && navigator.sendBeacon('/analytics', body)) || fetch('/analytics', {body, method: 'POST', keepalive: true}); }

getCLS(sendToAnalytics); getFID(sendToAnalytics); getLCP(sendToAnalytics);

© 2023