Reputation: 988
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
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
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
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
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