Last active
February 2, 2016 22:29
-
-
Save djih/d656ffbc4109aa29d00f to your computer and use it in GitHub Desktop.
Javascript snippet to time how long user spent on page
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
<script> | |
// time how long user spent on page | |
// following this example - http://stackoverflow.com/a/16046028 | |
var timeStart = new Date().getTime(); // time when user arrives | |
window.onbeforeunload = function(){ | |
var timeEnd = new Date().getTime(); // time when user leaves | |
var timeDif = parseInt(timeEnd - timeStart, 10)/60000; // calculate difference in minutes | |
amplitude.logEvent('User Left Page', {'time_spent_minutes': timeDif}); | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment