我正在尝试通过APIv4从谷歌分析中获取 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天直方图?
答案 0 :(得分:1)
行 感谢Intellij,我发现了这个问题 似乎我忘了将直方图尺寸添加到尺寸列表
dimensions.add(histogram);
现在一切都按预期工作了 谢谢!