Solved

Fetching usage time for a user from Amplitude API

  • 13 March 2023
  • 5 replies
  • 183 views

We're looking at surfacing to the user their time spent in app and noticed Amplitude already by default tracks this, but getting a successful response from the dashboard API (User Activity) has not been fruitful (usage_time is currently returning null from this endpoint even though usage time shows up fine in the dashboard, and it’s unclear why).

There might also be a better endpoint for what we want as I'm sure fetching past activity for a user is pretty expensive - we only want the usage_time property for the user. Is there a lighter-weight, non rate-limited endpoint we could use?

icon

Best answer by Saish Redkar 20 March 2023, 19:09

View original

5 replies

Userlevel 6
Badge +9

Thanks for reaching out here @kennyw Usage Time is calculated by:sum( max(event_time in session) - session_id)i.e sum of usage time of all sessions, in milliseconds. For additional information about the dashboard API visit our developer docs here. I’m going to share this with our support team who can assist with the second part of your question.

Userlevel 7
Badge +10

Hi @kennyw 

From what details you have provided above, here are couple of points -

  • Are you seeing Usage Time populated in the user profile as seen below?

    Or is it empty?

    • Ideally, if you are capturing session id ( not equal to -1 ) on all your events via Amplitude SDKs then you should be seeing that metric populated ( based on aggregation of user’s total lifetime spent in the app )

  • If you are using a custom session definition, then you might not be able to see Total Sessions/ usage time in the user profile.

AFAIK, the user activity endpoint is the only way of getting the "usage_time" for an individual user.

@Saish Redkar Yes I do see Usage Time populated in the user profile. It only shows up as null in the user activity endpoint response.

We want to fetch usage time on a per-user per-session basis, so we can see ourselves hitting the rate limit for the user activity endpoint pretty easily. Do you know if there are any future plans for supporting a user property endpoint or any workarounds for our use case? Thank you!

Userlevel 7
Badge +10

That’s weird. Ideally, the user profile’s Total Sessions and Usage Time should be mapping to the user activity response datapoints of "num_sessions" and  "usage_time". Not sure what’s the issue here.

Maybe Amplitude Support team can help out here if that’s a bug.

Re. the per-user per-session basis requirement, I don’t know if there is any endpoint which does that for now. The user activity endpoint looks the easiest option. All the other relevant ones are for aggregate session metrics.

You can try creating the desired session chart and fetch some data using the existing chart endpoint if that fits your case.
 

num_sessions, num_events, and usage_time are null from the endpoint (I’ve tested for multiple users), and those fields populate fine on the user profile.

This is what the end of the response object looks like (omitted other fields).

{"events": [], "userData": {... "aliasing_user_ids": [], "aliased_user_id": null, "aliasing_profiles": {}, "num_events": null, "num_sessions": null, "usage_time": null, "device_ids": null}, "metadata": {"missingData": false, "queryIds": [], "cached": false, "timeComputed": null, "kronosRuntime": 0, "kronosRequestDuration": 0, "subCluster": 0}}%

 

Reply