Sessions: How To Track

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.


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"];


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 
    mixpanel.track("App Close");


The challenge with tracking session length in JavaScript is programmatically defining a session-end Event. navigator.sendBeacon() supports this kind of use case. This could be used for exit tracking, and is also the safest, most sensible way to insure any non-sent analytics do get sent out before leaving the page.

An example use:

window.addEventListener("unload", ()=> navigator.sendBeacon(url, data));

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.

Is this article helpful?
18 out of 45 found this helpful



Article is closed for comments.