Search code examples
google-analyticsgoogle-analytics-api

How to construct google analytics query to avoid quota limits?


This is one part of my request. And I have 725 those kind of requests for each day in 2 year span.

I am getting analytics for 30 days traffic for certain dataset I am creating.

When I try to query the analytics for all 725 datasets I get quota error Requests per user per 100 seconds even though I put time.pause(2) before each request.

Is there something else I can do to avoid hitting the API quota?

{
   "reportRequests":[
      {
         "viewId":"104649158",
         "dateRanges":[
            {
               "startDate":"2017-12-01",
               "endDate":"2017-12-31"
            }
         ],
         "metrics":[
            {
               "expression":"ga:pageviews"
            },
            {
               "expression":"ga:uniquePageviews"
            },
            {
               "expression":"ga:pageviewsPerSession"
            },
            {
               "expression":"ga:timeOnPage"
            },
            {
               "expression":"ga:avgTimeOnPage"
            },
            {
               "expression":"ga:entrances"
            },
            {
               "expression":"ga:entranceRate"
            },
            {
               "expression":"ga:exitRate"
            },
            {
               "expression":"ga:exits"
            }
         ],
         "dimensions":[
            {
               "name":"ga:pagePathLevel2"
            }
         ],
         "dimensionFilterClauses":[
            {
               "filters":[
                  {
                     "dimensionName":"ga:pagePathLevel2",
                     "operator":"REGEXP",
                     "expressions":[
                        "23708|23707|23706|23705|23704|23703|23702|23701|23700|23699|23698|23697|23696|23695|23694|23693|23692"
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

Solution

  • 1) You should increase the user quota to 1000 requests (if not done already) by going into your Coogle Cloud Console -> Top-left Menu -> APIs & Services -> Analytics Reporting API -> Quota:

    https://console.cloud.google.com/apis/api/analyticsreporting.googleapis.com/quotas

    enter image description here

    enter image description here

    2) You could increase the time range and use the ga:yearMonth dimension to still get your monthly breakdown. However you might face sampling issues: since your query is "custom" (you use a filter + dimension), sampling will apply if for the given time range the total number of sessions at property level exceeds 500K (regardless of how many are actually included in the response). In this case there is no absolute answer, you have to find the time ranges that suit you best. samplesReadCounts / samplingSpaceSizes will help you detect sampling, and if required you will need to handle pagination.