getContext (SPA API)

Syntax

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 using npm to install the browser agent, you must enable the spa feature when instantiating the BrowserAgent class. In the features array, add the following:

    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.

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 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

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);
}
});
});