D
D
Daniel2019-12-28 19:30:10
API
Daniel, 2019-12-28 19:30:10

How to correctly calculate the time spent by the user online (VK API)?

For quite a long time I can’t think of / catch up with how to correctly implement the algorithm for calculating the time spent by the user online.
I asked the developers of some services that have online tracking functionality, but they answered me somehow fragmentarily or in monosyllables, which did not allow me to finally understand the algorithm and write it in code. It is clear that it is necessary to count the number of last_seen changes (the last time of visiting / sending a request to online) of the user, which will be one second online, but either for some fixed interval (doubtful), or for the period while online = 1 and will not become 0, or in some other way.
From the answers of those developers and reflections, I deduced several possible variants of the algorithm:
1. Count the number of last_seen changes until online=1 becomes 0. For example, requests at 10:00:00; 10:00:05; 10:00:10 gives the user 3 seconds online, then offline.
2. Calculate the time between the first last_seen change and the last while online=1. For example, if the user logged in at 10:00:00 and logged out/stopped changing last_seen at 10:00:25, it means that he was online for 25 seconds.
3. Calculate last_seen for a fixed time (for example, 1 min.), And then just sum up these changes / seconds. For example, online from 10:00:00, did something at 10:00:07, did something else at 10:00:59 = 3 seconds. In the second minute, 5 more actions and then offline. We get that he stayed 3 + 5 = 8 seconds.
Can you please tell me which algorithm is correct? Or, if there is none, please describe a normal and understandable implementation of the online time calculation functionality.
UPD. Judging by the answers, I was not quite understood correctly. I am thinking about an algorithm for calculating the online time of users not of my site / project, but of VK users using the VK API. I gave the corresponding parameters from the VKontakte response API above: online, last_seen, etc. They are returned when users.get is requested with fields=online,last_seen. Online time can be calculated by such services: vkonline.info, Online Time application , etc. I want to understand how, for what period and with what algorithm they count these minutes and seconds while a person is online.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
X
xmoonlight, 2019-12-29
@Dyaminigo

Send the start of the activity to the server:
1. The tab became active
2. Scroll the page/mouse/touch/keyboard/network
1. When the mouse is removed from the viewport
2. When the tab loses focus
3. When the tab is closed
4 By medium-double timeout exclusively for this client only! (Everyone has their own and is different!).
The average double timeout is calculated as follows: the average inactive period for this online session (NOT A REGULAR SERVER SESSION, BUT CALCULATED BY ACTIVITY/INACTIVITY!) Multiply by 2 and decrease the total active time by this value.

S
Sergey Sokolov, 2019-12-29
@sergiks

The answer depends on the algorithm, the frequency of your checks and updates of the last_seen parameter on the VK side.
It is probably worth doing a series of experiments on your account to find out:
This will help to draw conclusions:

  1. how often does it make sense to check the last_seen parameter for one user: every 15 minutes, every 10 minutes, every 5 minutes, every minute?
  2. and only then - how to interpret the received data - is your original question.

D
Dmitry Baibukhtin, 2019-12-29
@PiloTeZ

A simple option, accurate enough for most projects
After each request to the server, we record the time of the visit. If the request to the server is repeated within 10 minutes, subtract the current time minus the last one and add it to the total time spent on the site.
If within 10 minutes the next request did not follow, then the user left the session.

J
JoyceGraham, 2019-12-29
@JoyceGraham

The simplest algorithm is to send a ping from the client side to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question