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