Skip to content

beforeSend (Data Interception and Data Modification)

RUM SDK executes the beforeSend method before each data is sent, and by customizing the implementation of this method, the following operations can be performed:

  • Modify some data
  • Intercept data transmission

beforeSend provides two parameters:

function beforeSend(event, context)

event is an object generated by the SDK that collects various metrics data. context refers to specific relevant information as follows:

EVENT TYPE CONTEXT
View Location
Action Event
Resource (XHR) XMLHttpRequest, PerformanceResourceTiming
Resource (Fetch) Request, Response, PerformanceResourceTiming
Resource (Other) PerformanceResourceTiming
Error Error
Long Task PerformanceLongTaskTiming

Modify Some Data

window.DATAFLUX_RUM &&
    window.DATAFLUX_RUM.init({
        ...,
        beforeSend: (event, context) => {
            if (event.type === 'resource' && event.resource.type === 'fetch') {
                // Add response headers information to the original data
                event.context = {...event.context, responseHeaders: context.response.headers}
            }
        },
        ...
    });
Note

beforeSend can only modify data fields allowed by the SDK. Modifications outside these fields will be ignored.

The fields that the SDK allows to be modified are listed in the table below:

Property Type Description
view.url string Page address
view.referrer string Referrer of the page
resource.url string Resource address
error.message string Error message
error.resource.url string Error resource address
context string Global custom content, for example: content added via addAction, addError.

Intercept Data Transmission

You can intercept unnecessary data by returning true or false through the beforeSend method.

  • true means this data needs to be reported
  • false means this data should be ignored
window.DATAFLUX_RUM &&
    window.DATAFLUX_RUM.init({
        ...,
        beforeSend: (event) => {
            if (shouldDiscard(event)) {
                return false
            } else {
                return true
            }
            ...
        },
        ...
    });

Feedback

Is this page helpful? ×