JS自定义业务相关接口

JS自定义错误

使用「自定义错误」接口可以采集研发人员「try / catch 异常」和「业务错误」并在基调听云平台「异常分析」→「错误」中进行展示,可以帮助研发人员收集异常和错误。

  • 相关接口
// 您需要将以下代码,保存为tingyun@app-fix.js,添加到项⽬中

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 = {};
 /**
  * 自定义错误:
  * message 长度最大1024字节  
  * metaData的value值支持 number,string,array,map类型,最大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;
}
  • 代码示例
<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自定义事件

自定义事件可以统计 App 中的任意事件,开发者在 SDK 初始化后的任意位置调用「自定义事件接口并设置对应上传参数」。 如:真实用户操作时点击某个功能按钮或触发了某个功能事件等。

  • 相关接口
// 您需要将以下代码,保存为tingyun@app-fix.js,添加到项⽬中
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 采集自定义事件
  * @param name 事件名称,不能为空,长度限制为1024字符,超过截取前1024字符
  * @param properties 事件属性
  */
    var NBSAppAgent = {};
    NBSAppAgent.reportEvent = function(name, properties) {
            if(!name)
                return;
            if(!properties)
                eventProperties = {};
            nbs_callMethod('reportEvent', { 'name': name, 'properties': properties });
    };
    window['NBSAppAgent'] = NBSAppAgent;
}
  • 代码示例
<script src="tingyun@app-fix.js"></script>
function jsCustomEvent() {
  NBSAppAgent.reportEvent("JS_reportEvent_name",{"key":"value"});
}

js自定义业务线

调用接口设置业务线后,可以在平台「异常分析」模块过滤相应业务线名称来分析不同业务线的异常数据。

  • 相关接口
// 您需要将以下代码,保存为tingyun@app-fix.js,添加到项⽬中

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 设置当前业务,相同的key值会覆盖,保留最新值
   * @params businessLine 业务名称,最大长度为256字节
   * @params key  key为固定值,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;
}
  • 代码示例
<script src="tingyun@app-fix.js"></script>
function jsCustomLine() {
  NBSAppAgent.setBusinessLine('bname','shoppingCar'});
}
© 2007-2024 北京基调网络股份有限公司 all right reserved,powered by Gitbook本文档更新于: 2024-03-04 10:18

results matching ""

    No results matching ""