mck
mck

Reputation: 988

How to calculate time when user active on a page?

I am working on an application in which requirements are to calculate user total time spent on a page.

I have saved the page opening time in a session variable and get end time when session expires.

 Session["StartTime"] = DateTime.Now;

 protected void Session_End(object sender, EventArgs e)
 {
     Session["EndTime"] = DateTime.Now;

     //Saves in db
 }

Project requirements are to calculate only those time when user opens that page, when user minimizes current browser window that time must be excluded and also if user switches to some another tab in current browser page that time also not recorded.

How to detect that user is currently working on our project page in asp.net or in jquery?

Upvotes: 1

Views: 3568

Answers (4)

meandmax
meandmax

Reputation: 114

You can use to calculate the active time a user has spent o on a page. You can also execute functions based on the active time if needed.

Github: https://github.com/atlassian/browser-interaction-time Docs: https://atlassian.github.io/browser-interaction-time/

Upvotes: 0

yuc
yuc

Reputation: 508

This can also help you to detect the page is active or not, then you can calculate the time.

document.addEventListener("visibilitychange", function() {
  console.log( document.visibilityState );
});

ref: https://developer.mozilla.org/zh-TW/docs/Web/Events/visibilitychange

Upvotes: 1

Jaec
Jaec

Reputation: 390

You can use the $(window).focus() and the $(window).blur() functions, the former to detect when the user is working on the webpage and the latter to pause a javascript timer if the user puts the webpage on the foreground.

Upvotes: 3

Priyank Sheth
Priyank Sheth

Reputation: 2362

You can use Google Analytics for your requirement. Integrate that key and little code you will get from Google after registration. For your ease you can refer to below link I found for you:

How to measure active time on page

Let me know if you get any trouble.

Upvotes: 2

Related Questions