Search code examples
pythongoogle-apigoogle-analytics-apigoogle-api-python-client

GoogleAnalytics API Error startindex and maxresults


I extracting data using Google Analytics Reporting Api V3 with the Google APIs Python client library, I want my results in given index format or all the data in to small chunks.

I am trying to use index and result query it is showing an error

def get_report(analytics, view_id, value): #, index):
    # Use the Analytics Service Object to query the Analytics Reporting API V4.
    return analytics.reports().batchGet(
        body={
            'reportRequests': [
                {
                    'viewId': view_id,
                    # 'pageSize': 5,
                    'startIndex': 5,
                    'maxResults': 15,
                    'dimensions': [{'name': 'ga:sessionDurationBucket'}, {'name': 'ga:eventCategory'},
                                   {'name': 'ga:eventLabel'}, {'name': 'ga:country'}, {'name': 'ga:deviceCategory'}, {'name': 'ga:browser'}],
                    'dateRanges': [{'startDate': 'yesterday', 'endDate': 'yesterday'}],
                    'metrics': [{'expression': 'ga:totalEvents'}],
                    'dimensionFilterClauses': [{"filters": [{"dimensionName": "ga:eventCategory", "operator": "EXACT", "expressions": [value]}]}]

                }]
        }
    ).execute()

Response

<HttpError 400 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Invalid JSON payload received. Unknown name "start_index" at 'report_requests[0]': Cannot find field.
Invalid JSON payload received. Unknown name "max_results" at 'report_requests[0]': Cannot find field.">

Solution

  • 'startIndex': 5,
    'maxResults': 15,

    Are two parameters that were part of the Core Reporting API V3 they are not part of the Reporting API V4 you should be using

    pageToken string A continuation token to get the next page of the results. Adding this to the request will return the rows after the pageToken. The pageToken should be the value returned in the nextPageToken parameter in the response to the reports.batchGet request.

    pageSize number Page size is for paging and specifies the maximum number of returned rows. Page size should be >= 0. A query returns the default of 1,000 rows. The Analytics Core Reporting API returns a maximum of 10,000 rows per request, no matter how many you ask for. It can also return fewer rows than requested, if there aren't as many dimension segments as you expect. For instance, there are fewer than 300 possible values for ga:country, so when segmenting only by country, you can't get more than 300 rows, even if you set pageSize to a higher value.

    Update:

    To implement pagination you should check the response you get from the server will contain a nextPageToken if there are more results.

    nextPageToken string Page token to retrieve the next page of results in the list.

    To get the next set of results you should take your original request and replace the pageToken within that with the nextPageToken you received from your response. If you are sending more then one report make sure to attempt to match the nextPageToken with the correct report in the batch.

    Note: at this time there is no way to label a report within the batch I have a feature request out with the team to add this.