I want track offline event using Google Analytic and Local Storage. this is my code:
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);
})();
I simply save the events in local storage and when user get back online I try to send events to Google in a for but when I compare my counter with page-views that I see in Real Time mode(Google Analytic) I can't understand why they are different. I think is about Google sample rate or something because I test it many times and I see different results, sometimes the results is correct but sometimes have 1000 or more difference.
Here is my code for sending events:
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');
}
}
p.s: flag is the my variable to see if user is online or not.