How do I track session length?

The power of Mixpanel is that it's completely customizable, and the data is driven by Events that are important to your particular business (as opposed to more generic one-size-fits-all metrics, like pageviews or, in many ways, session time).

For most businesses, session length (which can be skewed by people leaving apps/windows/tabs open longer than they're being used) is often not the best proxy for user engagement or the best way to understand how users interact with the product. Therefore, Mixpanel does not track session length by default. Instead, Mixpanel recommends choosing Events important to your particular business to truly measure your level of user engagement (e.g., purchases made, articles read, levels completed, etc.).

However, if you do decide that tracking session length is important to your business, you can definitely do so - here is the general implementation process (see specifics by platform below):

  1. Decide what triggers a session start.
  2. Decide what programmatically constitutes a session end.
  3. Calculate the elapsed time between start and end Events.
  4. Append that elapsed time as an Event Property to either the session end Event or the first Event of the next session.

iOS

The iOS library contains a method called timeEvent that 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, Mixpanel sends an Event Property (in seconds) called “Duration” 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"];
}];

Android

The Android library contains a method called timeEvent that 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 that Event:

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

JavaScript

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, 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.

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.