Costantin
Costantin

Reputation: 2656

add quotaUser to Google Analytics API requests

I'm fetching data using the Google Analytics reporting API v4. I do this via python using cron jobs, in order to not over-run my quota and "pass" it to my users I need to setup a quotaUser parmeter according to this and this (2nd link is for v3)..

Currently I make my calls like this:

    s = analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': start_date, 'endDate': end_date}],
          'metrics': [
                {'expression': 'ga:sessions'},
            ],
          'dimensions': [{'name': 'ga:date'}],
        }]
      }
  ).execute()

I'm not sure where I should add the quotaUser and can I pass any id to it? Could I pass for example the same VIEW_ID?? Is that discouraged for any reason?

Thanks

Upvotes: 3

Views: 1256

Answers (1)

Andersson
Andersson

Reputation: 146

Matt is right, you add it as parameter to the batchGet call:

analytics.reports().batchGet(body={....}, quotaUser="somestring").execute()

The library will check if the named parameter is part of the accepted query parameters (e.g. prettyPrint, quotaUser, userip) and add it the the query string if it is.

Upvotes: 2

Related Questions