|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2022 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 batch creation |
| 20 | + * of multiple reports. |
| 21 | + * See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/batchRunReports |
| 22 | + * for more information. |
| 23 | + * Usage: |
| 24 | + * composer update |
| 25 | + * php run_batch_report.php YOUR-GA4-PROPERTY-ID |
| 26 | + */ |
| 27 | + |
| 28 | +namespace Google\Cloud\Samples\Analytics\Data; |
| 29 | + |
| 30 | +// [START analyticsdata_run_batch_report] |
| 31 | +use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; |
| 32 | +use Google\Analytics\Data\V1beta\DateRange; |
| 33 | +use Google\Analytics\Data\V1beta\Dimension; |
| 34 | +use Google\Analytics\Data\V1beta\Metric; |
| 35 | +use Google\Analytics\Data\V1beta\MetricType; |
| 36 | +use Google\Analytics\Data\V1beta\RunReportRequest; |
| 37 | +use Google\Analytics\Data\V1beta\RunReportResponse; |
| 38 | + |
| 39 | +/** |
| 40 | + * Runs a batch report on a Google Analytics 4 property. |
| 41 | + * @param string $propertyId Your GA-4 Property ID |
| 42 | + */ |
| 43 | +function run_batch_report(string $propertyId) |
| 44 | +{ |
| 45 | + // Create an instance of the Google Analytics Data API client library. |
| 46 | + $client = new BetaAnalyticsDataClient(); |
| 47 | + |
| 48 | + // Make an API call. |
| 49 | + $response = $client->batchRunReports([ |
| 50 | + 'property' => 'properties/' . $propertyId, |
| 51 | + 'requests' => [ |
| 52 | + new RunReportRequest([ |
| 53 | + 'dimensions' => [ |
| 54 | + new Dimension(['name' => 'country']), |
| 55 | + new Dimension(['name' => 'region']), |
| 56 | + new Dimension(['name' => 'city']), |
| 57 | + ], |
| 58 | + 'metrics' => [new Metric(['name' => 'activeUsers'])], |
| 59 | + 'date_ranges' => [new DateRange([ |
| 60 | + 'start_date' => '2021-01-03', |
| 61 | + 'end_date' => '2021-01-09', |
| 62 | + ]), |
| 63 | + ], |
| 64 | + ]), |
| 65 | + new RunReportRequest([ |
| 66 | + 'dimensions' => [new Dimension(['name' => 'browser'])], |
| 67 | + 'metrics' => [new Metric(['name' => 'activeUsers'])], |
| 68 | + 'date_ranges' => [new DateRange([ |
| 69 | + 'start_date' => '2021-01-01', |
| 70 | + 'end_date' => '2021-01-31', |
| 71 | + ]), |
| 72 | + ], |
| 73 | + ]), |
| 74 | + ], |
| 75 | + ]); |
| 76 | + |
| 77 | + print 'Batch report results' . PHP_EOL; |
| 78 | + foreach ($response->getReports() as $report) { |
| 79 | + printBatchRunReportsResponse($report); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Print results of a runReport call. |
| 85 | + * @param RunReportResponse $response |
| 86 | + */ |
| 87 | +function printBatchRunReportsResponse(RunReportResponse $response) |
| 88 | +{ |
| 89 | + // [START analyticsdata_print_run_report_response_header] |
| 90 | + printf('%s rows received%s', $response->getRowCount(), PHP_EOL); |
| 91 | + foreach ($response->getDimensionHeaders() as $dimensionHeader) { |
| 92 | + printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL); |
| 93 | + } |
| 94 | + foreach ($response->getMetricHeaders() as $metricHeader) { |
| 95 | + printf( |
| 96 | + 'Metric header name: %s (%s)' . PHP_EOL, |
| 97 | + $metricHeader->getName(), |
| 98 | + MetricType::name($metricHeader->getType()) |
| 99 | + ); |
| 100 | + } |
| 101 | + // [END analyticsdata_print_run_report_response_header] |
| 102 | + |
| 103 | + // [START analyticsdata_print_run_report_response_rows] |
| 104 | + print 'Report result: ' . PHP_EOL; |
| 105 | + |
| 106 | + foreach ($response->getRows() as $row) { |
| 107 | + printf( |
| 108 | + '%s %s' . PHP_EOL, |
| 109 | + $row->getDimensionValues()[0]->getValue(), |
| 110 | + $row->getMetricValues()[0]->getValue() |
| 111 | + ); |
| 112 | + } |
| 113 | + // [END analyticsdata_print_run_report_response_rows] |
| 114 | +} |
| 115 | +// [END analyticsdata_run_batch_report] |
| 116 | + |
| 117 | +// The following 2 lines are only needed to run the samples |
| 118 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 119 | +return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments