Skip to content

Commit 7983a8a

Browse files
committed
Add RunFunnelReportSample
1 parent 55bd284 commit 7983a8a

1 file changed

Lines changed: 229 additions & 0 deletions

File tree

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
* Copyright 2023 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+
// [START analyticsdata_run_funnel_report]
20+
import com.google.analytics.data.v1alpha.AlphaAnalyticsDataClient;
21+
import com.google.analytics.data.v1alpha.DateRange;
22+
import com.google.analytics.data.v1alpha.Dimension;
23+
import com.google.analytics.data.v1alpha.DimensionHeader;
24+
import com.google.analytics.data.v1alpha.Funnel;
25+
import com.google.analytics.data.v1alpha.FunnelBreakdown;
26+
import com.google.analytics.data.v1alpha.FunnelEventFilter;
27+
import com.google.analytics.data.v1alpha.FunnelFieldFilter;
28+
import com.google.analytics.data.v1alpha.FunnelFilterExpression;
29+
import com.google.analytics.data.v1alpha.FunnelFilterExpressionList;
30+
import com.google.analytics.data.v1alpha.FunnelStep;
31+
import com.google.analytics.data.v1alpha.FunnelSubReport;
32+
import com.google.analytics.data.v1alpha.MetricHeader;
33+
import com.google.analytics.data.v1alpha.Row;
34+
import com.google.analytics.data.v1alpha.RunFunnelReportRequest;
35+
import com.google.analytics.data.v1alpha.RunFunnelReportResponse;
36+
import com.google.analytics.data.v1alpha.SamplingMetadata;
37+
import com.google.analytics.data.v1alpha.StringFilter;
38+
import com.google.analytics.data.v1alpha.StringFilter.MatchType;
39+
40+
/* Google Analytics Data API sample application demonstrating the creation
41+
of a funnel report.
42+
43+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport
44+
for more information.
45+
46+
Before you start the application, please review the comments starting with
47+
"TODO(developer)" and update the code to use correct values.
48+
49+
To run this sample using Maven:
50+
cd google-analytics-data
51+
mvn compile
52+
mvn exec:java -Dexec.mainClass="com.example.analytics.RunFunnelReportSample"
53+
*/
54+
public class RunFunnelReportSample {
55+
56+
public static void main(String... args) throws Exception {
57+
/**
58+
* TODO(developer): Replace this variable with your Google Analytics 4 property ID before
59+
* running the sample.
60+
*/
61+
String propertyId = "YOUR-GA4-PROPERTY-ID";
62+
sampleRunFunnelReport(propertyId);
63+
}
64+
65+
/**
66+
* Runs a funnel query to build a report with 5 funnel steps.
67+
*
68+
* <ol>
69+
* <li>First open/visit (event name is `first_open` or `first_visit`).
70+
* <li>Organic visitors (`firstUserMedium` dimension contains the term "organic").
71+
* <li>Session start (event name is `session_start`).
72+
* <li>Screen/Page view (event name is `screen_view` or `page_view`).
73+
* <li>Purchase (event name is `purchase` or `in_app_purchase`).
74+
* </ol>
75+
*
76+
* The report configuration reproduces the default funnel report provided in the Funnel
77+
* Exploration template of the Google Analytics UI. See more at
78+
* https://support.google.com/analytics/answer/9327974
79+
*/
80+
static void sampleRunFunnelReport(String propertyId) throws Exception {
81+
82+
// Using a default constructor instructs the client to use the credentials
83+
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
84+
try (AlphaAnalyticsDataClient analyticsData = AlphaAnalyticsDataClient.create()) {
85+
RunFunnelReportRequest.Builder requestBuilder = RunFunnelReportRequest.newBuilder();
86+
requestBuilder
87+
.setProperty("properties/" + propertyId)
88+
.addDateRanges(DateRange.newBuilder().setStartDate("30daysAgo").setEndDate("today"))
89+
.setFunnelBreakdown(
90+
FunnelBreakdown.newBuilder()
91+
.setBreakdownDimension(Dimension.newBuilder().setName("deviceCategory")));
92+
Funnel.Builder funnelBuilder = requestBuilder.getFunnelBuilder();
93+
funnelBuilder.addSteps(
94+
FunnelStep.newBuilder()
95+
.setName("First open/visit")
96+
.setFilterExpression(
97+
FunnelFilterExpression.newBuilder()
98+
.setOrGroup(
99+
FunnelFilterExpressionList.newBuilder()
100+
.addExpressions(
101+
FunnelFilterExpression.newBuilder()
102+
.setFunnelEventFilter(
103+
FunnelEventFilter.newBuilder()
104+
.setEventName("first_open")))
105+
.addExpressions(
106+
FunnelFilterExpression.newBuilder()
107+
.setFunnelEventFilter(
108+
FunnelEventFilter.newBuilder()
109+
.setEventName("first_visit"))))));
110+
funnelBuilder.addSteps(
111+
FunnelStep.newBuilder()
112+
.setName("Organic visitors")
113+
.setFilterExpression(
114+
FunnelFilterExpression.newBuilder()
115+
.setFunnelFieldFilter(
116+
FunnelFieldFilter.newBuilder()
117+
.setFieldName("firstUserMedium")
118+
.setStringFilter(
119+
StringFilter.newBuilder()
120+
.setMatchType(MatchType.CONTAINS)
121+
.setCaseSensitive(false)
122+
.setValue("organic")))));
123+
funnelBuilder.addSteps(
124+
FunnelStep.newBuilder()
125+
.setName("Session start")
126+
.setFilterExpression(
127+
FunnelFilterExpression.newBuilder()
128+
.setFunnelEventFilter(
129+
FunnelEventFilter.newBuilder().setEventName("session_start"))));
130+
131+
funnelBuilder.addSteps(
132+
FunnelStep.newBuilder()
133+
.setName("Screen/Page view")
134+
.setFilterExpression(
135+
FunnelFilterExpression.newBuilder()
136+
.setOrGroup(
137+
FunnelFilterExpressionList.newBuilder()
138+
.addExpressions(
139+
FunnelFilterExpression.newBuilder()
140+
.setFunnelEventFilter(
141+
FunnelEventFilter.newBuilder()
142+
.setEventName("screen_view")))
143+
.addExpressions(
144+
FunnelFilterExpression.newBuilder()
145+
.setFunnelEventFilter(
146+
FunnelEventFilter.newBuilder()
147+
.setEventName("page_view"))))));
148+
funnelBuilder.addSteps(
149+
FunnelStep.newBuilder()
150+
.setName("Purchase")
151+
.setFilterExpression(
152+
FunnelFilterExpression.newBuilder()
153+
.setOrGroup(
154+
FunnelFilterExpressionList.newBuilder()
155+
.addExpressions(
156+
FunnelFilterExpression.newBuilder()
157+
.setFunnelEventFilter(
158+
FunnelEventFilter.newBuilder().setEventName("purchase")))
159+
.addExpressions(
160+
FunnelFilterExpression.newBuilder()
161+
.setFunnelEventFilter(
162+
FunnelEventFilter.newBuilder()
163+
.setEventName("in_app_purchase"))))));
164+
165+
// Make the request.
166+
RunFunnelReportResponse response = analyticsData.runFunnelReport(requestBuilder.build());
167+
printRunFunnelReportResponse(response);
168+
}
169+
}
170+
171+
// [START analyticsdata_print_run_funnel_report_response]
172+
/** Prints results of a runFunnelReport call. */
173+
static void printRunFunnelReportResponse(RunFunnelReportResponse response) {
174+
System.out.println("Report result:");
175+
System.out.println("=== FUNNEL VISUALIZATION ===");
176+
printFunnelSubReport(response.getFunnelVisualization());
177+
178+
System.out.println("=== FUNNEL TABLE ===");
179+
printFunnelSubReport(response.getFunnelTable());
180+
}
181+
182+
/** Prints the contents of a FunnelSubReport object. */
183+
private static void printFunnelSubReport(FunnelSubReport funnelSubReport) {
184+
System.out.println("Dimension headers:");
185+
for (DimensionHeader dimensionHeader : funnelSubReport.getDimensionHeadersList()) {
186+
System.out.println(dimensionHeader.getName());
187+
}
188+
System.out.println();
189+
190+
System.out.println("Metric headers:");
191+
for (MetricHeader metricHeader : funnelSubReport.getMetricHeadersList()) {
192+
System.out.println(metricHeader.getName());
193+
}
194+
System.out.println();
195+
196+
System.out.println("Dimension and metric values for each row in the report:");
197+
for (int rowIndex = 0; rowIndex < funnelSubReport.getRowsCount(); rowIndex++) {
198+
Row row = funnelSubReport.getRows(rowIndex);
199+
for (int fieldIndex = 0; fieldIndex < row.getDimensionValuesCount(); fieldIndex++) {
200+
System.out.printf(
201+
"%s: '%s'%n",
202+
funnelSubReport.getDimensionHeaders(fieldIndex).getName(),
203+
row.getDimensionValues(fieldIndex).getValue());
204+
}
205+
for (int fieldIndex = 0; fieldIndex < row.getMetricValuesCount(); fieldIndex++) {
206+
System.out.printf(
207+
"%s: '%s'%n",
208+
funnelSubReport.getMetricHeaders(fieldIndex).getName(),
209+
row.getMetricValues(fieldIndex).getValue());
210+
}
211+
}
212+
System.out.println();
213+
214+
System.out.println("Sampling metadata for each date range:");
215+
for (int metadataIndex = 0;
216+
metadataIndex < funnelSubReport.getMetadata().getSamplingMetadatasCount();
217+
metadataIndex++) {
218+
SamplingMetadata samplingMetadata =
219+
funnelSubReport.getMetadata().getSamplingMetadatas(metadataIndex);
220+
System.out.printf(
221+
"Sampling metadata for date range #%d: samplesReadCount=%d, samplingSpaceSize=%d%n",
222+
metadataIndex,
223+
samplingMetadata.getSamplesReadCount(),
224+
samplingMetadata.getSamplingSpaceSize());
225+
}
226+
}
227+
// [END analyticsdata_print_run_funnel_report_response]
228+
}
229+
// [END analyticsdata_run_funnel_report]

0 commit comments

Comments
 (0)