Skip to content

Commit b59a75e

Browse files
anweshangcf-owl-bot[bot]ikuleshov
authored
samples: add Java samples for metadata (googleapis#476)
* 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 * Add Java samples for metadata * Fixes linting error with mismatched filename * Style changes on comment * Changes comments * 🦉 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 572c4dc commit b59a75e

5 files changed

Lines changed: 270 additions & 0 deletions

File tree

‎java-analytics-data/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-analytics-dat
7979

8080
| Sample | Source Code | Try it |
8181
| --------------------------- | --------------------------------- | ------ |
82+
| Get Common Metadata Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/GetCommonMetadataSample.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/GetCommonMetadataSample.java) |
83+
| Get Metadata By Property Id Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/GetMetadataByPropertyIdSample.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/GetMetadataByPropertyIdSample.java) |
8284
| Quickstart Json Credentials Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/QuickstartJsonCredentialsSample.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/QuickstartJsonCredentialsSample.java) |
8385
| 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) |
8486
| 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) |
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.GetMetadataByPropertyIdSample"
29+
*/
30+
31+
// [START analyticsdata_get_metadata_by_property_id]
32+
33+
import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
34+
import com.google.analytics.data.v1beta.GetMetadataRequest;
35+
import com.google.analytics.data.v1beta.Metadata;
36+
37+
public class GetMetadataByPropertyIdSample {
38+
39+
public static void main(String... args) throws Exception {
40+
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
41+
String propertyId = "YOUR-GA4-PROPERTY-ID";
42+
sampleGetMetadataByPropertyId(propertyId);
43+
}
44+
45+
// Retrieves dimensions and metrics available for a Google Analytics 4 property, including custom
46+
// fields.
47+
static void sampleGetMetadataByPropertyId(String propertyId) throws Exception {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests. After completing all of your requests, call
50+
// the "close" method on the client to safely clean up any remaining background resources.
51+
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
52+
GetMetadataRequest request =
53+
GetMetadataRequest.newBuilder().setName("properties/" + propertyId + "/metadata").build();
54+
55+
// Make the request.
56+
Metadata response = analyticsData.getMetadata(request);
57+
58+
System.out.printf(
59+
"Dimensions and metrics available for Google Analytics 4 property %s "
60+
+ "(including custom fields):",
61+
propertyId);
62+
// Prints the response using a method in GetCommonMetadataSample.java
63+
GetCommonMetadataSample.printGetMetadataResponse(response);
64+
}
65+
}
66+
}
67+
// [END analyticsdata_get_metadata_by_property_id]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 GetCommonMetadata sample. */
28+
@RunWith(JUnit4.class)
29+
public class GetCommonMetadataSampleTest {
30+
31+
private String runSample() throws Exception {
32+
PrintStream stdOut = System.out;
33+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
34+
PrintStream out = new PrintStream(bout);
35+
System.setOut(out);
36+
37+
// Run the test using default system credentials.
38+
GetCommonMetadataSample.sampleGetCommonMetadata();
39+
System.setOut(stdOut);
40+
return bout.toString();
41+
}
42+
43+
@Test
44+
public void testGetCommonMetadata() throws Exception {
45+
// Act
46+
String out = runSample();
47+
48+
// Assert
49+
assertThat(out).contains("Dimensions and metrics");
50+
}
51+
}
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 GetMetadataByPropertyId sample. */
28+
@RunWith(JUnit4.class)
29+
public class GetMetadataByPropertyIdSampleTest {
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+
GetMetadataByPropertyIdSample.sampleGetMetadataByPropertyId(ga4PropertyId);
42+
System.setOut(stdOut);
43+
return bout.toString();
44+
}
45+
46+
@Test
47+
public void testGetMetadataByPropertyId() throws Exception {
48+
// Act
49+
String out = runSample(ga4PropertyId);
50+
51+
// Assert
52+
assertThat(out).contains("Dimensions and metrics");
53+
}
54+
}

0 commit comments

Comments
 (0)