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.
Google will rate limit you on the consecutive events you can send. Here are the rules.
It's like a Token Bucket Algorithm where the maximum tokens are 10 and the refresh rate is 1 new token every 5 seconds.
Now
_setSampleRate
and_setSessionCookietimeout
won't help you here, you should remove these parameters from your tracking code. The best thing you can do is to throttle you're requests, implementing the same algo on your end. Here's an example on how you could do that:
This should give you better numbers even though some metrics won't look nice. timeOnSite and timeOnPage for example. A new visit may be created if the user has been offline for more than 30 min even though he was interacting with the system.
Also notice that if you have too many events in there it can take a while to update all of them. I'd recommend you to keep your events to a reasonable amount. Try to track only things that are important for you future analysis.
GA Hit Limit Reference