File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_minute_ranges]
35+
36+
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
37+
import com.google.analytics.data.v1beta.Metric;
38+
import com.google.analytics.data.v1beta.MinuteRange;
39+
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
40+
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;
41+
42+
public class RunRealtimeReportWithMinuteRangesSample {
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+
sampleRunRealtimeReportWithMinuteRanges(propertyId);
48+
}
49+
50+
// Runs a realtime report on a Google Analytics 4 property.
51+
static void sampleRunRealtimeReportWithMinuteRanges(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+
.addMetrics(Metric.newBuilder().setName(("activeUsers")))
60+
.addMinuteRanges(
61+
MinuteRange.newBuilder().setName("0-4 minutes ago").setStartMinutesAgo(4))
62+
.addMinuteRanges(
63+
MinuteRange.newBuilder()
64+
.setName("25-29 minutes ago")
65+
.setEndMinutesAgo(29)
66+
.setEndMinutesAgo(25))
67+
.build();
68+
69+
// Make the request.
70+
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
71+
// Prints the response using a method in RunRealtimeReportSample.java
72+
RunRealtimeReportSample.printRunRealtimeReportResponse(response);
73+
}
74+
}
75+
}
76+
// [END analyticsdata_run_realtime_report_with_minute_ranges]
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 RunRealtimeReportWithMinuteRanges sample. */
28+
@RunWith(JUnit4.class)
29+
public class RunRealtimeReportWithMinuteRangesSampleTest {
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+
RunRealtimeReportWithMinuteRangesSample.sampleRunRealtimeReportWithMinuteRanges(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+
}

0 commit comments

Comments
 (0)