在API的第3版中,我看到可以传递一个max-results参数来获取1000条以上的记录。我还无法弄清楚如何使用python在API的v4中传递该参数。

我的代码如下所示。我已在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()

最佳答案

您要查找的参数的正确名称是: pageSize Reference Docs提供完整的API规范。

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

注意:无论您要求多少,该API最多每次请求返回 100,000个行。当您尝试max_results时,这表明您正在尝试从Core Reporting API V3进行迁移,请查看Migration Guide - Pagination documentation以了解如何请求下一个10,000行。

堆栈溢出额外提示。在您的问题中包括您的错误回答,因为这可能会增加您有人能够提供帮助的机会。

10-07 19:09
查看更多