Created
September 23, 2013 04:20
-
-
Save ktmud/6666437 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 统计埋点代码 | |
* @author 丘迟 <[email protected]> | |
*/ | |
(function(S, win, doc, undefined) { | |
var E = S.Event; | |
//默认的前缀 | |
//可以通过在后面的脚本修改全局变量来动态修改 | |
win.STAT_PREFIX = 'http://www.atpanel.com/search?'; | |
function send(stat, noTimstamp) { | |
var t = +new Date(), i = win[t] = new Image(); | |
i.onload = i.onerror = function(){ win[t]=null; }; | |
i.src = (/^http/.test(stat) ? stat : win.STAT_PREFIX + stat) + (noTimstamp ? '' : ('&t=' + t)); | |
i = null; | |
} | |
function trace(target) { | |
var stat, c = 0; | |
//向上回溯至多四个层级即可 | |
while (!stat && target && target.getAttribute && c < 4) { | |
c++; | |
stat = target.getAttribute('stat') || target.getAttribute('data-stat'); | |
target = target.parentNode; | |
} | |
if (stat) send(stat); | |
} | |
E.on(doc, 'click', function(e) { | |
trace(e.target); | |
}); | |
//IE7以上支持(鼠标中键)或者(Ctr+单击)在后台标签页打开链接 | |
//但这种情况并不会触发click事件 | |
//需要特殊处理(如果以后IE10解决了这个bug,需要修改此处代码) | |
if (S.UA.ie > 6) { | |
E.on(doc, 'keyup mouseup', function(e) { | |
var which = e.which; | |
if (which == 2 || (e.ctrlKey && which == 1)) trace(e.target); | |
}); | |
} | |
//PV埋点 | |
if (win.STAT_PV) { | |
send(win.STAT_PV, true); | |
} | |
//提供发送埋点请求的公用方法 | |
//考虑通用性,并不放在Search下 | |
S.StatSend = send; | |
})(KISSY, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment