function addOnloadEvent(func) {
var oldOnload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function () {
if (oldOnload) {
oldOnload();
}
func();
}
}
}
addOnloadEvent(loadIt);
function addOnunloadEvent(func) {
var oldOnunload = window.onunload;
if (typeof window.onunload != 'function') {
window.unonload = func;
} else {
window.onunload = function () {
if (oldunOnload) {
oldOnunload();
}
func();
}
}
}
addOnunloadEvent(unloadIt)
function loadIt() {
timeTracker._recordStartTime();
}
function unLoadIt() {
timeTracker._recordEndTime();
timeTracker._track(pageTracker, 'Category', 'Action');
我使用上面的代码来实现这个例子,用于 GA 事件跟踪来衡量花费的时间量
<html>
<head>
<title>Latency Tracking Demo</title>
</head>
<body>
<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
<script type="text/javascript" src="http://www.example.com/scripts/time-tracker.js"></script>
<script type="text/javascript">
var timeTracker = new TimeTracker();
var pageTracker = _gat._getTracker('UA-1735986-1');
</script>
<input type="button" value="Start Timer" onclick="timeTracker._recordStartTime();"/>
<input type="button" value="Stop Timer" onclick="timeTracker._recordEndTime();"/>
<input type="button" value="Track!" onclick="timeTracker._track(pageTracker, undefined, 'Manual Test');"/>
</body>
</html>
}
这个想法是我想测量从加载页面到用户导航离开的时间量。我使用了 GA 事件跟踪,因为我想将其视为事件并相应地对其进行分组。
一些想法我可以如何实现这个?谢谢