[英]how to extend the get values while using Google Analytics Data API (GA4) in java to get count of screenPageViews?
I appreciate your attention to my ask.感谢您对我的提问的关注。 When I use the below java code to get information from google analytics, it shows just 10 values of pages... Actually, the total page on my website is 37 pages and I would like to get total values from ga4.
当我使用下面的 java 代码从谷歌分析获取信息时,它只显示 10 个页面值...实际上,我网站上的总页面是 37 页,我想从 ga4 获取总值。 I just started study programming 2 months ago.
我刚开始学习编程 2 个月前。 Please help me to solve it.
请帮我解决它。
private void testUpdateGoogleAnalyticsApi() {
String ga4PropertyId = Container.config.getGa4PropertyId();
try (AlphaAnalyticsDataClient analyticsData = AlphaAnalyticsDataClient.create()) {
RunReportRequest request = RunReportRequest.newBuilder()
.setEntity(Entity.newBuilder().setPropertyId(ga4PropertyId))
.addDimensions(Dimension.newBuilder().setName("pagePath"))
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
.addDateRanges(DateRange.newBuilder().setStartDate("2020-12-01").setEndDate("today")).build();
// Make the request
RunReportResponse response = analyticsData.runReport(request);
System.out.println("Report result:");
for (Row row : response.getRowsList()) {
System.out.printf("%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
}
} catch (IOException e) {
e.printStackTrace();
}
}
If the limit parameter in the RunReportRequest is unspecified, the RunReportResponse will contain only 10 rows.如果未指定 RunReportRequest 中的 limit 参数,则 RunReportResponse 将仅包含 10 行。 For this report, this means you will only see 10 pages paths in the API's report even though a larger number of pages (37) exist for your website.
对于此报告,这意味着即使您的网站存在大量页面 (37),您也只会在 API 报告中看到 10 个页面路径。
The solution is to use the "setLimit" method on RunReportRequest's Builder.解决方案是在 RunReportRequest 的 Builder 上使用“setLimit”方法。 For example, please update the request's builder to the following to return up to 10,000 rows.
例如,请将请求的构建器更新为以下内容以返回最多 10,000 行。
RunReportRequest request = RunReportRequest.newBuilder()
.setEntity(Entity.newBuilder().setPropertyId(ga4PropertyId))
.addDimensions(Dimension.newBuilder().setName("pagePath"))
.addMetrics(Metric.newBuilder().setName("screenPageViews"))
.addDateRanges(DateRange.newBuilder().setStartDate("2020-12-01").setEndDate("today"))
.setLimit(10000L).build();
There is documentation on this API's pagination mechanism here .这里有关于这个 API 的分页机制的文档。 Pagination specifies how many rows you would like to see in the response.
分页指定您希望在响应中看到多少行。 In this case, the number of pages from your website.
在这种情况下,来自您网站的页面数。
The setLimit method controls the number of rows in the report response and is documented on github here . setLimit 方法控制报告响应中的行数,并记录在github上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.