javascript 从Google Analytics中的浏览器跟踪导航计时数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 从Google Analytics中的浏览器跟踪导航计时数据相关的知识,希望对你有一定的参考价值。

var NAMESPACE = NAMESPACE || {};
NAMESPACE.gaSpeedTrackingJsLoadStart = (new Date()).getTime();

/* All JS ... */

/* Encapsulate "use strict" to this part of the code, while others can still be unstrict. */
(function () {
    "use strict";

    /* Track JS Parsing time (start was set at the Beginning of the JS) */
    NAMESPACE.gaSpeedTrackingJsLoadEnd = (new Date()).getTime();
    if (parseInt(NAMESPACE.gaSpeedTrackingJsLoadStart,10)>0 && parseInt(NAMESPACE.gaSpeedTrackingJsLoadEnd,10)>0) {
        NAMESPACE.gaSpeedTrackingJsLoadDuration = parseInt(NAMESPACE.gaSpeedTrackingJsLoadEnd,10) - parseInt(NAMESPACE.gaSpeedTrackingJsLoadStart,10);
        // 0-60000: prevent ridiculous numbers from false calculation
        if (NAMESPACE.gaSpeedTrackingJsLoadDuration>0 && NAMESPACE.gaSpeedTrackingJsLoadDuration<60000) {
            var _gaq = window._gaq || [];
            _gaq.push(['_trackTiming', 'Navigation Timing', 'JavaScript Parsing', NAMESPACE.gaSpeedTrackingJsLoadDuration]);
        }
    }


    /* Calculate and track Browser's Performance Timing */
    NAMESPACE.getNavigationTimingStats = function () {
        var timing = window.performance.timing;
        return {
            dns:timing.domainLookupEnd - timing.domainLookupStart,
            connect:timing.connectEnd - timing.connectStart,
            ttfb:timing.responseStart - timing.connectEnd,
            basePage:timing.responseEnd - timing.responseStart,
            frontEnd:timing.loadEventStart - timing.responseEnd, 
			domContentLoadedEvent:timing.domContentLoadedEventEnd - timing.domContentLoadedEventStart,
			windowLoadEvent:timing.loadEventEnd - timing.loadEventStart,
			domInteractive:timing.domInteractive - timing.domLoading,
			domComplete:timing.domComplete - timing.domLoading,
			domCompleteToOnload: timing.loadEventStart - timing.domComplete
        };
    };
	
    NAMESPACE.trackNavigationTiming = function () {
        if (window.performance && window.performance.timing) {
            var _gaq = window._gaq || [];
            var ntStats = NAMESPACE.getNavigationTimingStats();

            // 0-60000: prevent ridiculous numbers from false calculation
            if (ntStats.dns > 0 && ntStats.dns < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'DNS', ntStats.dns]);
            }
            if (ntStats.connect > 0 && ntStats.connect < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'Connect', ntStats.connect]);
            }
            if (ntStats.ttfb > 0 && ntStats.ttfb < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'TTFB', ntStats.ttfb]);
            }
            if (ntStats.basePage > 0 && ntStats.basePage < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'BasePage', ntStats.basePage]);
            }
            if (ntStats.frontEnd > 0 && ntStats.frontEnd < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'FrontEnd', ntStats.frontEnd]);
            }
            if (ntStats.domContentLoadedEvent > 0 && ntStats.domContentLoadedEvent < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'domReady Events', ntStats.domContentLoadedEvent]);
            }
            if (ntStats.windowLoadEvent > 0 && ntStats.windowLoadEvent < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'windowLoad Events', ntStats.windowLoadEvent]);
            }			
            if (ntStats.domInteractive > 0 && ntStats.domInteractive < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'domInteractive', ntStats.domInteractive]);
            }
            if (ntStats.domComplete > 0 && ntStats.domComplete < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'domComplete', ntStats.domComplete]);
            }
            if (ntStats.domCompleteToOnload > 0 && ntStats.domCompleteToOnload < 60000) {
                _gaq.push(['_trackTiming', 'Navigation Timing', 'domCompleteToOnload', ntStats.domCompleteToOnload]);
            }

        }
    };

    $(window).load(function () {
        "use strict";
        NAMESPACE.trackNavigationTiming();
    });

}());


var NAMESPACE = NAMESPACE || {};
NAMESPACE.gaSpeedTrackingJsLoadStart = (new Date()).getTime();

/* All JS ... */

/* Encapsulate "use strict" to this part of the code, while others can still be unstrict. */
(function () {
    "use strict";

    /* Track JS Parsing time (start was set at the Beginning of the JS) */
    NAMESPACE.gaSpeedTrackingJsLoadEnd = (new Date()).getTime();
    if (parseInt(NAMESPACE.gaSpeedTrackingJsLoadStart,10)>0 && parseInt(NAMESPACE.gaSpeedTrackingJsLoadEnd,10)>0) {
        NAMESPACE.gaSpeedTrackingJsLoadDuration = parseInt(NAMESPACE.gaSpeedTrackingJsLoadEnd,10) - parseInt(NAMESPACE.gaSpeedTrackingJsLoadStart,10);
        // 0-60000: prevent ridiculous numbers from false calculation
        if (NAMESPACE.gaSpeedTrackingJsLoadDuration>0 && NAMESPACE.gaSpeedTrackingJsLoadDuration<60000) {
            var _gaq = window._gaq || [];
            ga('send', 'timing', 'Navigation Timing', 'JavaScript Parsing', NAMESPACE.gaSpeedTrackingJsLoadDuration);
        }
    }


    /* Calculate and track Browser's Performance Timing */
    NAMESPACE.getNavigationTimingStats = function () {
        var timing = window.performance.timing;
        return {
            dns:timing.domainLookupEnd - timing.domainLookupStart,
            connect:timing.connectEnd - timing.connectStart,
            ttfb:timing.responseStart - timing.connectEnd,
            basePage:timing.responseEnd - timing.responseStart,
            frontEnd:timing.loadEventStart - timing.responseEnd, 
			domContentLoadedEvent:timing.domContentLoadedEventEnd - timing.domContentLoadedEventStart,
			windowLoadEvent:timing.loadEventEnd - timing.loadEventStart,
			domInteractive:timing.domInteractive - timing.domLoading,
			domComplete:timing.domComplete - timing.domLoading,
			domCompleteToOnload: timing.loadEventStart - timing.domComplete
        };
    };
	
    NAMESPACE.trackNavigationTiming = function () {
        if (window.performance && window.performance.timing) {
            var _gaq = window._gaq || [];
            var ntStats = NAMESPACE.getNavigationTimingStats();

            // 0-60000: prevent ridiculous numbers from false calculation
            if (ntStats.dns > 0 && ntStats.dns < 60000) {
				  ga('send', 'timing', 'Page ready', 'load', timeSincePageLoad);
                ga('send', 'timing', 'Navigation Timing', 'DNS', ntStats.dns);
            }
            if (ntStats.connect > 0 && ntStats.connect < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'Connect', ntStats.connect);
            }
            if (ntStats.ttfb > 0 && ntStats.ttfb < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'TTFB', ntStats.ttfb);
            }
            if (ntStats.basePage > 0 && ntStats.basePage < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'BasePage', ntStats.basePage);
            }
            if (ntStats.frontEnd > 0 && ntStats.frontEnd < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'FrontEnd', ntStats.frontEnd);
            }
            if (ntStats.domContentLoadedEvent > 0 && ntStats.domContentLoadedEvent < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'domReady Events', ntStats.domContentLoadedEvent);
            }
            if (ntStats.windowLoadEvent > 0 && ntStats.windowLoadEvent < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'windowLoad Events', ntStats.windowLoadEvent);
            }			
            if (ntStats.domInteractive > 0 && ntStats.domInteractive < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'domInteractive', ntStats.domInteractive);
            }
            if (ntStats.domComplete > 0 && ntStats.domComplete < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'domComplete', ntStats.domComplete);
            }
            if (ntStats.domCompleteToOnload > 0 && ntStats.domCompleteToOnload < 60000) {
                ga('send', 'timing', 'Navigation Timing', 'domCompleteToOnload', ntStats.domCompleteToOnload);
            }

        }
    };

    $(window).load(function () {
        "use strict";
        NAMESPACE.trackNavigationTiming();
    });

}());

以上是关于javascript 从Google Analytics中的浏览器跟踪导航计时数据的主要内容,如果未能解决你的问题,请参考以下文章

从 javascript 调用 Google 云打印/搜索 API

如何使用 JavaScript 从 Google 日历页面获取事件 ID?

javascript Google地图标记从外部点击

从 Javascript 中的 Google 搜索中提取关键字

使用Javascript从Google Places搜索api获取纬度和经度

使用自动列宽(v3 API)从JavaScript将数据导入Google表格