Google Analytics Reporting API v4排序结果

时间:2017-11-09 14:01:52

标签: python sorting google-analytics google-analytics-v4

基于此处的示例:https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/service-py

我正在尝试在公共图书馆目录中查找搜索结果信息。我可以通过填写适当的指标和维度来获得结果,但我无法弄清楚如何将“排序”整合到

def get_report(analytics):
  """Queries the Analytics Reporting API V4.
  Args:
    analytics: An authorized Analytics Reporting API V4 service object.
  Returns:
    The Analytics Reporting API V4 response.
  """
  return analytics.reports().batchGet(
      body={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '1daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:avgSearchResultViews'}],
          'dimensions': [{'name': 'ga:searchKeyword'}],
        }]          

      }
  ).execute()

有谁知道如何在上面的代码块中集成排序和可能的maxResults限制器? Google有关于排序的信息:https://developers.google.com/analytics/devguides/reporting/core/v3/reference#sort。我尝试在指标表达式中添加“ - ”,甚至为“排序”添加了一个全新的行,但它还没有帮助我。

2 个答案:

答案 0 :(得分:1)

示例:

{
  "reportRequests":[
  {
    ...
    "orderBys": [
    {
      "fieldName": "ga:users",
      "sortOrder": "DESCENDING"
    },{
      "fieldName": "ga:source"
    }],
  }]
}

来源:GA API migration

答案 1 :(得分:0)

这就是方法。 Zeloslaw 的答案没有“orderType”,但您必须使用它。

{
    "reportRequests": [
      {
        "viewId": "<your_view_id>",
        "dateRanges": [
          {
            "startDate": "30daysAgo",
            "endDate": "yesterday"
          }
        ],
        "metrics": [
            {
              "formattingType": "METRIC_TYPE_UNSPECIFIED",
              "expression": "ga:searchUniques"
            }
          ],
          "dimensions": [
            {
              "name": "ga:searchKeyword"
            }
          ],
          "orderBys": [
            {
              "orderType": "VALUE",
              "sortOrder": "DESCENDING",
              "fieldName": "ga:searchUniques"
            }
          ],
          "samplingLevel": "DEFAULT"
      }
    ]
  }

您可以在此处找到批处理的其他详细信息https://developers.google.com/analytics/devguides/reporting/core/v4/basics