Skip to main content

JS Custom Business Interface

JS Custom Error

Using the 'Custom Error' interface allows developers to collect 'try / catch exceptions' and 'business errors' which are then displayed in the 'Exception Analysis' → 'Errors' section of the Tingyun platform. This helps developers to collect exceptions and errors.

  • Related Interface
// You need to save the following code as tingyun@app-fix.js and add it to your project

if(!window['NBSAppAgent']){
function nbs_callMethod(functionName, args) {
var wrap = {'method':functionName,'params':args};
var info = JSON.stringify(wrap);
if(typeof nbsJsBridge != 'undefined') {
nbsJsBridge.parseArguments(info);
}else if(typeof window.webkit != 'undefined'){
if (!window.webkit.messageHandlers['nbsJsBridge']) return;
window.webkit.messageHandlers['nbsJsBridge'].postMessage(info);
}
}

var NBSAppAgent = {};
/**
* Custom Error:
* The length of message should not exceed 1024 bytes.
* The value of metaData supports number, string, array, map types, with a maximum of 128k.
*/
NBSAppAgent.reportError = function(message, metaData, exception) {
if(!exception)
return;
if(!message)
message = '';
if(!metaData)
metaData = {};
var error = {message:exception.message,line:exception.line,column:exception.column,sourceURL:exception.sourceURL,stack:exception.stack,type:exception.name}
nbs_callMethod('reportError', { 'message': message, 'exception': error, 'metaData': metaData });
};
window['NBSAppAgent'] = NBSAppAgent;
}
  • Code Example
<script src="tingyun@app-fix.js"></script>
function jsCustomError() {
try{console.log(abc)}
catch(e){
NBSAppAgent.reportError("JS-Customerror-Webview-jsMessage",{"metaDataKey":"metaDataValue"},e);
}
}

JS Custom Event

Custom events can track any event within the App. Developers can call the 'Custom Event Interface' and set the corresponding upload parameters at any position after the SDK initialization. For example: when a real user clicks on a certain feature button or triggers a certain feature event.

  • Related Interface
// You need to save the following code as tingyun@app-fix.js and add it to your project
if(!window['NBSAppAgent']){
function nbs_callMethod(functionName, args) {
var wrap = {'method':functionName,'params':args};
var info = JSON.stringify(wrap);
if(typeof nbsJsBridge != 'undefined') {
nbsJsBridge.parseArguments(info);
}else if(typeof window.webkit != 'undefined'){
if (!window.webkit.messageHandlers['nbsJsBridge']) return;
window.webkit.messageHandlers['nbsJsBridge'].postMessage(info);
}
}


/**
* @brief Collect custom events
* @param name Event name, cannot be empty, length limit is 1024 characters, exceeding will be truncated to the first 1024 characters
* @param properties Event properties
*/
var NBSAppAgent = {};
NBSAppAgent.reportEvent = function(name, properties) {
if(!name)
return;
if(!properties)
eventProperties = {};
nbs_callMethod('reportEvent', { 'name': name, 'properties': properties });
};
window['NBSAppAgent'] = NBSAppAgent;
}
  • Code Example
<script src="tingyun@app-fix.js"></script>
function jsCustomEvent() {
NBSAppAgent.reportEvent("JS_reportEvent_name",{"key":"value"});
}

JS Custom Business Line

After calling the interface to set the business line, you can filter the corresponding business line name in the platform's 'Exception Analysis' module to analyze the exception data of different business lines.

  • Related Interface
// You need to save the following code as tingyun@app-fix.js and add it to your project

if(!window['NBSAppAgent']){
function nbs_callMethod(functionName, args) {
var wrap = {'method':functionName,'params':args};
var info = JSON.stringify(wrap);
if(typeof nbsJsBridge != 'undefined') {
nbsJsBridge.parseArguments(info);
}else if(typeof window.webkit != 'undefined'){
if (!window.webkit.messageHandlers['nbsJsBridge']) return;
window.webkit.messageHandlers['nbsJsBridge'].postMessage(info);
}
}

/**
* @brief Set the current business line, the same key value will be overwritten, keeping the latest value
* @params businessLine Business name, maximum length is 256 bytes
* @params key The key is a fixed value, bname
*/
var NBSAppAgent = {};
NBSAppAgent.setBusinessLine = function(key,businessLine) {
var ff = key;
if(!key)
return;
if(!businessLine)
return;
nbs_callMethod('setBusinessLine', {'key':key,'value':businessLine});
};
window['NBSAppAgent'] = NBSAppAgent;
}
  • Code Example
<script src="tingyun@app-fix.js"></script>
function jsCustomLine() {
NBSAppAgent.setBusinessLine('bname','shoppingCar'});
}