• Log in

setCustomAttribute (browser agent API)

Syntax

newrelic.setCustomAttribute(string $name, string OR number $value)

Adds a user-defined attribute name and value to subsequent events on the page.

Requirements

Agent version nr-593 or higher.

Description

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, the New Relic platform 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.

Errors for custom attributes will be included in events on the JS Errors page. To view or log errors for a custom attribute via API, use the browser API's noticeError call.

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

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

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

Examples

Get JavaScript/jQuery for HTML elements

This example uses JavaScript/jQuery to get the values of the following HTML elements on a Drupal-generated page:

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

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

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);
}
Copyright © 2022 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.