---
title: setUserId
source: https://docs.newrelic.com/docs/browser/new-relic-browser/browser-apis/setuserid
---

## Syntax

```js
newrelic.setUserId(value: string|null, resetSession?: boolean)
```

Adds a user-defined identifier string to subsequent events on the page.

## Requirements

-   Browser Lite, Pro, or Pro+SPA agent (v1.230.0 or higher)
-   If you're installing the browser agent via npm and building a custom agent with selected features, you must enable at least one feature when creating the `Agent` instance. For example, add the following in the`features` array:

    ```js
    import { Metrics } from '@newrelic/browser-agent/features/metrics'

    const options = {
      info: { ... },
      loader_config: { ... },
      init: { ... },
      features: [
        Metrics
      ]
    }
    ```

    For more information, see the [npm browser installation documentation](https://www.npmjs.com/package/@newrelic/browser-agent#new-relic-browser-agent).

## Description

Upon executing this function with a valid value, the browser agent records the value as the `enduser.id` attribute with all events until the attribute is manually unset. The identifier will be stored in the browser, so that subsequent page visits of the _same_ origin attach it on events **within a session** span. Note that this functionality may fluctuate depending on end-user browser privacy settings. If this function is called with a `value = null`, any existing user ID will be deleted from **both** the current page's events and the storage.

The ID will be attached to JavaScriptError events in particular for [Errors Inbox](https://docs.newrelic.com/docs/errors-inbox/errors-inbox/) usage. If you are using [SPA monitoring](https://docs.newrelic.com/docs/browser/single-page-app-monitoring/get-started/welcome-single-page-app-monitoring) with a compatible agent version, user ID will also be included in [`newrelic.interaction`](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-apis/browser-spa-api-newrelicinteraction) events.

Starting with agent version 1.307.0, if the `resetSession` attribute is set to `true`, when the user identifier is updated, the browser agent session resets. However, the session does not reset if the `enduser.id` attribute is not yet specified.

## Parameters

| Parameter                  | Description                                                                                                                                                                                                                                                                          |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `value` _string_ OR _null_ | Required. A string identifier for the end-user, useful for tying all browser events to specific users. The `value` parameter does not have to be unique. If IDs should be unique, the caller is responsible for that validation. Passing a `null` value unsets any existing user ID. |
| `resetSession` _boolean_   | Optional. Specifies whether to reset the browser agent session when the user identifier is updated. The session resets only if the `enduser.id` attribute has an assigned value.                                                                                                     |

## Examples - one user per machine/device

### Marking the start of a user session

```js
newrelic.setUserId('user-1234')
```

### Stop attributing events to the current user

```js
// Note: events are still attributed to the same session id
newrelic.setUserId(null)
```

## Examples - multiple users per machine/device

### Marking the start of a user session

```js
// Note: associates the specified userid to the current session id
newrelic.setUserId('user-1234', true)
```

### Switching to another user session

```js
// Note: resets the session, events will be attributed to a new user + session id
newrelic.setUserId('user-567', true)
```

### Ending a user session

```js
// Note: resets the session, effectively ending it.  Events will be attributed to a new session id going forward.
newrelic.setUserId(null, true)
```
