APIv4 直方图 - 无法排序

APIv4 Histogram - can't sort

我正在尝试通过 APIv4 从 google 分析中获取 5 天直方图,其中每个桶是 1 天

这是我正在尝试实现的查询,当 运行通过查询构建器

时,它工作正常
{
  "reportRequests": [
    {
      "viewId": "810020000",
      "filtersExpression": "ga:pagePath=@mypage",
      "dateRanges": [
        {
          "startDate": "5daysAgo",
          "endDate": "today"
        }
      ],
      "metrics": [
        {
          "expression": "ga:entranceRate"
        }
      ],
      "dimensions": [
        {
          "name": "ga:nthDay",
          "histogramBuckets": [
            "0",
            "1",
            "2",
            "3",
            "4"
          ]
        }
      ],
      "orderBys": [
        {
          "fieldName": "ga:nthDay",
          "orderType": "HISTOGRAM_BUCKET",
          "sortOrder": "ASCENDING"
        }
      ]
    }
  ]
}

然而当我尝试通过JAVA(APIv4)运行相同的查询时,我只得到一个结果(如果我不添加sortBy) 如果我添加 sort

,下面会出现错误
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Sort key ga:nthDay is not a dimension or metric in this query.",
    "reason" : "badRequest"
  } ],
  "message" : "Sort key ga:nthDay is not a dimension or metric in this query.",
  "status" : "INVALID_ARGUMENT"
}

这是java代码

Integer uniquePageviews = analyticsApi.getUniquePageViewsHistogramDaily("5daysAgo", "today", "ga:uniquePageviews");

public Integer getUniquePageViewsHistogramDaily(String startDayPhrase, String endDayPhrase, String gaMetricsPhrase) throws IOException {
            DateRange dateRange = new DateRange();
            dateRange.setStartDate(startDayPhrase);
            dateRange.setEndDate(endDayPhrase);
            List<DateRange> dateRanges = new ArrayList<>();
            dateRanges.add(dateRange);

            Metric metric = new Metric();
            metric.setExpression(gaMetricsPhrase);
            List<Metric> metrics = new ArrayList<>();
            metrics.add(metric);

        //TODO caclulate dynamically
        List<Long> histogramBuckets = new ArrayList<>();
        histogramBuckets.add(0L);
        histogramBuckets.add(1L);
        histogramBuckets.add(2L);
        histogramBuckets.add(3L);
        histogramBuckets.add(4L);

        List<Dimension> dimensions = new ArrayList<>();
        Dimension histogram = new Dimension();
        histogram.setName("ga:nthDay");
        histogram.setHistogramBuckets(histogramBuckets);


        List<OrderBy> orderBys = new ArrayList<>();
        OrderBy orderByDay = new OrderBy();
        orderByDay.setFieldName("ga:nthDay");
        orderByDay.setOrderType("HISTOGRAM_BUCKET");
        orderByDay.setSortOrder("ASCENDING");

        orderBys.add(orderByDay);

        return getUniquePageViewsHistogramDaily(dateRanges, metrics, dimensions, "ga:pagePath=@myaccount",orderBys);
    }

    private Integer getUniquePageViewsHistogramDaily(List<DateRange> dateRangeList,
                                                     List<Metric> metrics,
                                                     List<Dimension> dimensions,
                                                     String filterExpression,
                                                     List<OrderBy> orderBys) throws IOException {

        ReportRequest request = new ReportRequest()
                .setViewId(getViewId())
                .setDateRanges(dateRangeList)
                .setDimensions(dimensions)
                .setMetrics(metrics)
                .setFiltersExpression(filterExpression)
                .setOrderBys(orderBys);

        ArrayList<ReportRequest> requests = new ArrayList<>();
        requests.add(request);

        // Create the GetReportsRequest object.
        GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
        // Call the batchGet method.
        GetReportsResponse response = analyticsBuilder.reports().batchGet(getReport).execute();


        return Integer.valueOf(response.getReports().get(0).getData().getTotals().get(0).getValues().get(0));
    }

如何通过 Java 获得正常的 5 天直方图?

好的 感谢 Intellij,我发现了这个问题 似乎我忘记将直方图维度添加到维度列表

dimensions.add(histogram);

现在一切正常 谢谢!