To calculate this accurately, you will want to:
- log successful session verifications that happen in your APIs. In the log, you want to include the user ID and the timestamp. These logs should be saved in a db.
- Then, you can run a job which will "club" logs that are within a certain time frame of each other. For example, club all logs that are within 10 mins of each other.
- Then you will have a list of these sets and you can calculate the duration of these sets by taking the timestamp of the max log - time of the min log.
- Then you would sum all these set's times and you would have an estimate of how much time the user has spent on the app.