javascript 记录从chrome.loadTimes(Chrome / Opera,IE11)到Google Analytics的第一个绘制时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 记录从chrome.loadTimes(Chrome / Opera,IE11)到Google Analytics的第一个绘制时间相关的知识,希望对你有一定的参考价值。

(function ($) {
    'use strict';

    function recordTimeToFirstPaint() {
        // Use Chrome's loadTimes or IE 9+'s msFirstPaint to record the time to render in milliseconds:
        var firstPaintTime, timingSource;

        if ('chrome' in window && $.isFunction(window.chrome.loadTimes)) {
            var loadTimes = window.chrome.loadTimes();

            // A small percentage of traffic from Chrome in the wild returns impossibly large values – 2+
            // days! – which need to be filtered out:

            firstPaintTime = loadTimes.firstPaintTime - loadTimes.startLoadTime;

            if (firstPaintTime > 3600) {
                Raven.captureMessage('chrome.loadTimes() reported impossible values',
                                     {extra: {loadTimes: loadTimes}});
                return;
            }

            // Convert from seconds to milliseconds:
            firstPaintTime = Math.round(firstPaintTime * 1000);
            timingSource = 'chrome.loadTimes';
        } else if ('performance' in window) {
            var navTiming = window.performance.timing;

            if (navTiming.navigationStart === 0) {
                return; // IE9 bug - see below
            }

            // See http://msdn.microsoft.com/ff974719
            firstPaintTime = navTiming.msFirstPaint - navTiming.navigationStart;
            timingSource = 'msFirstPaint';
        }

        if (typeof firstPaintTime == 'number' && firstPaintTime > 0) {
            ga('send', {
                hitType: 'timing',
                transport: 'beacon',
                timingCategory: 'RUM',
                timingVar: 'First Paint (ms)',
                timingValue: firstPaintTime,
                timingLabel: timingSource
            });
        }
    }

    // Defer collecting data until after the load event has finished to avoid an IE9
    // bug where navigationStart will be 0 until after the browser updates the
    // performance.timing data structure:
    $(window).on('load', function () {
        setTimeout(recordTimeToFirstPaint);
    });
})(jQuery);

以上是关于javascript 记录从chrome.loadTimes(Chrome / Opera,IE11)到Google Analytics的第一个绘制时间的主要内容,如果未能解决你的问题,请参考以下文章

如何从 jquery 或 javascript 中的两个不同数组中查找唯一记录?

javascript 记录从chrome.loadTimes(Chrome / Opera,IE11)到Google Analytics的第一个绘制时间

记录JavaScript的util.js类库

从后面的 vb.net 代码调用 javascript

从不同的窗口(不是子窗口)重新加载 iframe - javascript

JavaScript 使用复选框从 MySQL 数据库中删除多行