google analytics 外部へのリンクを計測する

  • 2009年10月9日修正
    jQueryを使っている。$ではなく、jQueryとするべきだった。
  • 2009年10月7日修正
    一番下のコードは、外部リンクを検出する関数は、検出後に呼ぶべきだった、、
  • 2010年8月6日追加
    外部リンクの計測についてはこちらを。基本的に2009年に書いたブログは分かってないまま書いてます。今でも、推測で書きがちなんだけど、、、

実務では、使えないんだろうけど、このサイトで動くまでを書く。実際は、clickではなく、mouseoverで試したのだが。

これも、定番の作業のようだけど、

wordpressで、外部へのリンクを計測する。

functions.phpで、

wp_enqueue_script(‘mine’, get_settings(“site_url”).”/wp-includes/js/mine/test.js”, array(‘jquery’));

としておいて、jsファイルをincludeする。

timerIDの方法は、http://d.hatena.ne.jp/kminoru/20080305/1204703820 を参考にさせて頂いた。

で、css-selectorで、エントリ内のaタグを集めておいて、その中で外部へのリンクになっているものに選別する。

それに、eventListenerを追加しておく。ただ、get_outbound_linkが、どっかでこけるた際の処理が、よく練れてない。

function get_outbouund_link(){
try{
as = $(“div.entry-content a”);
as=jQuery(“div.entry-content a”); var ret = [];
for(var i=0,L=as.length;i<L;i++){
if(!as[i].href.match(document.location.host)){
ret.push(as[i]);
}
}
for(var i=0,L=ret.length;i<L;i++){
//console.log(ret[i]);
$(ret[i]).bind(“click”,jQuery(ret[i].bind(“click”,
function(){pageTracker._trackPagevie(“/virtual/”+this.href);}
);
}
return true;
}catch(err){
return false;
}
}
timerID = setInterval(function(){
//console.log(“once”);
var ret = get_outbouund_link();
//console.log(“once_end”);
if(!!document && $){
//console.log(“start”);
var ret = get_outbouund_link();
clearInterval(timerID);
//console.log(“end”);
timerID = null;
}
},500);

This entry was posted in ANALYTICS and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <img localsrc="" alt="">