-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_funnel_report.php
235 lines (215 loc) · 8.79 KB
/
run_funnel_report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Copyright 2023 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.
*/
/**
* Google Analytics Data API sample application demonstrating the creation
* of a funnel report.
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport
* for more information.
*/
namespace Google\Analytics\Data\Samples;
// [START analyticsdata_run_funnel_report]
use Google\Analytics\Data\V1alpha\Client\AlphaAnalyticsDataClient;
use Google\Analytics\Data\V1alpha\DateRange;
use Google\Analytics\Data\V1alpha\Dimension;
use Google\Analytics\Data\V1alpha\FunnelBreakdown;
use Google\Analytics\Data\V1alpha\FunnelEventFilter;
use Google\Analytics\Data\V1alpha\FunnelFieldFilter;
use Google\Analytics\Data\V1alpha\FunnelFilterExpression;
use Google\Analytics\Data\V1alpha\FunnelFilterExpressionList;
use Google\Analytics\Data\V1alpha\FunnelStep;
use Google\Analytics\Data\V1alpha\Funnel;
use Google\Analytics\Data\V1alpha\FunnelSubReport;
use Google\Analytics\Data\V1alpha\RunFunnelReportRequest;
use Google\Analytics\Data\V1alpha\RunFunnelReportResponse;
use Google\Analytics\Data\V1alpha\StringFilter;
use Google\Analytics\Data\V1alpha\StringFilter\MatchType;
/**
* Runs a funnel query to build a report with 5 funnel steps.
*
* Step 1: First open/visit (event name is `first_open` or `first_visit`).
* Step 2: Organic visitors (`firstUserMedium` dimension contains the term "organic").
* Step 3: Session start (event name is `session_start`).
* Step 4: Screen/Page view (event name is `screen_view` or `page_view`).
* Step 5: Purchase (event name is `purchase` or `in_app_purchase`).
*
* The report configuration reproduces the default funnel report provided in the Funnel
* Exploration template of the Google Analytics UI. See more at
* https://support.google.com/analytics/answer/9327974
*
* @param string $propertyId Your GA-4 Property ID
*/
function run_funnel_report(string $propertyId)
{
// Create an instance of the Google Analytics Data API client library.
$client = new AlphaAnalyticsDataClient();
// Create the funnel report request.
$request = (new RunFunnelReportRequest())
->setProperty('properties/' . $propertyId)
->setDateRanges([
new DateRange([
'start_date' => '30daysAgo',
'end_date' => 'today',
]),
])
->setFunnelBreakdown(
new FunnelBreakdown([
'breakdown_dimension' =>
new Dimension([
'name' => 'deviceCategory'
])
])
)
->setFunnel(new Funnel());
// Add funnel steps to the funnel.
// 1. Add first open/visit step.
$request->getFunnel()->getSteps()[] = new FunnelStep([
'name' => 'First open/visit',
'filter_expression' => new FunnelFilterExpression([
'or_group' => new FunnelFilterExpressionList([
'expressions' => [
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'first_open',
])
]),
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'first_visit'
])
])
]
])
])
]);
// 2. Add organic visitors step.
$request->getFunnel()->getSteps()[] = new FunnelStep([
'name' => 'Organic visitors',
'filter_expression' => new FunnelFilterExpression([
'funnel_field_filter' => new FunnelFieldFilter([
'field_name' => 'firstUserMedium',
'string_filter' => new StringFilter([
'match_type' => MatchType::CONTAINS,
'case_sensitive' => false,
'value' => 'organic',
])
])
])
]);
// 3. Add session start step.
$request->getFunnel()->getSteps()[] = new FunnelStep([
'name' => 'Session start',
'filter_expression' => new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'session_start',
])
])
]);
// 4. Add screen/page view step.
$request->getFunnel()->getSteps()[] = new FunnelStep([
'name' => 'Screen/Page view',
'filter_expression' => new FunnelFilterExpression([
'or_group' => new FunnelFilterExpressionList([
'expressions' => [
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'screen_view',
])
]),
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'page_view'
])
])
]
])
])
]);
// 5. Add purchase step.
$request->getFunnel()->getSteps()[] = new FunnelStep([
'name' => 'Purchase',
'filter_expression' => new FunnelFilterExpression([
'or_group' => new FunnelFilterExpressionList([
'expressions' => [
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'purchase',
])
]),
new FunnelFilterExpression([
'funnel_event_filter' => new FunnelEventFilter([
'event_name' => 'in_app_purchase'
])
])
]
])
])
]);
// Make an API call.
$response = $client->runFunnelReport($request);
printRunFunnelReportResponse($response);
}
// [START analyticsdata_print_run_funnel_report_response]
/**
* Print results of a runFunnelReport call.
* @param RunFunnelReportResponse $response
*/
function printRunFunnelReportResponse(RunFunnelReportResponse $response)
{
print 'Report result: ' . PHP_EOL;
print '=== FUNNEL VISUALIZATION ===' . PHP_EOL;
printFunnelSubReport($response->getFunnelVisualization());
print '=== FUNNEL TABLE ===' . PHP_EOL;
printFunnelSubReport($response->getFunnelTable());
}
/**
* Print the contents of a FunnelSubReport object.
* @param FunnelSubReport $subReport
*/
function printFunnelSubReport(FunnelSubReport $subReport)
{
print 'Dimension headers:' . PHP_EOL;
foreach ($subReport->getDimensionHeaders() as $dimensionHeader) {
print $dimensionHeader->getName() . PHP_EOL;
}
print PHP_EOL . 'Metric headers:' . PHP_EOL;
foreach ($subReport->getMetricHeaders() as $metricHeader) {
print $metricHeader->getName() . PHP_EOL;
}
print PHP_EOL . 'Dimension and metric values for each row in the report:';
foreach ($subReport->getRows() as $rowIndex => $row) {
print PHP_EOL . 'Row #' . $rowIndex . PHP_EOL;
foreach ($row->getDimensionValues() as $dimIndex => $dimValue) {
$dimName = $subReport->getDimensionHeaders()[$dimIndex]->getName();
print $dimName . ": '" . $dimValue->getValue() . "'" . PHP_EOL;
}
foreach ($row->getMetricValues() as $metricIndex => $metricValue) {
$metricName = $subReport->getMetricHeaders()[$metricIndex]->getName();
print $metricName . ": '" . $metricValue->getValue() . "'" . PHP_EOL;
}
}
print PHP_EOL . 'Sampling metadata for each date range:' . PHP_EOL;
foreach($subReport->getMetadata()->getSamplingMetadatas() as $metadataIndex => $metadata) {
printf('Sampling metadata for date range #%d: samplesReadCount=%d' .
'samplingSpaceSize=%d%s',
$metadataIndex, $metadata->getSamplesReadCount(), $metadata->getSamplingSpaceSize(), PHP_EOL);
}
}
// [END analyticsdata_print_run_funnel_report_response]
// [END analyticsdata_run_funnel_report]
// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../testing/sample_helpers.php';
return \Google\Analytics\Data\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);