Shoora
4/5/2019 - 11:09 PM

performanceMetrics()

performanceMetrics()

function performanceMetrics() {
    if(window.performance.timing.loadEventEnd) {

        console.log('performanceMetrics start');
        // https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API
        var perfData = window.performance.timing; 
        
        // Calculate the total page load time
        // This subtracts the time at which navigation began (navigationStart) from the time at which the load event handler returns (loadEventEnd). This gives you the perceived page load time.
        var pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;

        // Calculate request response time
        // You can calculate the time elapsed between the beginning of a request and the completion of getting the response using code like this:
        var connectTime = perfData.responseEnd - perfData.requestStart;

        // Calculate page render time
        // This is obtained by starting with the time at which loading of the DOM and its dependencies is complete (domComplete) and subtracting from it the time at which parsing of the DOM began (domLoading).
        var renderTime = perfData.domComplete - perfData.domLoading;

        // track metrics
        console.log("pageLoadTime", pageLoadTime);
        console.log("connectTime", connectTime);
        console.log("renderTime", renderTime);

        // track as event
        console.log("performanceMetrics", { 
            "correlationId": "someid", 
            "pageLoadTime": pageLoadTime, 
            "connectTime": connectTime, 
            "renderTime": renderTime,
            "performance.timing": JSON.stringify(perfData.toJSON())
        });

        console.log('performanceMetrics sent');

        return;
    }
    setTimeout(performanceMetrics, 2000);
}
performanceMetrics();