用户操作路径收集
用户的操作路径往往伴随着App及Page生命周期的执行,所以,我们是通过劫持二者的生命周期来进行记录。原理代码整合如下:
js
const originApp = App;
const self = this;
App = function(app){
["onLaunch","onShow","onHide","onError"].forEach(methodName=>{
//保存用户自定义方法
const userDefinedMethod = app[methodName];
//App Launch 获取基础信息
if(methodName === "onLaunch"){
self.getNetworkType();
self.config.setLocation && self.getLocation();
self.config.setSystemInfo&&self.getSystemInfo();
}
app[methodName] = function(options){
//用户操作记录埋点
const breadcrumb = {
type:"function",
time: utils.now(),
belong:"App",
method:methodName,
path:options&&options.path,//页面路径
query: options&&options.query,//页面参数
scene:options&&options.scene //场景编号
};
self.pushToBreadcrumb(breadcrumb);
//错误上报
methodName === "onError"&&self.error({msg:options});
//执行用户自定义方法
return (userDefinedMethod&&userDefinedMethod.call(this,options));
}
});
return originApp(app);
}