Track sessions

This article helps you:

  • Understand how Amplitude defines and tracks user sessions

  • Learn how to best incorporate sessions into your analyses

In Amplitude, sessions are a useful metric for understanding the frequency and duration of your users' engagement with your product. The most direct way to build a session-based analysis is with the User Sessions chart

Note

You may also find this video on User Sessions helpful.

How Amplitude defines "sessions"

Generally, a session is the period of time a user has your app in the foreground or has your website open. The specifics differ slightly between mobile and web applications:

  • For mobile, a session begins when the app moves into the foreground; it ends when the app goes into the background and fires no events for at least five minutes. All events sent within five minutes of each other count towards the current session. You can define your own session expiration time with setMinTimeBetweenSessionsMillis(timeout), where the timeout input is in milliseconds.
  • On a browser, a session begins when the website loads and the SDK is initialized; it ends when the last event is triggered. Web sessions time out after 30 minutes by default. All events fired within 30 minutes of each other count as part of the same session. Customize this timeout window can with the Browser SDK configuration options.

Amplitude automatically generates a session ID for each new session; that ID is the session's start time in milliseconds since epoch (also known as the Unix timestamp). All events within the same session share the same session ID. If you are using Amplitude's SDKs, this happens automatically. If you send data to Amplitude through the HTTP API, explicitly set the session ID field to track sessions.

How Amplitude tracks your sessions

By default, the setting in Amplitude for the session property is session ID. All events with the same session ID and the same user ID are grouped into the same session. The session ID doesn't need to be unique across multiple users. You can also change the property you use to group sessions.

As noted above, session IDs for events sent through the Amplitude SDKs are automatically generated and managed. However, for events sent through the HTTP API, Amplitude defaults to a session ID of -1. This means it excludes the event from all session metrics.

Note

This commonly occurs when sending data to Amplitude from Segment through a cloud-mode connection. As with sending data through the HTTP API, you need to explicitly set a session ID to track sessions.

SessionId.png

Events included in the same session are connected with a blue line, as shown above.

Amplitude assigns a session to a specific date based on its actual start time. The start time must fall within a chart's selected date range in order for Amplitude to include it on that chart.

For example, consider a session that begins on May 17th at 8:00 PM and ends on May 18th at 1:30 AM. This session appears on charts where the selected date range includes the date on which the session began. In this example, that date range can begin no later than May 17th, and end no earlier than May 17th. If the chart's date range begins on May 18th, this session doesn't appear on the chart, even though the session was still active on that date.

Start Session and End Session events

By default, Amplitude tracks Start Session and End Session events using the beginning and ending times of each session by session ID. Amplitude also uses session ID to calculate session lengths. If you use session IDs, Amplitude doesn't add extra events to your monthly event volume limit.

That said, if trackingStart Session andEnd Session events is critical to your analysis outside of session lengths, you can easily turn on tracking for these events by adding this line of code before initializing the SDK:

For Android:

1Amplitude amplitude = new Amplitude(new Configuration(apiKey = AMPLITUDE_API_KEY, context = applicationContext, trackingSessionEvents = true, ));

For iOS:

1[Amplitude instance].trackingSessionEvents = YES;

For Browser:

1amplitude.init(API_KEY, OPTIONAL_USER_ID, {
2 defaultTracking: {
3 sessions: true,
4 },
5});

Important Notes:

  • This only applies to Amplitude's Android, iOS, and Browser SDKs.
  • Start/End Session events count towards your monthly event volume limit.
  • The End Session event is sent at the start of the user's next session.
  • You can't add extra event properties to these Start Session and End Session events. If you would like to send event properties for session start/end events, try implementing your own custom Open App and Close App events.

Out-of-session events

You can also log events as out-of-session by setting the session ID to -1. Out-of-session events aren't considered part of the current session. Because they don't extend the current session, they can be useful if you're logging events triggered by push notifications.

Out-of-session events are normally server-side events received by Amplitude (see our HTTP API documentation for more details). These events appear in a user's event stream as disconnected green squares.

SessionId

Custom session property

By default, Amplitude sorts events into sessions according to session ID. You can also define a session without instrumentation, simply by setting a constant property, custom timeout window, or beginning and ending events to group sessions.

You need Admin or Manager privileges to edit session definitions.

Note

Custom session definitions are only available in the User Sessions and Pathfinder charts, as well as user timelines. Sessions only include active events.

To set a custom session definition, follow these steps:

  1. From the left sidebar, navigate to Settings > Projects.
  2. Click on the project you want to work with.
  3. Click Session Definitions. The Session Definitions modal appears.
  4. Click Custom Session Definition.
  5. Next, choose the specifications for any of three conditions that define the custom session:
    • Session property: Click Select property... to choose the event or user property you want to use for grouping sessions.
    • Starting Event and Ending Event: Click on Starting Event or Ending Event to choose the events that signifies the beginning and end of a session. The triggering of the ending event ends a session if it occurs before the timeout interval has elapsed.
    • Session timeout: Enter the default timeout interval in minutes. Amplitude counts all events from the same user prior to the interval you specify as being part of a single session. We recommend a default of 30 minutes.

You can define one condition or multiple conditions, but be aware that all conditions you specify must be met for Amplitude to count a session. If you don't define any of these conditions, Amplitude uses session ID as the session-defining property.

  1. Finally, enter the confirmation phrase and click Save.

customSessionDefinition.png

Note

Changing the session definition applies to all User Session, Funnel Analysis, Journeys charts, as well as the session metric, in your project. Be sure you understand what the impacts might be before setting or changing a custom session definition.

Be aware that Amplitude treats combinations of conditions as and logic, meaning all conditions must be met for the session to count. For example, if you want to guarantee that all events in sessions are from the same source, you can do this by using session property and timeout window in tandem:

  • Session property = device ID
  • Session timeout = 30 min

Alternatively, to define sessions based on in-app usage, you can use starting event and timeout window:

  • Start event = app open
  • Session timeout = 5 min
Was this page helpful?

Thanks for your feedback!

September 12th, 2024

Need help? Contact Support

Visit Amplitude.com

Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.