# Manual Session Tracking in RudderStack SDKs


This guide explains the manual session tracking feature available in the supported RudderStack SDKs ([JavaScript]({{< ref "sources/event-streams/sdks/rudderstack-javascript-sdk/_index.md" >}}), [Android (Java)]({{< ref "sources/event-streams/sdks/rudderstack-android-sdk/_index.md" >}}), [iOS (Obj-C)]({{< ref "sources/event-streams/sdks/rudderstack-ios-sdk/_index.md" >}}), [React Native]({{< ref "sources/event-streams/sdks/rudderstack-react-native-sdk.md" >}}), and [Flutter]({{< ref "sources/event-streams/sdks/rudderstack-flutter-sdk/_index.md" >}})).

## Overview

You can use the manual session tracking feature to define the start and end of a user session.

{{< warning >}}
Manual session tracking overrides the [automatic session tracking]({{< ref "sources/event-streams/sdks/session-tracking/automatic-session-tracking/" >}}).
{{< /warning >}}

RudderStack supports the following manual session tracking methods:

<table>
  <tr>
    <th width="180px">Method</th>
    <th width="180px">Parameters</th>
    <th>Description</th>
  </tr>
  <tr>
    <td rowspan="2"> <code class="inline-code">startSession()</code></td>
    <td>-</td>
    <td>RudderStack creates a new session and passes the current <code class="inline-code">timestamp</code> as the <code class="inline-code">sessionId</code> if you don't pass any parameter.</td>
  </tr>
  <tr>
    <td><code class="inline-code">sessionId</code><br /> <span style="color: #4D4DFF;font-size:12px;">Long integer with minimum length of 10 characters.</span></td>
    <td>RudderStack triggers a new user session if you pass a custom <code class="inline-code">sessionId</code> parameter. <br/><br/> RudderStack <strong>does not recommend</strong> using a decimal number as the <code class="inline-code">sessionId</code>. </td>
  </tr>
  <tr>
    <td><code class="inline-code">endSession()</code></td>
    <td>-</td>
    <td>RudderStack clears the <code class="inline-code">sessionId</code> and ends the session.</td>
  </tr>
</table>

## Persistence scope

The following sections list the persistence scope of manual session tracking in the JavaScript and mobile SDKs.

### JavaScript

The persistence scope of manual session tracking in the JavaScript SDK **does not depend** on whether [automatic session tracking]({{< ref "sources/event-streams/sdks/session-tracking/automatic-session-tracking/javascript/" >}}) is enabled.

If you call `startSession()`, the manual session continues even if you refresh or reopen the web page. To end the session, you must call `endSession()`.

{{< warning >}}
If you identify a user with a new `userId` in an existing session, RudderStack triggers a `reset()` call. This ends the existing session and generates a new one, irrespective of whether `endSession()` is called or not.
{{< /warning >}}

### Mobile SDKs

The persistence scope of manual session tracking in the mobile SDKs (Android (Java), iOS (Obj-C), React Native, and Flutter) **depends** on whether [automatic session tracking]({{< ref "sources/event-streams/sdks/session-tracking/automatic-session-tracking/mobile-sdks.md" >}}) is enabled:

- If automatic session tracking is enabled and you call `startSession()`, then RudderStack disables automatic session tracking **until the app is closed completely**. Once you restart the app, the SDKs resume automatic session tracking.
- If automatic session tracking is disabled and you call `startSession()`, the manual session is active until you end it by calling `endSession()`.

## Sample snippets

The following snippets highlight the use of the manual session tracking methods:

{{< tabs tabTotal="5" >}}
{{% tab tabName="JavaScript" %}}
```javascript
rudderanalytics.startSession() // Starts a new user session and automatically assigns a session ID.

rudderanalytics.startSession(sessionId) // Passes a custom session ID while creating a new session.

rudderanalytics.endSession() // Ends the user session and clears the session ID.
```
{{% /tab %}}
{{% tab tabName="Android (Java) — Legacy" %}}
The manual session tracking methods in the Android (Java) SDK are available in the following languages:

- Java
- Kotlin

The following snippets highlight the use of the manual session tracking methods:

```java
rudderClient.startSession(); // Starts a new user session and automatically assigns a session ID.

rudderClient.startSession(sessionId); // Passes a custom session ID while creating a new session.

rudderClient.endSession(); // Ends the user session and clears the session ID.
```

The corresponding Kotlin code is as follows:

```kotlin
// Starts a new user session and automatically assigns a session ID.
rudderClient.startSession()

// Passes a custom session ID while creating a new session.
rudderClient.startSession(sessionId)

// Ends the user session and clears the session ID.
rudderClient.endSession()
```
{{% /tab %}}
{{% tab tabName="iOS (Obj-C) — Legacy" %}}
The manual session tracking methods in the iOS (Obj-C) SDK are available in the following languages:

- Swift
- Objective-C

The following snippets highlight the use of the manual session tracking methods:

```swift
RSClient.sharedInstance()?.startSession() // Starts a new user session and automatically assigns a session ID.

RSClient.sharedInstance()?.startSession(sessionId) // Passes a custom session ID while creating a new session.

RSClient.sharedInstance()?.endSession() // Ends the user session and clears the session ID.
```

The corresponding Objective-C code is as follows:

```objectivec
[[RSClient sharedInstance] startSession]; // Starts a new user session and automatically assigns a session ID.

[[RSClient sharedInstance] startSession:sessionId]; // Passes a custom session ID while creating a new session.

[[RSClient sharedInstance] endSession]; // Ends the user session and clears the session ID.
```
{{% /tab %}}
{{% tab tabName="React Native" %}}
```typescript
// Starts a new user session and automatically assigns a session ID.
rudderClient.startSession();

// Passes a custom session ID while creating a new session.
rudderClient.startSession(sessionId);

// Ends the user session and clears the session ID.
rudderClient.endSession();
```
{{% /tab %}}
{{% tab tabName="Flutter" %}}
```dart
// Starts a new user session and automatically assigns a session ID.
rudderClient.startSession();

// Passes a custom session ID while creating a new session.
rudderClient.startSession(sessionId);

// Ends the user session and clears the session ID.
rudderClient.endSession();
```
{{% /tab %}}
{{< /tabs >}}

## FAQ

See the [Session Tracking FAQ]({{< ref "sources/event-streams/sdks/session-tracking/faq.md" >}}) guide for answers to some commonly-asked questions on session tracking.

<br />
