
Sessions help you measure how often and how long users engage with your product. Use the [User Sessions chart](/docs/data/user-properties-and-events) to build a session-based analysis.

## How Amplitude defines sessions

A session is the period when a user has your app in the foreground or has your website open. Session behavior differs between mobile and browser applications:

- For **mobile**, a session begins when the app moves into the foreground. A mobile session ends when the app goes into the background and sends no events for at least five minutes. Events sent within five minutes of each other count toward the current session. You can define a custom session expiration time with `setMinTimeBetweenSessionsMillis(timeout)`, where `timeout` is in milliseconds.
- On a **browser**, a session begins when the website loads and the SDK initializes. A browser session ends when the last event occurs. Web sessions time out after 30 minutes by default. Events sent within 30 minutes of each other count toward the same session. You can customize the browser timeout window with the [Browser SDK configuration options](/docs/sdks/analytics/browser/browser-sdk-2).

Amplitude automatically generates a session ID for each new session. The session ID is the session's start time in milliseconds since **epoch** (also known as the [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time)). All events within the same session share the same session ID. Amplitude SDKs manage session IDs automatically. If you send data to Amplitude through the HTTP API, explicitly set the [session ID](/docs/apis/analytics/http-v2) field to track sessions.

### How Amplitude counts sessions

To count sessions, Amplitude filters for events marked as "active," applies the project's session definition, groups the filtered events into sessions, and counts the grouped sessions.

For more information about setting a project's session definition, review [Manage Organizations and Projects](/docs/admin/account-management/manage-orgs-projects#view-and-edit-your-project-information).

## How Amplitude groups events into sessions

By default, Amplitude uses session ID as the session property. Amplitude groups all events with the same session ID and the same user ID into the same session. The session ID doesn't need to be unique across multiple users. You can change the property you use to group sessions.

Amplitude SDKs automatically generate and manage session IDs for events. For events sent through the [HTTP API](/docs/apis/analytics/http-v2), Amplitude defaults to a session ID of `-1`. A session ID of `-1` excludes the event from all session metrics.

{% callout type="note" %}
A session ID of `-1` commonly occurs when data comes to Amplitude from Segment through a cloud-mode connection. As with data sent through the HTTP API, you need to explicitly set a session ID to track sessions.
{% /callout %}

In a user's event stream, a blue line connects events in the same session.

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 for Amplitude to include the session on that chart.

For example, a session begins on May 17 at 8:00 PM and ends on May 18 at 1:30 AM. The session appears on charts where the selected date range includes May 17, the date when the session began. The date range for the example session can start on or before May 17 and end on or after May 17. If the chart's date range starts on May 18, the session doesn't appear on the chart, even though the session was still active on May 18.

### Start Session and End Session events

By default, Amplitude derives `Start Session` and `End Session` events from each session's 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.

If you need `Start Session` and `End Session` events for analysis beyond session lengths, enable `Start Session` and `End Session` event tracking by adding the following code before initializing the SDK:

For Android:

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

For iOS:

```objc
[Amplitude instance].trackingSessionEvents = YES;
```

For Browser:

```js
amplitude.init(API_KEY, OPTIONAL_USER_ID, {
  defaultTracking: {
    sessions: true,
  },
});
```

### Start Session and End Session limitations

- Session event tracking applies only to Amplitude's [Android](/docs/sdks/analytics/android/android-kotlin-sdk), [iOS](/docs/sdks/analytics/ios/ios-swift-sdk), and [Browser](/docs/sdks/analytics/browser/browser-sdk-2) SDKs.
- `Start Session` and `End Session` events count toward your monthly [event volume limit](/docs/faq/limits-and-quotas).
- Amplitude sends the `End Session` event at the start of the user's next session.
- By default, you can't add extra event properties to automatically generated `Start Session` and `End Session` events. To send event properties for session start and end events, you have two options:
  - Implement your own custom `Open App` and `Close App` events.
  - Use an [Enrichment Plugin](/docs/sdks/sdk-plugins#enrichment-plugins) to add properties to the generated session events. Enrichment plugins let you modify properties in Event objects.

### Out-of-session events

You can log events outside a session by setting the session ID to `-1`. Out-of-session events aren't part of the current session and don't extend the current session. Out-of-session events are useful for events triggered by push notifications.

Out-of-session events are usually server-side events received by Amplitude. For more information, refer to the [HTTP API](/docs/apis/analytics/http-v2) documentation. Out-of-session events appear in a user's event stream as disconnected green squares.

### Configure a custom session definition

By default, Amplitude sorts events into sessions by session ID. You can define sessions without adding instrumentation. A custom session definition can use a constant property, a custom timeout window, or beginning and ending events.

You need Admin or Manager privileges to edit session definitions.

{% callout type="note" %}
Custom session definitions are only available in the User Sessions and Pathfinder charts and user timelines. Sessions only include **active** events.
{% /callout %}

To configure a custom session definition:

1. From the left sidebar, navigate to _Settings > Projects_.
2. Select the project you want to work with.
3. Select **Session Definitions**. The _Session Definitions_ modal appears.
4. Select **Custom Session Definition**.
5. Configure one or more conditions:
   - **Session property**: Select _Select property..._ and choose the event or user property for grouping sessions.
   - **Starting Event and Ending Event**: Select **Starting Event** or **Ending Event** and choose the events that mark the beginning and end of a session. The ending event ends a session if the ending event occurs before the timeout interval elapses.
   - **Session timeout**: Enter the default timeout interval in minutes. Amplitude counts events from the same user that occur within the specified timeout interval as part of one session. Amplitude recommends a default of 30 minutes.

6. Enter the confirmation phrase and select **Save**.

{% callout type="note" %}
Changing the session definition applies to all User Sessions, Funnel Analysis, and Journeys charts, and to the session metric in your project. Review the impact before you configure or change a custom session definition.
{% /callout %}

Amplitude combines conditions with **and** logic. A session must meet all configured conditions to count. If you don't configure any conditions, Amplitude uses session ID as the session-defining property.

To require all events in a session to come from the same source, use session property and timeout window together:

- Session property = `device ID`
- Session timeout = `30 min`

To define sessions based on in-app usage, use starting event and timeout window:

- Start event = `app open`
- Session timeout = `5 min`
