Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: Quota Error: User Rate Limit Exceeded

I have a Apps Script where I Loop trough my Google Analytics Account till I get to a certain Profile.
if (profileItems[i].getName() == "Dev Test" )
Than in another function (see bellow) I make the call to the Google Analytics API and always get the Exception thrown: Exception: Quota Error: User Rate Limit Exceeded

function getReportDataForProfile(profileId) {
var tableId = 'ga:' + profileId;
var startDate = getLastNdays(14);   // 2 weeks (a fortnight) ago.
var endDate = getLastNdays(0);      // Today.

var optArgs = {
  'dimensions': 'ga:keyword',              // Comma separated list of dimensions.
  'sort': '-ga:visits,ga:keyword',         // Sort by visits descending, then keyword.
  'segment': 'dynamic::ga:isMobile==Yes',  // Process only mobile traffic.
  'filters': 'ga:source==google',          // Display only google traffic.
  'start-index': '1',
  'max-results': '250'                     // Display the first 250 results.
};

//
//Exponential Backoff
//
for (var n=0; n<6; n++) {
try {
     var results = Analytics.Data.Ga.get(
     tableId,                  // Table id (format ga:xxxxxx).
     startDate,                // Start-date (format yyyy-MM-dd).
     endDate,                  // End-date (format yyyy-MM-dd).
     'ga:visits,ga:pageviews', // Comma seperated list of metrics.
     optArgs);
 } catch(e) {
   if (n == 5) {
     //var results = e;
     //throw new Error('Quota ERROR');
     throw (e) 
   } 
   Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
  }    
}   
 return results;
}   
  • I increased the number of the Per user Limit in the API Console to 10.0
  • The used Porcentage is 6%
  • And I applied the Exponential Backoff
  • I also believe that by just asking for the Visits and PageViews I am not exagerating my number of calls.

The full code

Any ideias what is going on?

like image 528
Jacobvdb Avatar asked Feb 11 '13 20:02

Jacobvdb


People also ask

How do you fix user rate limit exceeded?

Resolve a 403 error: User rate limit exceeded To fix this error, try any of the following: Raise the per-user quota in the Google Cloud project. For more information, request a quota increase.

What is the meaning of user rate limit exceeded?

User Rate Limit Exceeded. Means that you are hitting a user rate quota. User rate quotas are flood protection they ensure that a single user of your application can not make to many requests at once. These quotas can not be extended.

How do I stop the rate limit exceeded on Twitter?

try not to over use the refresh button - this will cost you 3 calls per click (All Tweets, Replies & DMs) UPDATE: try lowering the total % in the settings window, twitter API tab to around 60-70% - you'll get less frequent updates but you'll use less API.


3 Answers

That's indeed because you are not allowed to query more than 10 requests per seconds. I'm posting a solution here (even if that's an old question) because it may help others:

/**
 * CallLimiter
 * @param {Number} numberOfCalls how many calls per duration are we allowed to do
 * @param {Number} duration      duration in ms
 */
function CallLimiter(numberOfCalls, duration){
  var lastCall = 0;
  var tokens   = 0;

  return function limiter(f){
    var now = +new Date();

    if (now - lastCall > duration) {
      tokens = numberOfCalls;
    }

    if (tokens <= 0) {
      tokens = numberOfCalls;
      // wait for `duration` before calling limiter(f) again
      return setTimeout(limiter.bind(this, f), duration);
    }

    tokens--;
    lastCall = now;
    f();
  };
}

And here is how to use it:

// Stub...
var Analytics = {Data: {Ga:{get:function(cb){
        cb();
}}}};

function app(){
  // Wrap the api call inside a limiter
  limiter(function(){

    Analytics.Data.Ga.get(function(err, data){
      // do something with the data...

      // ... then, loop if necessary
      app();
    });

  });
}

// Start
app();

And here is a blocking Scala version:

class CallLimiter(rate: Throttler.Rate) {
  var lastCall       = 0
  var duration       = rate.duration.toMillis
  var current_tokens = 0

  def apply[A](f: () => A): A = {
    val d = new Date().getTime

    if (d - lastCall > duration) {
      current_tokens = rate.numberOfCalls
    }
    if (current_tokens <= 0) {
      // wait for `duration` before calling f()
      Thread.sleep(duration)
      current_tokens = rate.numberOfCalls
    }
    current_tokens = current_tokens - 1
    lastCall = d
    f()
  }
}

Usage:

import akka.contrib.throttle.Throttler

val limiter = new CallLimiter(new Rate(10, Duration(1, TimeUnit.SECONDS)))
val accounts = limiter(() => analytics.management().accounts().list().execute().getItems)
like image 104
FGRibreau Avatar answered Sep 30 '22 21:09

FGRibreau


Maybe you are exceeding the max request per profile per second. That looks like it's set to 10 (https://developers.google.com/analytics/devguides/reporting/core/v3/limits-quotas)

like image 24
Viktor Avatar answered Oct 01 '22 21:10

Viktor


You can update the "per-user rate limit" yourself in the Developers Console, in the Quotas section.

This limit is meant to protect you from accidental usage.

You can learn more here: https://developers.google.com/console/help/new/#cappingusage

like image 43
Nicolas Adrian Avatar answered Sep 30 '22 21:09

Nicolas Adrian