試行錯誤日記になってきた。こんな記事のコードをコピペする人はいないと思うけど、、書き直しが多い。
前の記事で、GAのcookieのutmvの値に追加的に流入元の値を入れる記事を書いたけど、いろいろ訂正する。
まず、下に貼り付けたコードは、まずかった。pageTracker._setVar(“hogehoge”)を送れば、同時にpageviewも送られると思って、setVarの時には、trackPageviewを送らないようにしてたけど、送らないといけない。
今日データを見たら、セッション数 > ページビュー という結果になっていた。
_setVarが送られているので、sessionはカウントされているのだけど、trackPageviewがないので、ページビューがカウントされてない結果になった。、、、と思う。
もう一つ、ユーザ定義にセットした結果で、とりあえず新規のreferrer を突っ込んだけど、データを整理した形で格納したくなった。
訂正版だけど、まだまだ問題がありそう。googleとyahooの検索の時は、referrerのままでなく、キーワードを設定するようにした。(あと、exeをexecXとdreamhostの文字列の禁止制限に掛からないようにした。)
timerID1 = setInterval(function(){
if(_gat){
init_ga();
clearInterval(timerID1);
timerID1 = null;
}
},500);
timerID2 = setInterval(function(){
if(document && jQuery && _gat){
get_outbouund_link();
clearInterval(timerID2);
timerID2 = null;
}
},500);
//referrerや、profileページを見た場合には、superSetVarする。yahoo,googleからの来訪はキーワード
function init_ga(){
window.pageTracker = _gat._getTracker("UA-188512-12");
var d = new Date();
d = d.getFullYear().toString()+d.getMonth().toString()+d.getDate().toString();
var f = function(){
var c = 1;
return function(){
pageTracker._trackEvent("time", c.toString(), document.location.href,2);
c++;
};
}();
setInterval(f, 10000);
if(document.location.pathname == "/profile"){
superSetVar("path:seen_profile;");
}
if(!!document.referrer && !document.referrer.match(/^https?:\/\/wp\.slash\-reader\.com/)){
var value;
var r;
try{
if(r = document.referrer.match(/[&?]q\=([^&$]+)/)){
value = "key:" + r[1].replace(/\+/g, "|") + ";";
}else if(r = document.referrer.match(/[&?]p\=([^&$]+)/)){
value = "key:" + r[1].replace(/\+/g, "|") + ";";
}else{
r = document.referrer.match(/https?\:\/\/([^/$]*)/);
value = "ref:" + r[1] + ";" ;
}
}catch(e){
value = "error:" + e.toString() + ";" ;
}
superSetVar(value);
}else{
pageTracker._trackPageview();
}
}
//外部リンクへのイベントに、trackPageviewイベントをhookする
function get_outbouund_link(){
try{
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++){
jQuery(ret[i]).bind("click",
function(){
pageTracker._trackPageview("/virtual/" + this.href);
pageTracker._trackEvent("eternal_link", "getout", this.href, 2);
}
);
}
return true;
}catch(err){
//console.log(err);
return false;
}
}
// I(abc-analytics.com) copied from http://www.lunametrics.com/blog/2008/04/17/stuff-more-than-one-value-\
in-gas-user-defined-segment/
// "standard" read cookie function (copied from Prusak's gwo_write.js)
function read_cookie(cookie_name) {
var my_cookie=""+document.cookie;
var ind=my_cookie.indexOf(cookie_name);
if (ind==-1 || cookie_name=="") return "";
var ind1=my_cookie.indexOf(';',ind);
if (ind1==-1) ind1=my_cookie.length;
return unescape(my_cookie.substring(ind+cookie_name.length+1,ind1));
}
function superSetVar(appendValue) {
var getVar = read_cookie('__utmv'); // read the __utmv cookie
hasValue = getVar.indexOf(appendValue); // does the cookie already have the value we want to append?
if ( hasValue == -1 ) { // if the new value is not already in the cookie
removePrefix = /^.*\.(.*)/.execX(getVar); // __utmv cookie has the format 12345678.cookieValue - remove\
the ## prefix
if (removePrefix && removePrefix[1]) {
newVar = removePrefix[1] + appendValue; // append the value
}else{
newVar = appendValue; // this will execute if the cookie was not already set.
}
//var superSetVarTracker = _gat._getTracker("UA-1-1"); // set up a tracker to call _setVar from
//superSetVarTracker._initData();
pageTracker._setVar(newVar); // call _setVar with the new value
pageTracker._trackPageview();
}else{
pageTracker._trackPageview();
}
}
function unSetVar(removeValue){
var getVar = read_cookie('__utmv'); // read the __utmv cookie
hasValue = getVar.indexOf(removeValue); // does the cookie have the value we want to remove?
if ( hasValue != -1 ) { // if the value is in the cookie then . . . otherwise, there\
is nothing further to do.
removePrefix = /^.*\.(.*)/.execX(getVar); // __utmv cookie has the format 12345678.cookieValue - remove\
the ## prefix
if (removePrefix && removePrefix[1]) { // if we remove the prefix then. . .
//
var re = removeValue + '[^/]*';
re = new RegExp(re,'g');
newVar = removePrefix[1].replace(re,""); // newVar = removePrefix[1] MINUS removeValue
//
//var superSetVarTracker = _gat._getTracker("UA-1-1"); // set up a tracker to call _setVar from
// superSetVarTracker._initData();
pageTracker._setVar(newVar); // call _setVar with the new value
pageTracker._trackPageview();
}
}
}
こちらもあわせてどうぞ
Pingback: Google AnalyticsのCookie &パラメーター 一覧 | カグア!