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

Mixpanel tracks App Session ($ae_session) for mobile events by default. The App Session event is fired if the user spends more than 10 seconds in the app. This event also includes a property, “Session Length,” that reflects the number of seconds the user spent in the session. If you’d like to override the minimum and/or maximum session value you can do so following the details here.

To read more about App Session and other default mobile events collected by Mixpanel, click here.

To track sessions in Javascript, here is the general implementation process:

  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 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?
20 out of 48 found this helpful



Article is closed for comments.