---
title: Set custom user ID for sessions
source: https://docs.newrelic.com/docs/mobile-monitoring/new-relic-mobile/mobile-sdk/set-custom-user-id
---

## Android

### Syntax [#syntax]

#### Java [#java]

```java
NewRelic.setUserId(string $userId)
```

#### Kotlin [#kotlin]

```kotlin
NewRelic.setUserId(userId: String?)
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

This method sets a custom user identifier value to associate all events from a mobile user's session with a specific user ID. This method can be called anytime after the New Relic Android agent starts.

A user identifier is useful for several reasons. Using the [Crash analysis UI](https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-ui/crashes/crash-analysis-group-filter-your-crashes), you can:

-   Find crashes specific to a user. Focusing on one particular crash for one user is often enough to quickly resolve the issue.
-   Filter all crashes to show the occurrences for a user (or group of users) for each crash type; inversely, filter the crash list to show those users affected by each crash type. If the user ID is represented as an email address, it's then easy to reach out and let them know when there is a solution available that addresses those crashes.

    If you do not want to explicitly identify users because of privacy rules, this method is still useful when tracking user segments, such as paid vs. free end users, or registered vs. unregistered users. This allows you to filter or facet on the number and types of crashes experienced by each segment, and do outreach based on that segment.

    For more context on how to use this API, see [Send custom attributes and events](https://docs.newrelic.com/docs/data-apis/custom-data/custom-events/report-mobile-monitoring-custom-events-attributes).

    > #### ⚠️ IMPORTANT
    >
    > The following enhancements were added in Android agent v7.4.0:
    >
    > -   When `setUserId(value: string|null)` is called, if `userID` was previously null and the new value is not null, the current session continues and a new `userID` will be set.
    > -   When `setUserId(value: string|null)` is called, if `userID` was previously not null and the new `userID` is different (including `null`), the current session will be ended and a harvest will be performed. A new session will be created with the new `userID`.

    ### Parameters [#parameters]

    | Parameter | Type     | Description                 |
    | --------- | -------- | --------------------------- |
    | `$userID` | `string` | Required. Sets the user ID. |

    ### Return values [#return-values]

    Returns `true` if it succeeds, or `false` if it doesn't.

    ### Example [#example]

    Here's an example to set a user identifier to associate user session with events and attributes:

    #### Java [#java]

```java
boolean userIdWasSet = NewRelic.setUserId("SampleUserName");
```

#### Kotlin [#kotlin]

```kotlin
val userIdWasSet = NewRelic.setUserId("SampleUserName")
```

## iOS

### Syntax [#syntax]

#### Objective-c

```objectivec
+ (BOOL) setUserId:(NSString*)userId;
```

#### Swift [#swift]

```swift
NewRelic.setUserId(userId: String!) -> Bool
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

This method sets a custom user identifier value to associate all events from a mobile user's session with a specific user ID. This method can be called anytime after the New Relic iOS agent starts.

A user identifier is useful for several reasons. Using the [Crash analysis UI](https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-ui/crashes/crash-analysis-group-filter-your-crashes), you can:

-   Find crashes specific to a user. Focusing on one particular crash for one user often is enough to quickly resolve the issue.
-   Filter all crashes to show the occurrences for a user (or group of users) for each crash type; or, filter the crash list to show those users affected by each crash type. If the user ID is represented as an email address, it's then easy to reach out and let them know when there is a solution available that addresses those crashes.

    If you do not want to explicitly identify users because of privacy rules, this method is still useful when tracking user segments, such as paid vs. free end users, or registered vs. unregistered users. This allows you to filter or facet on the number and types of crashes experienced by each segment, and do outreach based on that segment.

    > #### ⚠️ IMPORTANT
    >
    > The following enhancements were added in iOS agent v7.5.0:
    >
    > -   When `setUserId(value: string|null)` is called, if `userId` was previously null and new value is not null, the current session continues and a new `userId` will be set.
    >
    > -   When `setUserId(value: string|null)` is called, if `userId` was previously not null and new `userId` is different (including `null`), the current session will be ended and a harvest will be performed. A new session will be created with the new `userId`.

    ### Parameters [#parameters]

    | Parameter | Type       | Description                                                      |
    | --------- | ---------- | ---------------------------------------------------------------- |
    | `userID`  | `NSString` | Required. The user ID to be associated with the current session. |

    ### Return values [#return-values]

    Returns `true` if it succeeds, or `false` if it doesn't.

    ### Example [#example]

    #### Objective-C [#obj-c]

```objectivec
BOOL userIdWasSet = [NewRelic setUserId:@"SampleUserName"];
```

#### Swift [#swift]

```swift
let userIdWasSet = NewRelic.setUserId("SampleUserName")
```

## Capacitor

### Syntax [#syntax]

```typescript
setUserId(options: { userId: string; }) => void
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type                  | Description                                                      |
| --------- | --------------------- | ---------------------------------------------------------------- |
| `options` | `{ userId: string; }` | Required. An object that contains the user ID.                   |
| `userId`  | `string`              | Required. The user ID to be associated with the current session. |

### Example [#example]

```typescript
NewRelicCapacitorPlugin.setUserId({ userId: "CapacitorUserId" });
```

## Cordova

### Syntax [#syntax]

```typescript
setUserId(userId: string): void;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type     | Description                                                      |
| --------- | -------- | ---------------------------------------------------------------- |
| `userID`  | `string` | Required. The user ID to be associated with the current session. |

### Example [#example]

```js
NewRelic.setUserId("CORDOVA12934");
```

## Flutter

### Syntax [#syntax]

```typescript
setUserId(String userId): void;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type     | Description                                                      |
| --------- | -------- | ---------------------------------------------------------------- |
| `userID`  | `string` | Required. The user ID to be associated with the current session. |

### Example [#example]

```dart
NewrelicMobile.instance.setUserId("RN12934");
```

## .NET MAUI

### Syntax [#syntax]

```csharp
SetUserId(string userId): bool;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type     | Description                                                      |
| --------- | -------- | ---------------------------------------------------------------- |
| `userID`  | `string` | Required. The user ID to be associated with the current session. |

### Example [#example]

```csharp
CrossNewRelic.Current.SetUserId("User123");
```

## React Native

### Syntax [#syntax]

```typescript
setUserId(userId: string): void;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type     | Description                                                      |
| --------- | -------- | ---------------------------------------------------------------- |
| `userID`  | `string` | Required. The user ID to be associated with the current session. |

### Example [#example]

Set user

```js
NewRelic.setUserId("RN12934");
```

Unset user

```js
NewRelic.setUserId("");
```

## Unity

### Syntax [#syntax]

```csharp
SetUserId(string userId): bool;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type     | Description                                                      |
| --------- | -------- | ---------------------------------------------------------------- |
| `userID`  | `string` | Required. The user ID to be associated with the current session. |

### Example [#example]

```csharp
NewRelicAgent.SetUserId("User123");
```

## Unreal Engine

### Syntax [#syntax]

```csharp
setUserId(FString userId): void;
```

### Description [#description]

Set a custom user identifier value to associate user sessions with analytics events and attributes.

### Parameters [#parameters]

| Parameter | Type      | Description                                                      |
| --------- | --------- | ---------------------------------------------------------------- |
| `userID`  | `FString` | Required. The user ID to be associated with the current session. |

### Example [#example]

```cpp
#include "NewRelicBPLibrary.h"

UNewRelicBPLibrary::setUserId("Unreal12934");
```
