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. I just started study programming 2 months ago. 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. 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.
The solution is to use the "setLimit" method on RunReportRequest's Builder. For example, please update the request's builder to the following to return up to 10,000 rows.
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. 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.