Docs » Supported integrations in Splunk Observability Cloud » Instrument mobile and web applications for Splunk RUM » Instrument browser-based web applications for Splunk RUM » API reference for Browser RUM instrumentation

API reference for Browser RUM instrumentation πŸ”—

Use the following API methods when creating manual instrumentation for Splunk RUM for Browser. For manual instrumentation examples, see Manually instrument browser-based web applications.

Note

try ... catch blocks can prevent your app from crashing when using the CDN version of Browser RUM.

Methods πŸ”—

setGlobalAttributes πŸ”—

The setGlobalAttributes method adds a list of attributes to every new span. For example, you can use this method to add user metadata to spans. See Add user metadata using global attributes.

SplunkRum.setGlobalAttributes(attributes?: Attributes): void;

Argument

Description

attributes

Object of attributes added to all spans. If undefined, all current global attributes are deleted and no longer added to new spans.

The following example sets different attributes to all new spans:

SplunkRum.setGlobalAttributes({
  'enduser.id': 'Test User',
});
// All new spans now include enduser.id

SplunkRum.setGlobalAttributes({
  'dark_mode.enabled': darkModeToggle.status,
});
// All new spans now include enduser.id and dark_mode.enabled

SplunkRum.setGlobalAttributes()
// New spans no longer include those attributes

getGlobalAttributes πŸ”—

The getGlobalAttributes method retrieves all current global attributes and returns an Attributes object with attributes. It doesn’t take arguments.

SplunkRum.getGlobalAttributes(): Attributes;

The following example shows how to use getGlobalAttributes after using setGlobalAttributes:

SplunkRum.setGlobalAttributes({
  'enduser.id': 'Test User',
});
SplunkRum.setGlobalAttributes({
  'dark_mode.enabled': darkModeToggle.status,
});

const attrs = SplunkRum.getGlobalAttributes();
/* console.log(attrs)
{
  'enduser.id': 'Test User',
  'dark_mode.enabled': true
}
*/

getSessionId πŸ”—

The getSessionId method retrieves the current session ID. It doesn’t take arguments.

SplunkRum.getSessionId(): string;

The following example shows how to retrieve the session ID and add it to the application metadata:

LiveChat.onChatStarted(() => {
  LiveChat.setMetadata('splunk.sessionId', SplunkRum.getSessionId());
});

addEventListener and removeEventListener πŸ”—

You can register event listeners with addEventListener and remove them using removeEventListener.

Event listeners take an object in the form { payload: { /* Depending on event */ }} as the first parameter.

SplunkRum.addEventListener(type: string, listener: Function): void
SplunkRum.removeEventListener(type: string, listener: Function): void

Event

Payload

Description

'session-changed'

sessionId: string (New session ID)

Emitted when the session ID changes.

'global-attributes-changed'

attributes: object (New global attributes)

Emitted when setGlobalAttributes is called.

The following example shows how to add an event listener to track changes of session ID:

SplunkRum.addEventListener('session-changed', (event) => {
  LiveChat.setMetadata('splunk.sessionId', event.payload.sessionId);
});