我希望使用Google Analytic和Local Storage跟踪离线活动.这是我的代码:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-27966345-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setSessionCookieTimeout',10]);
_gaq.push(['_setSampleRate', '400']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
Run Code Online (Sandbox Code Playgroud)
我只是将事件保存在本地存储中,当用户重新上线时,我尝试将事件发送给Google,但是当我将我的计数器与我在实时模式(Google Analytic)中看到的页面视图进行比较时,我无法理解为什么他们不同.我认为是谷歌采样率或其他因为我测试了很多次,我看到不同的结果,有时结果是正确的,但有时有1000或更多的差异.
这是我发送事件的代码:
while(ctr>0){
if(sd==0){
sd=1;
alert(ctr);
}
//
if(flag==0)break;
var name='tosend';
var tosend_action=localStorage.getItem(name+'action'+ctr);
var tosend_label=localStorage.getItem(name+'label'+ctr);
var tosend_value=localStorage.getItem(name+'value'+ctr);
_gaq.push(['_trackEvent',value,tosend_action,tosend_label+"_val:"+tosend_value,tosend_value]);
_gaq.push(['_trackPageview',name+'value'+ctr]);
localStorage.removeItem(name+'action'+ctr);
localStorage.removeItem(name+'label'+ctr);
localStorage.removeItem(name+'value'+ctr);
ctr=Number(ctr)-1;
localStorage.removeItem('counter');
localStorage.setItem('counter',ctr);
ctr=localStorage.getItem('counter');
}
}
Run Code Online (Sandbox Code Playgroud)
ps:flag是我的变量,用于查看用户是否在线.