JS Custom Business Related Interfaces
JS Custom Error
The 'Custom Error' interface can be used to collect developers' 'try / catch exceptions' and 'business errors' and display them in the TingYun platform 'Exception Analysis' → 'Error' for helping developers collect exceptions and errors.
- Related Interface
//You need to save the following code as tingyun@app-fix.js and add it to the 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 maximum length of 1024 bytes
* metaData value supports Number, String, Array, Map types, 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 SDK initialization. For example: when a real user clicks on a certain feature button or triggers a certain feature event, etc.
- Related Interface
//You need to save the following code as tingyun@app-fix.js and add it to the 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 events
* @param name The name of the event, cannot be empty, limited to 1024 characters, exceeding will take the first 1024 characters
* @param properties The properties of the event
*/
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 '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 the 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 the current business line, the same key value will be overwritten, and the latest value will be retained
* @params businessLine The name of the business, maximum length of 256 bytes
* @params key For internal business use by Tingyun, please consult the Tingyun platform for the value
*/
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'});
}