Skip to content

Instantly share code, notes, and snippets.

@rachelmyers
Created May 30, 2013 17:31
Show Gist options
  • Save rachelmyers/5679654 to your computer and use it in GitHub Desktop.
Save rachelmyers/5679654 to your computer and use it in GitHub Desktop.
Front End Performance Monitoring
TrackTiming = function(category, feature) {
this.category = category;
this.feature = feature;
this.startTime = new Date().getTime();
this.endTime = null;
return this;
};
TrackTiming.prototype.startTime = new Date().getTime(); // shared startTime
TrackTiming.prototype.setEnd = function() {
this.endTime = new Date().getTime();
return this;
};
TrackTiming.prototype.sendFeatureTiming = function() {
var time = this.endTime - this.startTime;
var twoMinutes = 1000 * 60 * 2;
if (time < twoMinutes && time > 0) {
window._gaq.push(['_trackTiming',
this.category, this.feature, time]);
}
return this;
};
var tt = new TrackTiming(); // init
$(‘#myImg’).on('load', function() {
tt.setEndTime().sendFeatureTiming();
});
TrackTiming.prototype.sendFeatureTiming = function() {
var time = this.endTime - this.startTime;
var twoMinutes = 1000 * 60 * 2;
if (time < twoMinutes && time > 0) {
window._gaq.push(['_trackTiming',
this.category, this.feature, time]);
}
return this;
};
var tt = new TrackTiming(); // init
$(‘#myImg’).on('load', function() {
tt.setEndTime().sendFeatureTiming();
});
var measurements = [];
var now = new Date();
var checkTime = function(measurements) {
var newNow = new Date();
measurements.push(newNow - now);
now = newNow;
setTimeout(checkTime,300);
};
// collects an array of times:
// measurements = [357,300,300,424,404,300]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment