Google Analytics报告API v4:获取会话和收入数据

时间:2017-04-12 22:33:18

标签: java google-analytics google-analytics-api google-analytics-v4 google-reporting-api

我尝试从Google Analytics Report API v4获取Sessions,Revenue,Transactions,Bounce Rate数据 与香奈儿分组:

  • 有机搜索
  • 电子邮件
  • 直接
  • 品牌付费搜索
  • 社会
  • 推荐
  • .. etc

现在我正在使用测试请求编写Java模块,该模块设置了以下参数:

尺寸:

GA:acquisitionTrafficChannel;

度量:

GA:会话

GA:percentNewSessions

GA:newusers使用

当我使用ga:acquisitionTrafficChannel + ga:sessions GA报告api返回值时,但当我尝试添加请求 ga:percentNewSessions,ga:newUsers 时,它会返回错误:

{
  "domain": "global",
  "message": "Selected dimensions and metrics cannot be queried together.",
  "reason": "badRequest"
}

要在代码中执行请求,请执行以下操作:

DateRange dateRange = new DateRange();
            dateRange.setStartDate("2015-06-15");
            dateRange.setEndDate("2015-06-30");
            ReportRequest request = new ReportRequest()
                    .setViewId(context.getProperty(VIEW_ID).evaluateAttributeExpressions().getValue())
                    .setDateRanges(Arrays.asList(dateRange))
                    .setDimensions(Arrays.asList(
                            new Dimension().setName("ga:acquisitionTrafficChannel")
                    ))
                    .setMetrics(Arrays.asList(
                            new Metric().setExpression("ga:sessions"),
                            new Metric().setExpression("ga:percentNewSessions"),
                            new Metric().setExpression("ga:newUsers")
                    ));

            ArrayList<ReportRequest> requests = new ArrayList<>();
            requests.add(request);
            GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
            GetReportsResponse response = service.reports().batchGet(getReport).execute();

如何正确申请?我走的是正确的方向吗?

因为正如我所说,我需要对收入,跳出率做同样的事情。 但我还没有完全理解如何在没有错误的情况下组合度量标准和维度。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

About my question:

As solution for my needs I used following combination in code:

To get all Channel groups ("Organic Search, Email, Direct, etc") I used following dimension: ga:channelGrouping - it will return all

To get values for Sessions, Revenue, Transactions, Bounce Rate, etc I used following metrics:

ga:sessions
ga:transactionRevenue
ga:transactions
ga:bounceRate

Also here can be more metrics if it is needed.

Maybe it will be useful to somebody.

Actually, question about error with combination in question (with ga:acquisitionTrafficChannel) is still open :)