javascript 帮助程序包装console.time添加Google Analytics日志功能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 帮助程序包装console.time添加Google Analytics日志功能相关的知识,希望对你有一定的参考价值。
window.PerfHelpers = window.PerfHelpers || {};
;(function(PerfHelpers) {
var timers = {};
PerfHelpers = window.performance || {};
PerfHelpers.now = PerfHelpers.now || function () {};
if ((!console) || (!console.time)) {
console.time = function() {};
console.timeEnd = function() {};
}
var consoleTime = console.time.bind(window.console);
var consoleTimeEnd = console.timeEnd.bind(window.console);
console.time = function(key) {
var phTimeKey = '[PHTime]' + key;
timers[phTimeKey + '_start'] = PerfHelpers.now();
var _startDate = (new Date().toLocaleString());
timers[phTimeKey + '_startDate'] = _startDate;
//console.log(phTimeKey + '[STARTED]: ' + _startDate);
consoleTime(phTimeKey);
};
console.timeEnd = function (key) {
var phTimeKey = '[PHTime]' + key;
var _startTime = timers[phTimeKey + '_start'];
if (_startTime) {
var _endDate = (new Date().toLocaleString());
var _endTime = PerfHelpers.now();
var _totalTime = _endTime - _startTime;
delete timers[phTimeKey + '_start'];
delete timers[phTimeKey + '_startDate'];
//console.log(phTimeKey + '[ENDED]: ' + _endDate);
consoleTimeEnd(phTimeKey);
if ('ga' in window) {
var alKey = 'ACT_' + key;
var _roundedTime = Math.round(_totalTime);
ga('send', 'timing', 'web-performance', alKey, _roundedTime, 'Total Time');
ga('send', {
hitType: 'event',
eventCategory: 'web-performance',
eventAction: alKey,
eventLabel: _endDate,
eventValue: _roundedTime
});
// console.debug('[GA][timing]:', 'send', 'event', 'web-performance', alKey, _endDate, _roundedTime);
}
return _totalTime;
} else {
return undefined;
}
};
})(window.PerfHelpers);
以上是关于javascript 帮助程序包装console.time添加Google Analytics日志功能的主要内容,如果未能解决你的问题,请参考以下文章
如何可视化 JavaScript 包装器对象?
JavaScript的执行console.log导致错误:“在主线程同步的XMLHttpRequest已经过时......”
JavaScript基本包装类
JavaScript 包装类
javascript类型系统——包装对象
javascript类型系统——包装对象