SPA: onEnd

onEnd(function $callback)
Adds custom attributes for SPA interactions to the end of an event.

Requirements

Agent version nr-963 or higher.

SPA calls require New Relic Browser Pro.

Description

Similar to the SPA getContext() method, this method:

  • Takes a callback that is invoked once the interaction has finished.
  • Passes the same object passed to callbacks given to getContext().

This allows you to alter the event at the end of the function; for example, to add custom attributes to the event.

Parameters

Parameter Description

$callback

function

Required. A function that accepts the interaction context object as its only argument. You can invoke methods to modify the interaction, such as:

However, methods that have asynchronous side effects, such as this onEnd call, will not have an effect. Other calls with asynchronous side effects include:

Return value(s)

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

Example(s)

// router.js
router.addRoute('/dashboard', () => {
    const interaction = newrelic.interaction().onEnd(ctx => {
        interaction.setAttribute(
            'averageChartLoadTime',
            ctx.totalChartLoadTime / ctx.chartLoadCount
        )
    })
    getCharts().forEach(loadChart)
})

// chart-loader.js
function loadChart (chart) {
    const start = Date.now()
    chart.load().then(() => {
        const loadTime = Date.now() - start
        interaction.getContext(ctx => {
             ctx.totalChartLoadTime = (ctx.totalChartLoadTime || 0) + loadTime
             ctx.chartLoadCount += (ctx.chartLoadCount || 0) + 1
        })
    }
}