Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Max Results in API v4 (python)

In v3 of the API I'm seeing that there was a max-results parameter that could be passed to get more than 1000 records. I haven't been able to figure out how to pass that parameter in v4 of the API using python.

My code looks something like below. I've commented out my best guess at max_result.

def get_report(analytics):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          #'max_results': 100000,
          'dateRanges': [{'startDate': '2016-04-01', 'endDate': '2016-08-09'}],
          'dimensions': [{'name':'ga:date'},
                    {'name': 'ga:channelGrouping'}],
          'metrics': [{'expression': 'ga:sessions'},
                 {'expression': 'ga:newUsers'},
                 {'expression': 'ga:goal15Completions'},
                 {'expression': 'ga:goal9Completions'},
                 {'expression': 'ga:goal10Completions'}]
        }]
      }
  ).execute()
like image 223
FortuneFaded Avatar asked Aug 12 '16 15:08

FortuneFaded


1 Answers

The correct name of the parameter you are looking for is: pageSize. The Reference Docs provide the full API specifications.

def get_report(analytics):
  # Use the Analytics Service Object to query the Analytics Reporting API V4.
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'pageSize': 10000,
          'dateRanges': [{'startDate': '2016-04-01', 'endDate': '2016-08-09'}],
          'dimensions': [{'name':'ga:date'},
                    {'name': 'ga:channelGrouping'}],
          'metrics': [{'expression': 'ga:sessions'},
                 {'expression': 'ga:newUsers'},
                 {'expression': 'ga:goal15Completions'},
                 {'expression': 'ga:goal9Completions'},
                 {'expression': 'ga:goal10Completions'}]
        }]
      }
  ).execute()

Note: the API returns a maximum of 100,000 rows per request, no matter how many you ask for. As you attempted max_results this tells me you are trying to migrate from the Core Reporting API V3, check out the Migration Guide - Pagination documentation to understand how to request the next 10,000 rows.

Stack Overflow extra tip. Include your error responses in your question, as it will likely improve your chances of someone being able to help.

like image 53
Matt Avatar answered Oct 17 '22 22:10

Matt