Below is a JavaScript code snippet that tracks Time to First Paint(TFP), Time to First Contentful Paint(TFCP), Latency, and some other critical performance metrics. It then sends this data in a JSON format to an endpoint.
(function() {
document.addEventListener('DOMContentLoaded', function(){
var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {};
var performanceTiming = performance.timing;
var analyticsData = {}; // To store the metrics
// Time to First Paint (TFP)