---
title: setAttribute (SPA API)
source: https://docs.newrelic.com/docs/browser/new-relic-browser/browser-apis/setattribute
---

## Syntax

```js
newrelic.interaction().setAttribute(string $key, any $value)
```

Adds a custom SPA attribute only to the current interaction in browser.

## Requirements

-   Browser Pro+SPA agent (v963 or higher)
-   If you're installing the browser agent via npm and building a custom agent with selected features, you must enable the `spa` feature when creating the `Agent` instance. In the `features` array, add the following:

    ```js
    import { Spa } from '@newrelic/browser-agent/features/spa';

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

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

## Description

This method adds a custom attribute to an interaction. If saved, this attribute will be exposed as a new property on the resulting [`BrowserInteraction`](https://docs.newrelic.com/docs/insights/explore-data/attributes/browser-default-attributes-insights#browserinteraction-attributes) event. Unlike attributes added with [`setCustomAttribute()`](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-apis/browser-api-newrelicsetcustomattribute), an attribute added to an interaction will only apply to the current interaction, and it will not be added to [`PageAction`](https://docs.newrelic.com/docs/insights/explore-data/attributes/browser-default-attributes-insights#pageaction-list) events.

New Relic merges these custom attributes with the custom attributes set by calling [`setCustomAttribute()`](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-apis/browser-api-newrelicsetcustomattribute) and with custom attributes set by a server-side agent.

| **Order of precedence** | **Custom attributes**                                                                                       |
| ----------------------- | ----------------------------------------------------------------------------------------------------------- |
| Highest level           | `BrowserInteraction` attributes set with the SPA API: These overwrite attributes set in the other two ways. |
| Next level              | Attributes set by `setCustomAttribute()`: These overwrite server-side custom attributes.                    |
| Lowest level            | Custom attributes set server-side.                                                                          |

Errors for custom attributes will be included in events on the [JS Errors page](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-pro-features/javascript-errors-page-detect-analyze-errors). To view or log errors for a custom attribute via API, use the browser agent API's [`noticeError`](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-spa-api/newrelicnoticeerror-browser-agent-api) call.

## Parameters

| Parameter       | Description                                                                                                                                                                              |
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$key` _string_ | Required. Used as the attribute name on the `BrowserInteraction` event.                                                                                                                  |
| `$value` _any_  | Required. Used as the attribute value on the `BrowserInteraction` event. This can be a string, number, boolean, or object. If it is an object, New Relic serializes it to a JSON string. |

## Return values

This method returns the same API object created by `interaction()`.

## Examples

```js
router.addRoute('/profile', () => {
  const user = getCurrentUser();
  newrelic.interaction()
    .setAttribute('username', user.username)
    .setAttribute('userId', user.id);
  renderProfile(user);
});
```
