埋点

下面是我在项目中遇到 埋点需求的时候,用来测试埋点的代码,需要看过来

import {tracking} from 'env'; export const fTracking = { param: { uid: '', page_token: new Date().getTime() + Math.floor(Math.random()*100), from: 'WEB', ac_val: approach.getQueryString('md') ? approach.getQueryString('md'): '', //页面md ac_referer: window.location.pathname, //页面来源页, ac: 'view', // 监听当前页面 actionOwner: '' // 监听 触发事件的对象 }, init: function (param) { let that = this; if(this.param.ac_referer != '/') { this.param.ac_val = approach.getQueryString('id')? approach.getQueryString('id'): ''; } $('body').append($('<div id="trackDiv"></div>')); this.param.uid = param; let viewTimer = setInterval(function() { that.send(); }, 30000); }, send: function (action, actionOwner) { if(action) {//如果当前页面的view 就是跳转过来的页面为referer this.param.ac = action; // this.param.ac_referer = window.location.pathname; } else { this.param.ac = 'view'; if(window.location.href.indexOf('5paH56ug')!= -1) { this.param.ac_val = approach.getQueryString('5paH56ug'); } } if(actionOwner) { this.param.actionOwner = actionOwner; } else { this.param.actionOwner = document.referrer; } let i; let q = ''; for(i in this.param){ if(q == ''){ q = i + "=" + this.param[i]; }else{ q = q + "&" + i + "=" + this.param[i]; } } let id = 'cc.track' ; let src = tracking+'?' + q; this._sendImage(id,src); }, run: function(){ this._bindEvent(); }, _sendImage : function(id, src){ if (window.localStorage) { localStorage.setItem(id,src); } let img = new Image(); img.src=src; img.id = id; img.style.width = 0; img.style.height = 0; document.getElementById("trackDiv").appendChild(img); let timer = setInterval(function() { if (img.complete) { document.getElementById("trackDiv").removeChild(img); clearInterval(timer); } if (window.localStorage) { localStorage.removeItem(id); } }, 50); }, _bindEvent: function(){ let that = this; $("*[tccAction]").bind("click",function(e){ let action = $(e.currentTarget).attr("tccAction"); let actionOwner = $(e.currentTarget).attr("actionOwner"); that.send(action,actionOwner); }); $("*[tccAction]").blur(function(e){ let action = $(e.currentTarget).attr("tccAction"); let actionOwner = $(e.currentTarget).attr("actionOwner"); that.send(action,actionOwner); }); }, }
原文地址:https://www.cnblogs.com/lisiyang/p/8548228.html