andfinally
10/1/2013 - 10:26 AM

Ping Google Analytics "Log" and the time in hh:mm:ss at intervals - Stephan's code. Seems to log custom event with GA every 1 or 1.5 seconds

Ping Google Analytics "Log" and the time in hh:mm:ss at intervals - Stephan's code. Seems to log custom event with GA every 1 or 1.5 seconds, with seconds elapsed since the analytics JS started running.

// Converts number of seconds to hh:mm:ss
if( typeof Number.prototype.toHHMMSS !== 'function' ) {
  Number.prototype.toHHMMSS = function (){
		var hours = Math.floor(this / 3600);
		var minutes = Math.floor((this - (hours * 3600)) / 60);
		var seconds = this - (hours * 3600) - (minutes * 60);
		var str;
		str = ( hours === 0 ? '' : (hours < 10 ? '0' + hours : hours ) + ':' );
		str += ( minutes < 10 ? '0' + minutes : minutes ) + ':';
		str += ( seconds < 10 ? '0' + seconds : seconds );
		return str;
	}
}

var ping = function(){
	var secs = new Date()
	secs = Math.floor( (secs-time )/1000 )
	var tos = secs.toHHMMSS()
	_gaq.push(['_trackEvent', 'Time', 'Log', tos, secs, true]);
	rate = rate*1.5
	pings = window.setTimeout(function () {
		ping()
	}, rate*1000);
}


// Timed ping
rate = 1 // secs
time = new Date()
clearTimeout( pings )
pings = window.setTimeout(function () {
  ping()
}, rate*1000);