Skip to main content

JS Custom Business

JS Custom Error

Use the "custom error" interface to collect "try/catch exceptions" and "business errors" and display them in the "Error" section under "Exception Analysis" on the Tingyun platform. This helps developers 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:
* message max length 1024 bytes
* metaData value supports Number, String, Array, Map types, max 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 be used to track any event in the app. Developers can call the "custom event" interface and set the corresponding upload parameters anywhere after SDK initialization. For example: when a user clicks a function button or triggers a function event during real user operation.

  • 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);
}
}

/**
* Collect custom event
* @param name Event name, cannot be empty, max length 1024 characters, excess will be truncated
* @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 "Exception Analysis" module on the platform to analyze exception data for 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);
}
}

/**
* Set current business line. The same key will overwrite previous values, keeping the latest value
* @params businessLine Business line name, max length 256 bytes
* @params key For Tingyun internal business use, please consult the Tingyun platform for values
*/
var NBSAppAgent = {};
NBSAppAgent.setBusinessLine = function(key, businessLine) {
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');
}