Skip to content
This repository was archived by the owner on Jan 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a3ed2a7
A copy sample to test fork process
anweshan Mar 16, 2022
88f41b8
Adds sample RunReportSample
anweshan Mar 16, 2022
8a04c19
Modifies runReport sample to have headers printed
anweshan Mar 16, 2022
29a535c
Addresses comment differences inbetween python and java sample
anweshan Mar 21, 2022
b60322d
Changes println to printf for consistency
anweshan Mar 21, 2022
79adead
Test for run report
anweshan Mar 22, 2022
546100f
Fixes a matching tag
anweshan Mar 22, 2022
cc370ef
Fixes checkstyle violation
anweshan Mar 22, 2022
e211886
-Another commit to fix checkstyle violations
anweshan Mar 22, 2022
1203155
Changes to linting
anweshan Mar 22, 2022
74f1305
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 22, 2022
b91eb33
Merge branch 'main' into main
ikuleshov Mar 22, 2022
5800c09
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Mar 23, 2022
ac86d60
Merge branch 'main' into main
ikuleshov Mar 23, 2022
ae367fe
Fixes merge conflicts
anweshan Apr 6, 2022
da09669
Adds samples related to date ranges for java
anweshan Apr 12, 2022
150028e
Removes spaces in readme
anweshan Apr 12, 2022
1541859
Fixes spacing
anweshan Apr 12, 2022
3003ac0
Fixes linting
anweshan Apr 12, 2022
a3cab45
Fixes linting violations
anweshan Apr 12, 2022
cb2fee6
Adds java samples with multiple metrics/dimensions
anweshan Apr 12, 2022
9cb2044
Fixes lines in README
anweshan Apr 12, 2022
ccadaad
Adds Java samples for dimension filters
anweshan Apr 12, 2022
32d8961
Adds java samples related to dimension filters
anweshan Apr 12, 2022
044ce53
Adds Java samples for RunRealtimeReport
anweshan Apr 12, 2022
d7a9ace
Style changes on comment
anweshan Apr 13, 2022
3497289
Comment changes on files
anweshan Apr 25, 2022
6f0b2ef
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 17, 2022
3175479
Merge branch 'main' into java-samples-6
ikuleshov May 17, 2022
cf32955
Fixes incorrect metric
anweshan May 20, 2022
a86c079
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 24, 2022
84e4729
Merge branch 'main' into java-samples-6
anweshan May 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ If you are using Maven, add this to your pom.xml file:
<dependency>
<groupId>com.google.analytics</groupId>
<artifactId>google-analytics-data</artifactId>
<version>0.12.0</version>
<version>0.13.0</version>
</dependency>
```

If you are using Gradle without BOM, add this to your dependencies

```Groovy
implementation 'com.google.analytics:google-analytics-data:0.12.0'
implementation 'com.google.analytics:google-analytics-data:0.13.0'
```

If you are using SBT, add this to your dependencies

```Scala
libraryDependencies += "com.google.analytics" % "google-analytics-data" % "0.12.0"
libraryDependencies += "com.google.analytics" % "google-analytics-data" % "0.13.0"
```

## Authentication
Expand Down Expand Up @@ -85,6 +85,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-analytics-dat
| 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) |
| Run Batch Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunBatchReportSample.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/RunBatchReportSample.java) |
| Run Pivot Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunPivotReportSample.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/RunPivotReportSample.java) |
| Run Realtime Report Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportSample.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/RunRealtimeReportSample.java) |
| Run Realtime Report With Multiple Dimensions Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleDimensionsSample.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/RunRealtimeReportWithMultipleDimensionsSample.java) |
| Run Realtime Report With Multiple Metrics Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunRealtimeReportWithMultipleMetricsSample.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/RunRealtimeReportWithMultipleMetricsSample.java) |
| 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) |
| Run Report With Aggregations Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunReportWithAggregationsSample.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/RunReportWithAggregationsSample.java) |
| Run Report With Cohorts Sample | [source code](https://github.com/googleapis/java-analytics-data/blob/main/samples/snippets/src/main/java/com/example/analytics/RunReportWithCohortsSample.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/RunReportWithCohortsSample.java) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.analytics;

/* Google Analytics Data API sample application demonstrating the creation of
a realtime report.

See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
for more information.

Before you start the application, please review the comments starting with
"TODO(developer)" and update the code to use correct values.

To run this sample using Maven:
cd java-analytics-data/samples/snippets
mvn compile
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportSample"
*/

// [START analyticsdata_run_realtime_report]

import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
import com.google.analytics.data.v1beta.Dimension;
import com.google.analytics.data.v1beta.DimensionHeader;
import com.google.analytics.data.v1beta.Metric;
import com.google.analytics.data.v1beta.MetricHeader;
import com.google.analytics.data.v1beta.Row;
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;

public class RunRealtimeReportSample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
String propertyId = "YOUR-GA4-PROPERTY-ID";
sampleRunRealtimeReport(propertyId);
}

// Runs a realtime report on a Google Analytics 4 property.
static void sampleRunRealtimeReport(String propertyId) throws Exception {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
RunRealtimeReportRequest request =
RunRealtimeReportRequest.newBuilder()
.setProperty("properties/" + propertyId)
.addDimensions(Dimension.newBuilder().setName("country"))
.addMetrics(Metric.newBuilder().setName("activeUsers"))
.build();

// Make the request.
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
printRunRealtimeReportResponse(response);
}
}

// Prints results of a runRealReport call.
static void printRunRealtimeReportResponse(RunRealtimeReportResponse response) {
// [START analyticsdata_print_run_realtime_report_response_header]
System.out.printf("%s rows received%n", response.getRowsList().size());

for (DimensionHeader header : response.getDimensionHeadersList()) {
System.out.printf("Dimension header name: %s%n", header.getName());
}

for (MetricHeader header : response.getMetricHeadersList()) {
System.out.printf("Metric header name: %s (%s)%n", header.getName(), header.getType());
}
// [END analyticsdata_print_run_realtime_report_response_header]

// [START analyticsdata_print_run_realtime_report_response_rows]
System.out.println("Report result:");
for (Row row : response.getRowsList()) {
System.out.printf(
"%s, %s%n", row.getDimensionValues(0).getValue(), row.getMetricValues(0).getValue());
}
// [END analyticsdata_print_run_realtime_report_response_rows]
}
}
// [END analyticsdata_run_realtime_report]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.analytics;

/* Google Analytics Data API sample application demonstrating the creation of
a realtime report.

See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
for more information.

Before you start the application, please review the comments starting with
"TODO(developer)" and update the code to use correct values.

To run this sample using Maven:
cd java-analytics-data/samples/snippets
mvn compile
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportWithMultipleDimensionsSample"
*/

// [START analyticsdata_run_realtime_report_with_multiple_dimensions]

import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
import com.google.analytics.data.v1beta.Dimension;
import com.google.analytics.data.v1beta.Metric;
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;

public class RunRealtimeReportWithMultipleDimensionsSample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
String propertyId = "YOUR-GA4-PROPERTY-ID";
sampleRunRealtimeReportWithMultipleDimensions(propertyId);
}

// Runs a realtime report on a Google Analytics 4 property.
static void sampleRunRealtimeReportWithMultipleDimensions(String propertyId) throws Exception {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
RunRealtimeReportRequest request =
RunRealtimeReportRequest.newBuilder()
.setProperty("properties/" + propertyId)
.addDimensions(Dimension.newBuilder().setName("country"))
.addDimensions(Dimension.newBuilder().setName(("city")))
.addMetrics(Metric.newBuilder().setName("activeUsers"))
.build();

// Make the request.
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
// Prints the response using a method in RunRealtimeReportSample.java
RunRealtimeReportSample.printRunRealtimeReportResponse(response);
}
}
}
// [END analyticsdata_run_realtime_report_with_multiple_dimensions]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.analytics;

/* Google Analytics Data API sample application demonstrating the creation of
a realtime report.

See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
for more information.

Before you start the application, please review the comments starting with
"TODO(developer)" and update the code to use correct values.

To run this sample using Maven:
cd java-analytics-data/samples/snippets
mvn compile
mvn exec:java -Dexec.mainClass="com.example.analytics.RunRealtimeReportWithMultipleMetricsSample"
*/

// [START analyticsdata_run_realtime_report_with_multiple_metrics]

import com.google.analytics.data.v1beta.BetaAnalyticsDataClient;
import com.google.analytics.data.v1beta.Dimension;
import com.google.analytics.data.v1beta.Metric;
import com.google.analytics.data.v1beta.RunRealtimeReportRequest;
import com.google.analytics.data.v1beta.RunRealtimeReportResponse;

public class RunRealtimeReportWithMultipleMetricsSample {

public static void main(String... args) throws Exception {
// TODO(developer): Replace with your Google Analytics 4 property ID before running the sample.
String propertyId = "YOUR-GA4-PROPERTY-ID";
sampleRunRealtimeReportWithMultipleMetrics(propertyId);
}

// Runs a realtime report on a Google Analytics 4 property.
static void sampleRunRealtimeReportWithMultipleMetrics(String propertyId) throws Exception {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BetaAnalyticsDataClient analyticsData = BetaAnalyticsDataClient.create()) {
RunRealtimeReportRequest request =
RunRealtimeReportRequest.newBuilder()
.setProperty("properties/" + propertyId)
.addDimensions(Dimension.newBuilder().setName("unifiedScreenName"))
.addMetrics(Metric.newBuilder().setName(("screenPageViews")))
.addMetrics(Metric.newBuilder().setName("conversions"))
.build();

// Make the request.
RunRealtimeReportResponse response = analyticsData.runRealtimeReport(request);
// Prints the response using a method in RunRealtimeReportSample.java
RunRealtimeReportSample.printRunRealtimeReportResponse(response);
}
}
}
// [END analyticsdata_run_realtime_report_with_multiple_metrics]
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.analytics;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for the RunRealtimeReport sample. */
@RunWith(JUnit4.class)
public class RunRealtimeReportSampleTest {

private String ga4PropertyId =
System.getProperty("analyticsdata.quickstart.ga4PropertyId", "222596558");

private String runSample(String ga4PropertyId) throws Exception {
PrintStream stdOut = System.out;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bout);
System.setOut(out);

// Run the test using default system credentials.
RunRealtimeReportSample.sampleRunRealtimeReport(ga4PropertyId);
System.setOut(stdOut);
return bout.toString();
}

@Test
public void testRunRealtimeReport() throws Exception {
// Act
String out = runSample(ga4PropertyId);

// Assert
assertThat(out).contains("Report result:");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.analytics;

import static com.google.common.truth.Truth.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for the RunRealtimeReportWithMultipleDimensions sample. */
@RunWith(JUnit4.class)
public class RunRealtimeReportWithMultipleDimensionsSampleTest {

private String ga4PropertyId =
System.getProperty("analyticsdata.quickstart.ga4PropertyId", "222596558");

private String runSample(String ga4PropertyId) throws Exception {
PrintStream stdOut = System.out;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
PrintStream out = new PrintStream(bout);
System.setOut(out);

// Run the test using default system credentials.
RunRealtimeReportWithMultipleDimensionsSample.sampleRunRealtimeReportWithMultipleDimensions(
ga4PropertyId);
System.setOut(stdOut);
return bout.toString();
}

@Test
public void testRunRealtimeReportWithMultipleDimensions() throws Exception {
// Act
String out = runSample(ga4PropertyId);

// Assert
assertThat(out).contains("Report result:");
}
}
Loading