|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | +/* This application demonstrates the usage of the Analytics Data API using |
| 20 | + service account credentials. For more information on service accounts, see |
| 21 | + https://cloud.google.com/iam/docs/understanding-service-accounts |
| 22 | +
|
| 23 | + The following document provides instructions on setting service account |
| 24 | + credentials for your application: |
| 25 | + https://cloud.google.com/docs/authentication/production |
| 26 | +
|
| 27 | + In a nutshell, you need to: |
| 28 | +
|
| 29 | + 1. Create a service account and download the key JSON file. |
| 30 | + https://cloud.google.com/docs/authentication/production#creating_a_service_account |
| 31 | +
|
| 32 | + 2. Provide service account credentials using one of the following options: |
| 33 | + - set the GOOGLE_APPLICATION_CREDENTIALS environment variable, the API |
| 34 | + client will use the value of this variable to find the service account key |
| 35 | + JSON file. |
| 36 | +
|
| 37 | + https://cloud.google.com/docs/authentication/production#setting_the_environment_variable |
| 38 | +
|
| 39 | + OR |
| 40 | +
|
| 41 | + - manually pass the path to the service account key JSON file to the API client |
| 42 | + by specifying the keyFilename parameter in the constructor. |
| 43 | + https://cloud.google.com/docs/authentication/production#passing_the_path_to_the_service_account_key_in_code |
| 44 | +
|
| 45 | + To run this sample using Maven: |
| 46 | + cd java-analytics-data/samples/snippets |
| 47 | + mvn compile |
| 48 | + mvn exec:java -Dexec.mainClass="QuickstartSample" |
| 49 | + */ |
| 50 | + |
| 51 | +// [START analytics_data_quickstart] |
| 52 | + |
| 53 | +import com.google.analytics.data.v1alpha.AlphaAnalyticsDataClient; |
| 54 | +import com.google.analytics.data.v1alpha.DateRange; |
| 55 | +import com.google.analytics.data.v1alpha.Dimension; |
| 56 | +import com.google.analytics.data.v1alpha.Entity; |
| 57 | +import com.google.analytics.data.v1alpha.Metric; |
| 58 | +import com.google.analytics.data.v1alpha.Row; |
| 59 | +import com.google.analytics.data.v1alpha.RunReportRequest; |
| 60 | +import com.google.analytics.data.v1alpha.RunReportResponse; |
| 61 | + |
| 62 | +public class QuickstartSample { |
| 63 | + |
| 64 | + // This is an example snippet that calls the Google Analytics Data API and runs a simple report |
| 65 | + // on the provided GA4 property id. |
| 66 | + static void sampleRunReport(String ga4PropertyId) throws Exception { |
| 67 | + // Instantiates a client using default credentials. |
| 68 | + // See https://cloud.google.com/docs/authentication/production for more information |
| 69 | + // about managing credentials. |
| 70 | + try (AlphaAnalyticsDataClient analyticsData = AlphaAnalyticsDataClient.create()) { |
| 71 | + RunReportRequest request = RunReportRequest.newBuilder() |
| 72 | + .setEntity(Entity.newBuilder().setPropertyId(ga4PropertyId)) |
| 73 | + .addDimensions( |
| 74 | + Dimension.newBuilder().setName("city")) |
| 75 | + .addMetrics(Metric.newBuilder().setName("activeUsers")) |
| 76 | + .addDateRanges( |
| 77 | + DateRange.newBuilder().setStartDate("2020-03-31").setEndDate("today")).build(); |
| 78 | + |
| 79 | + // Make the request |
| 80 | + RunReportResponse response = analyticsData.runReport(request); |
| 81 | + |
| 82 | + System.out.println("Report result:"); |
| 83 | + for (Row row : response.getRowsList()) { |
| 84 | + System.out.printf("%s, %s%n", row.getDimensionValues(0).getValue(), |
| 85 | + row.getMetricValues(0).getValue()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public static void main(String... args) throws Exception { |
| 91 | + // TODO(developer): Replace this variable with your GA4 property ID before running the sample. |
| 92 | + String ga4PropertyId = "GA4 PROPERTY ID"; |
| 93 | + sampleRunReport(ga4PropertyId); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// [END analytics_data_quickstart] |
0 commit comments