Consecutive GA hits being dropped

2019-04-11 16:24发布

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.

1条回答
叼着烟拽天下
2楼-- · 2019-04-11 16:51

Google will rate limit you on the consecutive events you can send. Here are the rules.

  • You can send up to 10 hits (Events or pageview) in a single burst.
  • After that all hits are silently droped.
  • each 5s you get 1 extra hit. Up to the maximum of 10.

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:

var tokens = 10;

function update_tokens() {
    if (tokens < 10) tokens++;
}

// Even though new tokens should be generated each 5 seconds I give it 10 seconds just to make sure we have tokens available.
setInterval(update_tokens, 10 * 1000);


var hits_to_send = [
    ['_trackPageview', '/page1'],
    ['_trackPageview', '/page2'],
    ['_trackEvent', 'category', 'action', 'label'],
    //...
    ];

// Recursive function to check tokens and send requests.
function send_next() {
    if (hits_to_send.length==0) return;
    if (tokens > 0) {
        tokens--;
        _gaq.push(hits_to_send.shift());
    }
    else {
        setTimeout(send_next, 5 * 1000);
        return;
    }
    send_next();
    return;
}


//When you go online just call:
send_next();

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

查看更多
登录 后发表回答