|
| 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 retrieving dimension and metrics |
| 20 | +metadata. |
| 21 | +
|
| 22 | +See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata |
| 23 | +for more information. |
| 24 | +
|
| 25 | +To run this sample using Maven: |
| 26 | + cd java-analytics-data/samples/snippets |
| 27 | + mvn compile |
| 28 | + mvn exec:java -Dexec.mainClass="com.example.analytics.GetCommonMetadataSample" |
| 29 | + */ |
| 30 | + |
| 31 | +// [START analyticsdata_get_common_metadata] |
| 32 | + |
| 33 | +import com.google.analytics.data.v1beta.BetaAnalyticsDataClient; |
| 34 | +import com.google.analytics.data.v1beta.DimensionMetadata; |
| 35 | +import com.google.analytics.data.v1beta.GetMetadataRequest; |
| 36 | +import com.google.analytics.data.v1beta.Metadata; |
| 37 | +import com.google.analytics.data.v1beta.MetricMetadata; |
| 38 | + |
| 39 | +public class GetCommonMetadataSample { |
| 40 | + |
| 41 | + public static void main(String... args) throws Exception { |
| 42 | + // Runs the sample. |
| 43 | + sampleGetCommonMetadata(); |
| 44 | + } |
| 45 | + |
| 46 | + // Retrieves dimensions and metrics available for all Google Analytics 4 properties. |
| 47 | + static void sampleGetCommonMetadata() throws Exception { |
| 48 | + // Set the Property ID to 0 for dimensions and metrics common to all properties. In this special |
| 49 | + // mode, this method will not return custom dimensions and metrics. |
| 50 | + String propertyId = "0"; |
| 51 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 52 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 53 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 54 | + try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) { |
| 55 | + GetMetadataRequest request = |
| 56 | + GetMetadataRequest.newBuilder().setName("properties/" + propertyId + "/metadata").build(); |
| 57 | + |
| 58 | + // Make the request. |
| 59 | + Metadata response = analyticsData.getMetadata(request); |
| 60 | + |
| 61 | + System.out.println("Dimensions and metrics available for all Google Analytics 4 properties:"); |
| 62 | + printGetMetadataResponse(response); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Prints results of the getMetadata call. |
| 67 | + static void printGetMetadataResponse(Metadata response) { |
| 68 | + // [START analyticsdata_print_get_metadata_response] |
| 69 | + for (DimensionMetadata dimension : response.getDimensionsList()) { |
| 70 | + System.out.println("DIMENSION"); |
| 71 | + System.out.printf( |
| 72 | + "%s (%s): %s%n", |
| 73 | + dimension.getApiName(), dimension.getUiName(), dimension.getDescription()); |
| 74 | + System.out.printf("custom definition: %s%n", dimension.getCustomDefinition()); |
| 75 | + if (dimension.getDeprecatedApiNamesList() != null |
| 76 | + && !dimension.getDeprecatedApiNamesList().isEmpty()) { |
| 77 | + System.out.printf("Deprecated API names: %s%n", dimension.getDeprecatedApiNamesList()); |
| 78 | + } |
| 79 | + System.out.println(); |
| 80 | + } |
| 81 | + for (MetricMetadata metric : response.getMetricsList()) { |
| 82 | + System.out.println("METRIC"); |
| 83 | + System.out.printf( |
| 84 | + "%s (%s): %s%n", metric.getApiName(), metric.getUiName(), metric.getDescription()); |
| 85 | + System.out.printf("custom definition: %s%n", metric.getCustomDefinition()); |
| 86 | + System.out.printf("Type: %s%n", metric.getType()); |
| 87 | + if (metric.getDeprecatedApiNamesList() != null |
| 88 | + && !metric.getDeprecatedApiNamesList().isEmpty()) { |
| 89 | + System.out.printf("Deprecated API names: %s%n", metric.getDeprecatedApiNamesList()); |
| 90 | + } |
| 91 | + System.out.println(); |
| 92 | + } |
| 93 | + // [END analyticsdata_print_get_metadata_response] |
| 94 | + } |
| 95 | +} |
| 96 | +// [END analyticsdata_get_common_metadata] |
0 commit comments