Skip to content

Instantly share code, notes, and snippets.

@djih
Last active February 2, 2016 22:29
Show Gist options
  • Save djih/d656ffbc4109aa29d00f to your computer and use it in GitHub Desktop.
Save djih/d656ffbc4109aa29d00f to your computer and use it in GitHub Desktop.
Javascript snippet to time how long user spent on page
<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