User Online Time Tracking

I want a User Online Time Tracking system that helps to calculate the time when used online or work on the website.
however, we store the details about login and logout and calculate but what if the user login and doesn’t log out (I think for a particular time, the website does not need to log in again) that means we are used to sessions now can you explain next part :stuck_out_tongue_winking_eye: :kissing_closed_eyes:

There is no “next part”.

Unless you’re using something like websockets to track a continual connection, you cannot determine when someone has closed their browser to leave without logging out.

1 Like

You could infer something about this using sessions since a logged in user’s session will connect every HTTP request with their account so you can look at the timing of requests to gain some notion of activity.

Another possibility is to run javascript in pages to see how long they are open. They’d need to preemptively save this state (to localStorage?) for when the window was closed and then you’d need a method of uploading this data on the next visit (a serviceworker?).

1 Like

If was doing something like this I’d look at creating a piece of Middleware in Django that looks at every request. For every request with a session the middleware could note the timestamp and the user. For a user you would be left with a list of timestamps. From there maybe assume that if a user makes two requests within 5 minutes they are “active” and compress the list. You’d end with a bunch of intervals which you could then add up. It does depend how accurate you want it.

This might not work for your architecture if users don’t generate a lot of page requests during normal activity.

1 Like

In the Chat app, how can I tell if a user is online or not? Will the method be the same??