newrelic.setCustomAttribute

newrelic.setCustomAttribute(string NAME, string VALUE)
Adds a user-defined attribute name and value to subsequent events on the page.

Requirements

Requires New Relic Browser agent version nr-593 or higher.

Description

The Browser API call adds a user-defined attribute name and value to subsequent events on the page. Make this call before the window load event fires (when that data is transmitted) in order for the attributes to be included in the PageView event. Once an attribute is set, New Relic Insights records it with all PageAction events until the page is reloaded or the attribute is manually unset.

If you are using SPA monitoring with a compatible agent version, attributes set with this call will also be included in newrelic.interaction events. However, attributes set using the SPA API will take precedence over these attributes.

Parameters

Parameter Description

name

string

Required. Name of the attribute. Appears as column in the PageView event. It will also appear as a column in the PageAction event if you are using it.

Avoid using reserved NRQL words when you name the attribute/value.

value

string

Required. Value of the attribute. Appears as the value in the named attribute column in the PageView event. It will appear as a column in the PageAction event if you are using it. Custom attribute values cannot be complex objects, only simple types such as strings and numbers.

Avoid using reserved NRQL words when you name the attribute/value.

Example(s)

Get JavaScript/jQuery for HTML elements

This example uses JavasScript/jQuery to get the values of the following HTML elements on the Drupal-generated pages for New Relic's Docs site:

  • <link rel="shortlink" href="/node/1111" />
  • <h1>Using NRQL</h1>

New Relic reports them to Insights as custom attributes. This is useful to query PageView and PageAction events in Insights.

var node_id = '';
node_id= jQuery("link[rel='shortlink']").attr("href");
var node_title = '';
node_title= jQuery('h1').text();

if (typeof newrelic == 'object') {
  newrelic.setCustomAttribute('nodeId', node_id);
  newrelic.setCustomAttribute('title', node_title);
}