Sign In Register

How can we help you today?

Start a new topic

Recording session duration per user


We'd like to record the session duration (in seconds/mins) for each logged in user. We can see that the Analytics API provides AverageSessionDuration (once EndSessionRequest is logged) for all users combined but are there any existing methods to record it for individual users? Just to avoid us reinventing the wheel. 

If you could advise which events to use to trigger the start/end of recording the session, that would be helpful too.


2 people have this question

I can see "sessionTimeTotal" is logged under System->Player which is what we're looking for, but there doesn't seem to be any get() or method in Spark.setScriptData("string", response...) to read this value in Cloud Code? Please advise on how to read this data.

1 person likes this

+1. Would be interested to see an answer to this too. (i.e. Gaining access to the sessionTimeTotal and sessionCount properties in the system->player collection).

Here's the reply from the ticket:

The sessionTimeTotal and sessionCountTotal values in the Player document is used in background calculations for analytics purposes, it cannot be returned or modified via the Spark Api.


To record each session in detail you will need to make use on the EndSessionRequestThe SDK will automatically create a new session ID when the application is started, this method can be useful to call when the app goes into the background, on a play session has been finished, to allow reporting on player session length.


From the EndSessionResponse you have access to the sessions duration, which you can use to store session times, in a manner that suits your application. For Eg:


EndSessionResponseCloud code.

Spark.getLog().info("EndSession-" + Spark.getPlayer().getDisplayName() + " has ended their session. Duration: "+ (Spark.getData().sessionDuration /1000).toString() + " seconds.");

Depending on your needs it may be worthwhile to create a runtime or Gds doc per player to store your session durations as well as a count of sessions per player.

The above works fine for us, but not clear how to reset the session once the app goes into the background (on a phone). Still waiting for a reply on that (may need to use a custom timed event to do it properly).

1 person likes this

Thanks Daire, appreciate the update!

Login to post a comment