Mixpanel enables you to track sessions for mobile and web apps.
This article provides best practices on how to track mobile and web sessions.
Mobile Session Tracking
By default, Mixpanel tracks "App Session" ($ae_session) for mobile events.
The "App Session" event executes when the user spends more than ten seconds in the app.
This event also includes a property, “Session Length” ($ae_session_length) that reflects the number of seconds the user spent in the session.
To override the minimum or maximum session value, see Adjust Session Length Parameters for App Session Events for details.
To read more about "App Session" and other default mobile events that Mixpanel collects, see Default Mobile Events Collection.
Web Session Tracking
- Decide what triggers a session start.
- Decide what programmatically constitutes a session end.
- Calculate the elapsed time between start and end events.
- Append that elapsed time as an event property to either the session end event or the first event of the next session.
navigator.sendBeacon() for exit tracking. It is also the safest, most sensible way to ensure any non-sent analytics do get sent out before leaving the page.
For 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 "Page Close" event), you can return the time between the start and end event using mixpanel.time_event.
So you could call a "Page Open" event at the start of a session, and then call a "page_close" event when the session ends. Then use mixpanel.time_event to return the time between the start and end event.
When you call the "Page Close" event, an event property, “Duration” appends to the "Page Close" event with the elapsed time in seconds as the property value.