gpt4 book ai didi

python - GoogleAnalytics API 错误 startindex 和 maxresults

转载 作者:太空宇宙 更新时间:2023-11-04 02:55:10 32 4
gpt4 key购买 nike

我使用 Google Analytics Reporting Api V3 提取数据与 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 的两个参数它们不属于 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 与批处理中的正确报告相匹配。

注意:目前无法在批处理中标记报告,我向团队提出了添加此功能的请求。

关于python - GoogleAnalytics API 错误 startindex 和 maxresults,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42622052/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com