Community Tip: Session length tracking

This Community Tip will explore the utility of session length tracking and describe how to add this metric to your iOS, Android, or JavaScript app.

Mixpanel’s Engagement Analytics is designed as an event-driven tracking tool for drawing actionable insights from your users’ engagement. Traditionally, businesses tracked session lengths as a proxy for user engagement. As you get started with your Mixpanel implementation, you will notice that Mixpanel does not include a default built-in session calculation. Instead, we encourage you to focus on the actions that constitute engaged usage of your application, site, or other web-connected widget. For most businesses, session length is not the best proxy for engagement, but for some, session length is the best proxy.

This Community Tip will describe the implementation of session length tracking in our client-side libraries. Additionally, we will recommend actions for businesses to track instead of session lengths.

Session definitions may vary

Session length is the amount of time a user interacts with your website or app before becoming disengaged. What constitutes a session length can greatly vary across businesses, between mobile and web, and even for different use cases in an application. For example, does a particular event signify the end of a session or should 30 minutes (or some other time period) of inactivity equate to session-end? Given the custom nature of Mixpanel, these are not analysis decisions we wanted to predetermine for you.

Tracking session length

Tracking session length is entirely possible! While there is no default session calculation, Mixpanel, being Mixpanel, allows you to customize session length in the context of your project.

The steps to tracking session length are to determine

  • What triggers a session-start
  • What constitutes a session-end event, programmatically
  • how much time elapsed between the start and end

You can then append the elapsed time between these two events as an event property to either the session-end event and/or the first event of the next session.


The iOS library contains a method called timeEvent. timeEvent allows you to start a timer at a designated session start and stop the timer at a session-end event. Once the track call is made for the session-end event, an event property (in seconds) called “Duration” is sent with the session-end event.

// start the timer for the event "App Close"
[mixpanel timeEvent:@"App Close"];
// stop the timer once "App Close" event is called
[self appCloseWithSuccessHandler:{
    [mixpanel track:@"App Close"];


The Android library also contains a method called timeEvent that works in a very similar way. timeEvent starts a timer when the method is called and will send a “Duration” event property (in seconds) with the defined session-end event once the track call is made for the event.

// start the timer for the event "App Close"
mixpanel.timeEvent("App Close");
// stop the timer if the AppClose() method returns true
    mixpanel.track("App Close");

As a note, the timeEvent method, in both the iOS and Android libraries, is not exclusively for session-length tracking and can be used to measure how long any activity takes.


The challenge with tracking session length in JavaScript is programmatically defining a session-end event. It is essentially impossible to complete a tracking API call when a user leaves your site because there is no robust handler in JavaScript for closing a browser window.

In fact, most session length web metrics are calculated by adding the time spent on the previous page once a user is redirected to a new page within your website. Session-length tracking is dependent upon landing on a new page and consequently, the time spent on the final page of your website before leaving the site is excluded from the calculation, underestimating a user’s true session length and weakening the metric’s accuracy.

However, if you can programmatically define a session-end event in the context of your website, for example, a Log Out event, you can return the time between the start and end event using mixpanel.time_event.

time_event takes an event name as an argument. To track session length, you could call time_event at the session-start event with the session-end event name in the argument. When the track call is made for the session-end event, an event property called “Duration” will be appended to the session-end event with the elapsed time in seconds as the property value.

// track the elapsed time between a Log In and Log Out event
//call time_event at Log In
mixpanel.time_event("Log Out");
//call time_event again at Log Out to send an event with the session time
mixpanel.track("Log Out");

Visualizing session length in your Mixpanel project

Regardless of the library you are using, the Segmentation report will allow you drill down into your defined session-end event and segment by the “Duration” property (“$duration” in our backend).

Since Duration is sent as an integer, you can easily utilize any of Mixpanel’s numeric operators such as “AVG”, “SUM”, “MIN”, or “MAX”. The Segmentation report below shows the average session-length for a mobile gaming app where the session-end event is “App Close”.

Even if we dig deeper into this duration average and look at duration average by current level (for levels between 0 – 10), average duration fails to provide insights on users’ actions. Do users on level 8 tend to have a longer session length on average because the next level is too challenging and users are getting stuck? Does completing the next level simply require more time? Are users spending time engaging with another feature on the app, like a chat room? Session length simply often does not provide actionable information about users’ engagement.

Segmenting by session length is also available in our Retention and Funnels report.

Actionable session-based metrics to consider

Finally, there are some session-based metrics that do not require session-length tracking that may prove to be more actionable.

  • Average number of sessions per user within an hour, day, week, or month. You can visualize this metric by selecting a Session Start event, such as App Open, and building an average Segmentation report segmented by an identifier event property, such as user_id. This metric could help you identify your power users or users who may be at risk of churning.
  • First-session data

Have any questions or suggestions for other session-based metrics? Please email support@mixpanel.com to talk to someone smart, quickly.

Get the latest from Mixpanel
This field is required.