Evaluate the time spent on a page

Hello, is there any way to evaluate the time a user spent on given page ?
I want to be able to give in seconds the time a user has spent on a given page when he presses the back button or changes page.
Thank you.

I believe the simplest way to achieve this is to make a control variable in your run code and check $rootScope.$on("$stateChangeStart") event.

Something like this… (I don’t test it)

   var lastLog = null;
   $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){
	  if(!lastLog) {
		lastLog = new Date();
	  else {
		//Time stayed in page, now call a function or save it in localStorage
        //You can know what page it is with fromState param
		var now = new Date();
		console.log(now.getTime() - lastLog.getTime());

As I said I don’t test it, but I think this should guide you to the correct answer

1 Like