SPA: getContext

newrelic.interaction().getContext([function $callback])
Stores values across the current SPA interaction asynchronously in New Relic Browser.

Requirements

Agent version nr-963 or higher.

SPA calls require New Relic Browser Pro.

Description

This SPA method takes a callback that will be invoked asynchronously with the context object associated with the current interaction. Use to store values associated with the current interaction in New Relic Browser.

This object is shared across the entire interaction and can be used in multiple places. This allows for aggregating data over the course of the interaction.

Parameters

Parameter Description

$callback

function

Optional. A function that accepts the interaction context object as its only argument.

Return value(s)

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

Example(s)

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