google analytics -在API v4 (python)中设置最大结果



在API的v3中,我看到可以传递一个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。参考文档提供了完整的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迁移,请查看迁移指南-分页文档,以了解如何请求下一个10,000行。

Stack Overflow额外提示。在你的问题中包括你的错误回答,因为这可能会增加别人能够帮助你的机会。

最新更新