Skip to content

Commit fd205fe

Browse files
anweshangcf-owl-bot[bot]ikuleshov
authored
samples: add Java samples for RunRealtimeReport (googleapis#475)
* A copy sample to test fork process * Adds sample RunReportSample * Modifies runReport sample to have headers printed * Addresses comment differences inbetween python and java sample * Changes println to printf for consistency * Test for run report * Fixes a matching tag * Fixes checkstyle violation * -Another commit to fix checkstyle violations * Changes to linting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Adds samples related to date ranges for java * Removes spaces in readme * Fixes spacing * Fixes linting * Fixes linting violations * Adds java samples with multiple metrics/dimensions * Fixes lines in README * Adds Java samples for dimension filters * Adds java samples related to dimension filters * Adds Java samples for RunRealtimeReport * Style changes on comment * Comment changes on files * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixes incorrect metric * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: ikuleshov <ikuleshov@google.com>
1 parent 46b3f83 commit fd205fe

7 files changed

Lines changed: 404 additions & 0 deletions

File tree

‎java-analytics-data/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-analytics-dat
8585
| Quickstart Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/QuickstartSample.java) |
8686
| Run Batch Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunBatchReportSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunBatchReportSample.java) |
8787
| Run Pivot Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunPivotReportSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunPivotReportSample.java) |
88+
| Run Realtime Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportSample.java) |
89+
| Run Realtime Report With Multiple Dimensions Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleDimensionsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleDimensionsSample.java) |
90+
| Run Realtime Report With Multiple Metrics Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleMetricsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleMetricsSample.java) |
8891
| Run Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunReportSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunReportSample.java) |
8992
| Run Report With Aggregations Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunReportWithAggregationsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunReportWithAggregationsSample.java) |
9093
| Run Report With Cohorts Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunReportWithCohortsSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-analytics-data&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/analytics/RunReportWithCohortsSample.java) |
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
/* Google Analytics Data API sample application demonstrating the creation of
20+
a realtime report.
21+
22+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
23+
for more information.
24+
25+
Before you start the application, please review the comments starting with
26+
"TODO(developer)" and update the code to use correct values.
27+
28+
To run this sample using Maven:
29+
cd java-analytics-data/samples/snippets
30+
mvn compile
31+
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportSample"
32+
*/
33+
34+
// [START analyticsdata_run_realtime_report]
35+
36+
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
37+
import com.google.analytics.data.v1beta.Dimension;
38+
import com.google.analytics.data.v1beta.DimensionHeader;
39+
import com.google.analytics.data.v1beta.Metric;
40+
import com.google.analytics.data.v1beta.MetricHeader;
41+
import com.google.analytics.data.v1beta.Row;
42+
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
43+
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;
44+
45+
public class RunRealtimeReportSample {
46+
47+
public static void main(String... args) throws Exception {
48+
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
49+
String propertyId = "YOUR-GA4-PROPERTY-ID";
50+
sampleRunRealtimeReport(propertyId);
51+
}
52+
53+
// Runs a realtime report on a Google Analytics 4 property.
54+
static void sampleRunRealtimeReport(String propertyId) throws Exception {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests. After completing all of your requests, call
57+
// the "close" method on the client to safely clean up any remaining background resources.
58+
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
59+
RunRealtimeReportRequest request =
60+
RunRealtimeReportRequest.newBuilder()
61+
.setProperty("properties/" + propertyId)
62+
.addDimensions(Dimension.newBuilder().setName("country"))
63+
.addMetrics(Metric.newBuilder().setName("activeUsers"))
64+
.build();
65+
66+
// Make the request.
67+
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
68+
printRunRealtimeReportResponse(response);
69+
}
70+
}
71+
72+
// Prints results of a runRealReport call.
73+
static void printRunRealtimeReportResponse(RunRealtimeReportResponse response) {
74+
// [START analyticsdata_print_run_realtime_report_response_header]
75+
System.out.printf("%s rows received%n", response.getRowsList().size());
76+
77+
for (DimensionHeader header : response.getDimensionHeadersList()) {
78+
System.out.printf("Dimension header name: %s%n", header.getName());
79+
}
80+
81+
for (MetricHeader header : response.getMetricHeadersList()) {
82+
System.out.printf("Metric header name: %s (%s)%n", header.getName(), header.getType());
83+
}
84+
// [END analyticsdata_print_run_realtime_report_response_header]
85+
86+
// [START analyticsdata_print_run_realtime_report_response_rows]
87+
System.out.println("Report result:");
88+
for (Row row : response.getRowsList()) {
89+
System.out.printf(
90+
"%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
91+
}
92+
// [END analyticsdata_print_run_realtime_report_response_rows]
93+
}
94+
}
95+
// [END analyticsdata_run_realtime_report]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
/* Google Analytics Data API sample application demonstrating the creation of
20+
a realtime report.
21+
22+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
23+
for more information.
24+
25+
Before you start the application, please review the comments starting with
26+
"TODO(developer)" and update the code to use correct values.
27+
28+
To run this sample using Maven:
29+
cd java-analytics-data/samples/snippets
30+
mvn compile
31+
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportWithMultipleDimensionsSample"
32+
*/
33+
34+
// [START analyticsdata_run_realtime_report_with_multiple_dimensions]
35+
36+
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
37+
import com.google.analytics.data.v1beta.Dimension;
38+
import com.google.analytics.data.v1beta.Metric;
39+
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
40+
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;
41+
42+
public class RunRealtimeReportWithMultipleDimensionsSample {
43+
44+
public static void main(String... args) throws Exception {
45+
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
46+
String propertyId = "YOUR-GA4-PROPERTY-ID";
47+
sampleRunRealtimeReportWithMultipleDimensions(propertyId);
48+
}
49+
50+
// Runs a realtime report on a Google Analytics 4 property.
51+
static void sampleRunRealtimeReportWithMultipleDimensions(String propertyId) throws Exception {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests. After completing all of your requests, call
54+
// the "close" method on the client to safely clean up any remaining background resources.
55+
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
56+
RunRealtimeReportRequest request =
57+
RunRealtimeReportRequest.newBuilder()
58+
.setProperty("properties/" + propertyId)
59+
.addDimensions(Dimension.newBuilder().setName("country"))
60+
.addDimensions(Dimension.newBuilder().setName(("city")))
61+
.addMetrics(Metric.newBuilder().setName("activeUsers"))
62+
.build();
63+
64+
// Make the request.
65+
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
66+
// Prints the response using a method in RunRealtimeReportSample.java
67+
RunRealtimeReportSample.printRunRealtimeReportResponse(response);
68+
}
69+
}
70+
}
71+
// [END analyticsdata_run_realtime_report_with_multiple_dimensions]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
/* Google Analytics Data API sample application demonstrating the creation of
20+
a realtime report.
21+
22+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
23+
for more information.
24+
25+
Before you start the application, please review the comments starting with
26+
"TODO(developer)" and update the code to use correct values.
27+
28+
To run this sample using Maven:
29+
cd java-analytics-data/samples/snippets
30+
mvn compile
31+
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportWithMultipleMetricsSample"
32+
*/
33+
34+
// [START analyticsdata_run_realtime_report_with_multiple_metrics]
35+
36+
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
37+
import com.google.analytics.data.v1beta.Dimension;
38+
import com.google.analytics.data.v1beta.Metric;
39+
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
40+
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;
41+
42+
public class RunRealtimeReportWithMultipleMetricsSample {
43+
44+
public static void main(String... args) throws Exception {
45+
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
46+
String propertyId = "YOUR-GA4-PROPERTY-ID";
47+
sampleRunRealtimeReportWithMultipleMetrics(propertyId);
48+
}
49+
50+
// Runs a realtime report on a Google Analytics 4 property.
51+
static void sampleRunRealtimeReportWithMultipleMetrics(String propertyId) throws Exception {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests. After completing all of your requests, call
54+
// the "close" method on the client to safely clean up any remaining background resources.
55+
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
56+
RunRealtimeReportRequest request =
57+
RunRealtimeReportRequest.newBuilder()
58+
.setProperty("properties/" + propertyId)
59+
.addDimensions(Dimension.newBuilder().setName("unifiedScreenName"))
60+
.addMetrics(Metric.newBuilder().setName(("screenPageViews")))
61+
.addMetrics(Metric.newBuilder().setName("conversions"))
62+
.build();
63+
64+
// Make the request.
65+
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
66+
// Prints the response using a method in RunRealtimeReportSample.java
67+
RunRealtimeReportSample.printRunRealtimeReportResponse(response);
68+
}
69+
}
70+
}
71+
// [END analyticsdata_run_realtime_report_with_multiple_metrics]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
/** Tests for the RunRealtimeReport sample. */
28+
@RunWith(JUnit4.class)
29+
public class RunRealtimeReportSampleTest {
30+
31+
private String ga4PropertyId =
32+
System.getProperty("analyticsdata.quickstart.ga4PropertyId", "222596558");
33+
34+
private String runSample(String ga4PropertyId) throws Exception {
35+
PrintStream stdOut = System.out;
36+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
37+
PrintStream out = new PrintStream(bout);
38+
System.setOut(out);
39+
40+
// Run the test using default system credentials.
41+
RunRealtimeReportSample.sampleRunRealtimeReport(ga4PropertyId);
42+
System.setOut(stdOut);
43+
return bout.toString();
44+
}
45+
46+
@Test
47+
public void testRunRealtimeReport() throws Exception {
48+
// Act
49+
String out = runSample(ga4PropertyId);
50+
51+
// Assert
52+
assertThat(out).contains("Report result:");
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.analytics;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
/** Tests for the RunRealtimeReportWithMultipleDimensions sample. */
28+
@RunWith(JUnit4.class)
29+
public class RunRealtimeReportWithMultipleDimensionsSampleTest {
30+
31+
private String ga4PropertyId =
32+
System.getProperty("analyticsdata.quickstart.ga4PropertyId", "222596558");
33+
34+
private String runSample(String ga4PropertyId) throws Exception {
35+
PrintStream stdOut = System.out;
36+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
37+
PrintStream out = new PrintStream(bout);
38+
System.setOut(out);
39+
40+
// Run the test using default system credentials.
41+
RunRealtimeReportWithMultipleDimensionsSample.sampleRunRealtimeReportWithMultipleDimensions(
42+
ga4PropertyId);
43+
System.setOut(stdOut);
44+
return bout.toString();
45+
}
46+
47+
@Test
48+
public void testRunRealtimeReportWithMultipleDimensions() throws Exception {
49+
// Act
50+
String out = runSample(ga4PropertyId);
51+
52+
// Assert
53+
assertThat(out).contains("Report result:");
54+
}
55+
}

0 commit comments

Comments
 (0)