python - GoogleAnalytics API 错误起始索引和最大结果

标签 python google-api google-analytics-api google-api-python-client

我使用 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 错误起始索引和最大结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42622052/

相关文章:

python - 有没有办法阻止 JOIN 将新数据集与旧数据集连接起来?

python - 获取 Flask JSON 响应作为 HTML 表?

javascript - Chrome 扩展 gmail API cookiePolicy?

php - Guzzle 错误计数() : Parameter must be an array or an object that implements Countable in

javascript - 如何禁用谷歌分析调试js?

python - Flask 发送流作为响应

python - 在 Django 中上传文件时出现 OSError

google-api - 尽管访问公共(public)文件夹,Google 云端硬盘仍需要身份验证

security - 将 Google API 凭据存储在数据库中。安全吗?

google-analytics-api - 如何将谷歌分析集成到 Titanium 移动应用程序?