|
| 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] |
0 commit comments