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

## Syntax

```js
newrelic.interaction().getContext(function $callback)
```

Stores values for the current SPA interaction asynchronously 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 API method takes a callback that will be invoked asynchronously with the context object associated with the current interaction. Use this for aggregating data associated with the current interaction. These values can be used in other parts of your code.

This context is also provided by the [`onEnd`](https://docs.newrelic.com/docs/browser/new-relic-browser/browser-agent-spa-api/spa-on-end) call.

## Parameters

| Parameter              | Description                                                                            |
| ---------------------- | -------------------------------------------------------------------------------------- |
| `$callback` _function_ | Required. A function that accepts the interaction context object as its only argument. |

## Return values

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

## Examples

```js
router.addRoute('/products/{productId}', params => {
  newrelic.interaction().getContext(ctx => ctx.productId = params.productId);
  renderProduct(params.productId);
  updateHash();
});

window.addEventListener('hashchange', (ev) => {
  const interaction = newrelic.interaction();
  interaction.getContext(ctx => {
    if (ctx.productId) {
      interaction.setAttribute('productId', ctx.productId);
    }
  });
});
```
