连续的GA命中被丢弃 [英] Consecutive GA hits being dropped

查看:89
本文介绍了连续的GA命中被丢弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Google分析和本地存储来跟踪离线事件。
这是我的代码:

  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 =文档.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);
})();

我只是将事件保存在本地存储中,当用户重新联机时,我尝试将事件发送给Google在一个for,但是当我将我的柜台与我在实时模式(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');
}

}

ps:flag是我的变量以查看用户是否在线。

解决方案

Google会限制您可以发送的连续活动。以下是规则。


  • 您可以在一次突发中发送最多10次点击(事件或综合浏览量)。

  • 之后,所有命中都会默默地放弃。

  • 每5秒你会得到1次额外命中。最多10个。


这就像一个令牌桶算法,其中最大令牌为10,刷新率为每5秒1个新令牌。



现在 _setSampleRate _setSessionCookietimeout 在这里没有帮助,您应该从跟踪代码中移除这些参数。你可以做的最好的事情是扼杀你的请求,在你的最后实现相同的算法。这里有一个关于如何做到这一点的例子:

  var tokens = 10; 

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

//即使每5秒钟产生一个新的令牌,为了确保我们有可用的令牌,我给它10秒钟。
setInterval(update_tokens,10 * 1000);


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

//递归函数检查令牌和发送请求。
函数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;
}


//当你上网时,只需调用:
send_next();



这应该会让你更好尽管一些指标看起来不太好,例如,timeOnSite和timeOnPage。如果用户离线超过30分钟,即使他正在与系统交互,也可能创建新的访问。



另外请注意,如果您的活动太多,可能需要一段时间才能更新所有活动。我建议你保持你的事件合理的数额。尝试仅跟踪对您未来分析很重要的事情。



GA命中限制参考


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.

  • 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

这篇关于连续的GA命中被丢弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆