Skip to main content

JS Custom Business

JS Custom Error

Using the "custom error" API, you can collect developers' "try / catch exceptions" and "business errors" and display them in the Tingyun platform's "Exception Analysis → Errors" section, helping developers collect exceptions and errors.

  • Related API
// 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 count any events in the App. Developers can call the "custom event API" at any location after SDK initialization and set corresponding upload parameters. For example: when real users click a function button or trigger a function event during operation, etc.

  • Related API
// 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 event
* @param name Event name, cannot be empty, max length 1024 characters, truncated if exceeded
* @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 API to set the business line, you can filter the corresponding business line name in the platform's "Exception Analysis" module to analyze exception data for different business lines.

  • Related API
// 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 current business, same key value will override, keep the latest value
* @params businessLine Business name, max length 256 bytes
* @params key key is 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');
}