This topic describes some methods of the browser monitoring SDK and their usage.

The SDK opens some data reporting methods, which can be called on the page to report more data.

api() for reporting the API call success rate

This method reports the API call success rate on the page. The SDK listens for AJAX requests on the page and calls this method for reporting by default. If data on the page is requested using JSONP or other custom methods (such as the client SDK), you can call api() in the data request method for manual reporting.

Note To call this method, we recommend you set disabledHook to true in SDK configuration items. For detailed configuration, see Configuration items of the browser monitoring SDK.

Call parameter description:__bl.api(api, success, time, code, msg)

Parameter Type Description Required Default value
api String  The method name. Yes N/A
success Boolean Specifies whether the call is successful. Yes N/A
time Number The time consumed by the method call. Yes N/A
code String or number The return code. No The null string ('').
msg String The response information. No The null string ('').

Example

var begin = Date.now(),
    url = '/data/getTodoList.json';

$.ajax({
    url: url,
    data: {id: 123456}
}).done(function (result) {
    var time = Date.now() - begin;
    // Reports that the method call is successful.
    window.__bl && __bl.api(url, true, time, result.code, result.msg);
    // do something ....
}).fail(function (error) {
    var time = Date.now() - begin;
    // Reports that the method call fails.
    window.__bl && __bl.api(url, false, time, 'ERROR', error.message);
    // do something ...
});
			

error() for reporting the error information

This method reports JavaScript (JS) errors or exceptions you want to capture on the page.

Generally, the SDK listens for global errors on the page and calls this method to report exceptions. However, due to the same-origin policy of the browser, error details are usually out of reach. In this case, you must manually report such errors.

Call parameter description: __bl.error(error, pos)

Parameter Type Description Required Default value
error Error The JS error object. Yes N/A
pos Object The location where the error occurs. It contains the following three attributes: No N/A
pos.filename String The name of the file where the error occurs. No N/A
pos.lineno Number The number of the row where the error occurs. No N/A
pos.colno Number The number of the column where the error occurs. No N/A

Example 1: Listen to and report JS errors on the page.

window.addEventListener('error', function (ex) {
    // Event parameters usually contain position information.
    window.__bl && __bl.error(ex.error, ex);
});
			

Example 2: Report a custom error.

window.__bl && __bl.error(new Error('A custom error occurs.'), {
    filename: 'app.js', 
    lineno: 10, 
    colno: 15
});
			

sum() for summation

This methods counts how many times a specific business event occurs.

Call parameter description: __bl.sum(key, value)

Parameter Type Description Required Default value
key String The name of the event. Yes N/A
value Number The cumulative number in each report. Default value: 1. No 1

Example

__bl.sum('event-a');

__bl.sum('event-b', 3);

__bl.sum('group-x::event-c', 2);
			

avg() for average value calculation

This method counts how many times a specific event occurs on average in a business scenario.

Call parameter description: __bl.avg(key, value)

Parameter Type Description Required Default value
key String The name of the event. Yes N/A
value Number The number of reported items. Default value: 0. No 0

Example

__bl.avg('event-a', 1);
__bl.avg('event-b', 3);

__bl.avg('events::event-c', 10);
__bl.avg('speed::event-d', 142.42);
			

addBehavior() for adding a user behavior

This method adds a custom user behavior to the end of the current behavior queue.

The SDK maintains a user behavior queue with a maximum of 100 records. When a JS error occurs, the SDK reports the current behavior queue and clears the queue. You can call this method to add a custom behavior to the end of the current behavior queue.

Note To call this method, you need to set behavior to true in SDK configuration.

Call parameter description: __bl.addBehavior(behavior)

Parameter Type Description Required Default value
data Object Behavior data. Valid values:
  • name: behavior name of the string type. It is required, with a maximum length of 20 characters.
  • message: behavior content of the string type. It is required, with a maximum length of 200 characters.
Yes
page String The page where the behavior happens. No Value of Location. pathname

Example

{
  data: {
    name: 'string',
    message: 'string',
  },
  page: 'string'
}

reportBehavior() for user behavior reporting

When you call this method, the current behavior queue is reported immediately. This method is optional.

If you do not manually call this method, when a JS error occurs, the current behavior queue is automatically reported, with a maximum size of 100. If the queue contains more than 100 behavior records, behavior records are discarded from the header of the queue.

Note To call this method, you need to set behavior to true in SDK configuration.

Call parameter description: __bl.reportBehavior()

No request parameters.

Other methods

Non-log reporting methods are usually used for modifying some configuration items of the SDK.

setConfig() for modifying configuration items

This method allows you to modify some of the configuration items after SDK initialization. For detailed configuration, see Configuration items of the browser monitoring SDK.

Call parameter description: __bl.setConfig(next)

Parameter Type Description Required Default value
next Object The configuration items to be modified and their values. Yes N/A

Example: Change the value disableHook to true to disable automatic API reporting.

__bl.setConfig({
    disableHook: true
});
			

setPage() for setting the name of the current page

This method resets the page name of a page. PV will be reported again by default. This method is generally used for SPAs. For more information, seeAdvanced browser monitoring scenarios.

Call parameter description: __bl.setPage(next, sendPv)

Parameter Type Description Required Default value
page String  The new page name. Yes N/A
sendPv Boolean Specifies whether to report PV. PV is reported by default. No true

Example 1: Set the name of the current page to the current URL hash, and report PV again.

__bl.setPage(location.hash);
			

Example 2: Set the name of the current page to 'homepage' without triggering PV reporting.

__bl.setPage('homepage', false);