微信支付埋点统计结果不符合预期效果----延时惹的祸

    最近在做一个埋点的任务,项目上线一段时间,发现前端的埋点统计数量与服务端的统计数量结果比存在接近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);
                            } 
                  

                })

这是一段用来做支付的代码,当用户支付成功后直接关闭当前页面,不停留在当前详情页面的话,那么这次统计的数据很有可能丢失,原因就是上面标红的那段延时效果,还来不及执行我写的埋点部分,所以导致部分数据丢失。

解决方案:

将上面标蓝的代码提出,放到定时器的外面,就可以尽量避免这种情况的发生,提高数据获取的准确性。

不足:

上面的方案并不是可以完全解决问题,比如网络不好,或者直接杀掉微信进程,都还是有可能导致该埋点的统计丢失。

提供给需要帮助的人,仅与参考!谢谢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值