Skip to content

beforeSend (Data Interception and Modification)

The RUM SDK executes the beforeSend method before sending each piece of data. By customizing the implementation of this method, you can achieve the following operations:

  • Modify part of the data
  • Intercept sending data

beforeSend provides two parameters:

function beforeSend(event, context)

event is generated by the SDK to collect various metric data objects. For context, refer to the following specific information:

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

Modify Part of the Data

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

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

The fields allowed to be modified by the SDK are listed in the following table:

Attribute Type Description
view.url string Page address
view.referrer string Page referrer
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 Sending Data

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

  • true indicates that this piece of data needs to be reported
  • false indicates that this piece of 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? ×