在某处用上了chedong提供的基于Google Analytics的点出统计,是通过在链接被触发的时候加上监测outbound,代码如下
document.onclick = function(e) {
e = e || event;
var el = e.target || e.srcElement;
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
e = e || event;
var el = e.target || e.srcElement;
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
测试了一下,发现这样监测不到图片链接。于是加了一行,变成这样。
document.onclick = function(e) {
e = e || event;
var el = e.target || e.srcElement;
if ( el.tagName=='IMG' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
e = e || event;
var el = e.target || e.srcElement;
if ( el.tagName=='IMG' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
但是,除<a><img /></a>以外,很多时候我们的链接还可能是<a><span>text</span></a>,又或<a>text<em>text<em></a>,单判断当前element是IMG的话,还是不够的。
于是再改一下,变成这样
document.onclick = function(e) {
e = e || event;
var el = e.target || e.srcElement;
if ( el.parentNode.tagName=='A' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
e = e || event;
var el = e.target || e.srcElement;
if ( el.parentNode.tagName=='A' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.href.replace("http:\/\/www.chedong.com/", "") +
el.href.replace("http:\/\/", "\/"));
}
}
另有,fisher写到,可用window.location.pathname替代window.location.href.replace("http:\/\/www.chedong.com/", "")。亦即
document.onclick = function(e) {
e = e || event;
var el = e.target || e.srcElement;
if ( el.parentNode.tagName=='A' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.pathname + el.href.replace("http:\/\/", "\/"));
}
}
e = e || event;
var el = e.target || e.srcElement;
if ( el.parentNode.tagName=='A' ) el = el.parentNode
if ( el.tagName=='A' ) {
pageTracker._trackPageview("\/clickto/" + window.location.pathname + el.href.replace("http:\/\/", "\/"));
}
}
Related posts:
- make Google Account Multi-Login script work for Firefox 3.5 升级到firefox 3.5之后,Google Account Multi-Login 就不能切换用户了。去主页看了一下,也没有新版本可以更新。 检查了一下脚本,发现自第83行开始的这几行出了问题 this.parentNode.Email.value = un; ......