最近在做一个埋点的任务,项目上线一段时间,发现前端的埋点统计数量与服务端的统计数量结果比存在接近1:2的惨烈景状。
为了查找问题原因,现在贴一点代码出来:
http.pay(postData,function(response) {
let res = {
data: response,
time: new Date().getTime()
};
if(response.code==10000){
let charge = response.data;
//console.log(pingpp);
pingpp.createPayment(charge, function(result, err){
console.log(result);
console.log(err.msg);
console.log(err.extra);
if (result == "success") {
//埋点 =====修复后的代码位置
try{
if(type == 'k'){
let goodsName = _this.data.goodsId ? _this.data.goodsName : _this.data.name;//课程名称
SC_TA.ta("payCourseOrder_web",{
course_id:goodsId.toString(),
course_title:goodsName
});
}
}catch(e){
console.log(e);
}
setTimeout(function(){
if(type == 'k'){
_this.isLoading = false;
_this.data.isBuy = true;
// 埋点======修复前的代码位置
try{
let goodsName = _this.data.goodsId ? _this.data.goodsName : _this.data.name;//课程名称
SC_TA.ta("py",{
course_id:goodsId.toString(),
course_title:goodsName
});
}catch(e){
console.log(e);
}
}else{
},800);
}
})
这是一段用来做支付的代码,当用户支付成功后直接关闭当前页面,不停留在当前详情页面的话,那么这次统计的数据很有可能丢失,原因就是上面标红的那段延时效果,还来不及执行我写的埋点部分,所以导致部分数据丢失。
解决方案:
将上面标蓝的代码提出,放到定时器的外面,就可以尽量避免这种情况的发生,提高数据获取的准确性。
不足:
上面的方案并不是可以完全解决问题,比如网络不好,或者直接杀掉微信进程,都还是有可能导致该埋点的统计丢失。
提供给需要帮助的人,仅与参考!谢谢。