在API v4中设置最大结果(python) [英] Setting Max Results in API v4 (python)

查看:95
本文介绍了在API v4中设置最大结果(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在API的v3中,我看到有一个可以传递的最大结果参数来获取超过1000条记录。我一直无法弄清楚如何使用python在API的第4版中传递该参数。



我的代码如下所示。我已经评论了我在max_result上的最佳猜测。

  def get_report(analytics):
#使用Analytics Service用于查询Analytics Reporting API V4的对象。
return analytics.reports()。batchGet(
body = {$ b $'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(分析):
#使用Google Analytics服务对象查询Analytics Reporting API V4。
return analytics.reports()。batchGet(
body = {$ b $'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,每个请求的API返回的最大值为 10,000 行。正如您试图 max_results 这告诉我您正尝试从Core Reporting API V3迁移,请查看迁移指南 - 分页文档了解如何请求下一个10,000行。



<堆栈溢出额外提示。在你的问题中加入你的错误回答,因为它可能会提高你的帮助能力。


In v3 of the API I'm seeing that there was a max-results parameter that could be passed to get more than 1000 records. I haven't been able to figure out how to pass that parameter in v4 of the API using python.

My code looks something like below. I've commented out my best guess at 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()

解决方案

The correct name of the parameter you are looking for is: pageSize. The Reference Docs provide the full API specifications.

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

Note: the API returns a maximum of 10,000 rows per request, no matter how many you ask for. As you attempted max_results this tells me you are trying to migrate from the Core Reporting API V3, check out the Migration Guide - Pagination documentation to understand how to request the next 10,000 rows.

Stack Overflow extra tip. Include your error responses in your question, as it will likely improve your chances of someone being able to help.

这篇关于在API v4中设置最大结果(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆