Last active
August 29, 2015 14:26
-
-
Save arthur-debert/834e3ad91e8f16fa88fc to your computer and use it in GitHub Desktop.
Quick & Dirty tracker of user UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Allows to quickly asses amount of work (clickes, key presses, page reloads) | |
users have to do while performing a task. | |
Usage: | |
var tracker = new UserInteractoinStats(); // once you will begin doing the stats | |
// Once you want to see results | |
tracker.stats(); | |
This will also survive page loads, using local storage. | |
If you want to clear all data, just : | |
tracker.clear() | |
If you want to run multiple trackers at the same time, you can | |
shard their counters using a prefixed name. Just initialize the | |
tracker with the name you want: | |
tracker = new UserInteractionStats("fillLoginForm"); | |
// do part of the work here | |
tracker = new UserInteractionStats("errorCorrection"); | |
// more ui interaction goes on | |
tracker.stats('fillLoginForm'); // user the fillLoginForm | |
tracker.stats('errorCorrection') | |
*/ | |
function UserInteractionStats(name){ | |
name = name || "anon"; | |
var self = this; | |
this.keyPressedKey = name + 'numKeypresses'; | |
this.mousePressedKey = name + 'numClick'; | |
this.pageLoadedKey = name + 'numPageLoads'; | |
this.startedAtKey = name + 'startedAt'; | |
this.reset(); | |
this.keyPressed = function(){ | |
return self.incr(self.keyPressedKey); | |
}; | |
this.mousePressed = function(){ | |
return self.incr(self.mousePressedKey) | |
}; | |
this.pageLoaded = function(){ | |
return self.incr(self.pageLoadedKey); | |
}; | |
if (!sessionStorage.getItem(this.startedAtKey)){ | |
sessionStorage.setItem(this.startedAtKey) | |
} | |
document.addEventListener('keyup', this.keyPressed) | |
document.addEventListener('click', this.mousePressed) | |
window.addEventListener('beforeunload', this.pageLoaded) | |
} | |
UserInteractionStats.prototype.incr = function(propName){ | |
sessionStorage.setItem(propName, parseInt(sessionStorage.getItem(propName) || 0) + 1); | |
return true; | |
} | |
UserInteractionStats.prototype.stats = function(){ | |
console.log("Key Presses: " + sessionStorage.getItem(this.keyPressedKey)); | |
console.log("Mouse Clicks: " + sessionStorage.getItem(this.mousePressedKey)) ; | |
console.log("Page Loads: " + sessionStorage.getItem(this.pageLoadedKey)); | |
var t = Math.floor(new Date().getTime() - parseInt(sessionStorage.getItem(this.startedAtKey)) / 1000)/1000 | |
console.log("Time: " + t); | |
} | |
UserInteractionStats.prototype.reset = function(){ | |
document.removeEventListener('keyup', this.keyPressed, false) | |
document.removeEventListener('click', this.mousePressed, false) | |
window.removeEventListener('beforeunload', this.pageLoaded, false) | |
} | |
UserInteractionStats.prototype.clear = function(){ | |
sessionStorage.removeItem(this.keyPressedKey, 0); | |
sessionStorage.removeItem(this.mousePressedKey, 0); | |
sessionStorage.removeItem(this.startedAtKey, 0); | |
sessionStorage.removeItem(this.pageLoadedKey, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment