GoogleAnalytics API startindex 和 maxresults 错误

GoogleAnalytics API Error startindex and maxresults

我使用 Google Analytics Reporting Api V3 with the Google APIs Python client library 提取数据,我希望我的结果采用给定的索引格式或将所有数据分成小块。

我正在尝试使用索引和结果查询,但显示错误

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()

回应

<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.">

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

您应该使用

Core Reporting API V3 they are not part of the Reporting API V4 的两个参数

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.

更新:

要实现分页,您应该检查 response 如果有更多结果,您从服务器获得的将包含 nextPageToken。

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

要获得下一组结果,您应该接受原始请求并将其中的 pageToken 替换为您从响应中收到的 nextPageToken。如果您要发送多于一个报告,请确保尝试将 nextPageToken 与批次中的正确报告相匹配。

注意:目前无法在批次中标记报告我有一个功能请求与团队一起添加这个。