# Session Management

Events collected by the SDK are annotated with a session identifier (ID). This session identifier is utilized to group events emitted by the SDK and can be used to retrieve events from a specific session.

The SDK manages the used session identifier using one of the two following strategies:

* `ActivityBased` (Kotlin) / `activityBased` (Swift) / `SessionStrategy.Activity` (JavaScript): A session strategy that generates a new session ID after a certain period of app inactivity. The
inactivity duration is measured by the minutes elapsed since the last log. The session ID is persisted to disk and survives app restarts.
* `Fixed` (Kotlin) / `fixed` (Swift) / `SessionStrategy.Fixed`: A session strategy that never expires the session ID but does not survive process restart.

The minimal session strategy setup looks as follows:

=== "Android (Kotlin)"

    ```kotlin
    // `ActivityBased` session strategy
    Logger.start(
      // ...
      sessionStrategy = SessionStrategy.ActivityBased()
    )

    // `Fixed` session strategy
    Logger.start(
      // ...
      sessionStrategy = SessionStrategy.Fixed
    )
    ```

=== "Android (Java)"

    ```java
    // `ActivityBased` session strategy
    Logger.start(
      // ...
      new SessionStrategy.ActivityBased()
    );

    // `Fixed` session strategy
    Logger.start(
      // ...
      new SessionStrategy.Fixed()
    );
    ```

=== "iOS (Swift)"

    ```swift
    // `activityBased` session strategy
    Logger.start(
      // ...
      sessionStrategy: .activityBased()
    )

    // `fixed` session strategy
    Logger.start(
      // ...
      sessionStrategy: .fixed()
    )

    ```

=== "iOS (Objective-C)"

    ```objective-c
    // `activityBased` session strategy
    [CAPLogger
      // ...
      sessionStrategy: [CAPSessionStrategy activityBased]
    ];

    // `fixed` session strategy
    [CAPLogger
      // ...
      sessionStrategy: [CAPSessionStrategy fixed]
    ];

    ```

=== "React Native"

    ```javascript
    // `activityBased` session strategy
    init("<your-api-key>", SessionStrategy.Activity);

    // `fixed` session strategy
    init("<your-api-key>", SessionStrategy.Fixed);

    ```

## Retrieving Session ID

The current session identifier can be retrieved when needed using the `Logger` getter. Session ID is only available after the Logger has been started.

=== "Android (Kotlin)"

    ```kotlin
    Logger.sessionId
    ```

=== "Android (Java)"

    ```java
    Logger.getSessionId();
    ```

=== "iOS (Swift)"

    ```swift
    Logger.sessionID
    ```

=== "iOS (Objective-C)"

    ```objective-c
    [CAPLogger sessionID];
    ```

=== "React Native"

    ```javascript
    import { getSessionID } from '@bitdrift/react-native';

    getSessionID();
    ```

## Retrieving Session URL

Similarly, the full session permalink URL can be retrieved using the `Logger` getter. This is particularly helpful when [integrating with external systems](../integrations.md). Session URL is only available after the Logger has been started.

=== "Android (Kotlin)"

    ```kotlin
    Logger.sessionUrl
    ```

=== "Android (Java)"

    ```java
    Logger.getSessionUrl();
    ```

=== "iOS (Swift)"

    ```swift
    Logger.sessionURL
    ```

=== "React Native"

    ```javascript
    import { getSessionURL } from '@bitdrift/react-native';

    getSessionURL();
    ```

## Generating New Session ID

It may be desirable to generate a new session identifier in response to specific user action(s) (e.g., user logging out) or other events.

A new session identifier can be generated using a simple method call:

=== "Android (Kotlin)"

    ```kotlin
    Logger.startNewSession()
    ```

=== "Android (Java)"

    ```java
    Logger.startNewSession();
    ```

=== "iOS (Swift)"

    ```swift
    Logger.startNewSession()
    ```

Depending on the session strategy used, the new session identifier is generated in the following way:

* `ActivityBased` (Kotlin) / `activityBased` (Swift) / `Activity: A random, unique session identifier is generated each time the `startNewSession` method is called.
* `Fixed` (Kotlin) / `fixed` (Swift): A session identifier is set to the identifier retrieved using the `sessionIdGenerator` (Kotlin) lambda / `sessionIDGenerator` (Swift) closure. This closure is passed to the initializer of the `Fixed` (Kotlin) / `fixed` (Swift) session strategy.

For the `Fixed` (Kotlin) / `fixed` (Swift) session strategy, a session ID generator can be passed in the following way:

=== "Android (Kotlin)"

    ```kotlin
    var counter = 0;
    Logger.start(
      // ...
      sessionStrategy = SessionStrategy.Fixed(sessionIdGenerator = { (++counter).toString() })
    )

    println(Logger.sessionId) // prints "1"

    Logger.startNewSession()

    println(Logger.sessionId) // prints "2"
    ```

=== "Android (Java)"

    ```java
    AtomicInteger counter = new AtomicInteger(0);
    Logger.start(
      // ...
      new SessionStrategy.Fixed(() -> Integer.toString(counter.incrementAndGet()))
    );

    println(Logger.getSessionId()); // prints "1"

    Logger.startNewSession();

    println(Logger.getSessionId()); // prints "2"
    ```

=== "iOS (Swift)"

    ```swift
    var counter = 0
    Logger.start(
      // ...
      sessionStrategy: .fixed {
        counter += 1;
        return \"(counter)"
      }
    )

    print(Logger.sessionID) // prints "1"

    Logger.startNewSession()

    print(Logger.sessionID) // prints "2"
    ```

## Observing Session ID Changes

In the case of the `ActivityBased` (Kotlin) / `activityBased` (Swift) session strategy, the session identifier is managed on behalf of SDK customers and can change without their explicit request. For this reason, the SDK provides a way for customers to register for session identifier changes specific to this session strategy type:

=== "Android (Kotlin)"

    ```kotlin
    Logger.start(
      // ...
      sessionStrategy = SessionStrategy.ActivityBased(
        inactivityThresholdMins = 30,
        onSessionIdChanged = { newSessionId ->
          // Do something with the new session ID
        }
      )
    )
    ```

=== "Android (Java)"

    ```java
    Logger.start(
      // ...
      new SessionStrategy.ActivityBased(
        30, //inactivityThresholdMins
        newSessionId -> {
          // Do something with the new session ID
          return Unit.INSTANCE;
        }
      )
    );
    ```

=== "iOS (Swift)"

    ```swift
    Logger.start(
      // ...
      sessionStrategy: .activityBased(
        inactivityThresholdMins: 30,
        onSessionIDChanged: { newSessionID in
          // Do something with the new session ID
        }
      )
    )
    ```
