|
| 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 | +
|
| 20 | +"""Google Analytics Data API sample application demonstrating the creation |
| 21 | +of a basic report. |
| 22 | +See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport |
| 23 | +for more information. |
| 24 | +""" |
| 25 | +
|
| 26 | +Before you start the application, please review the comments starting with |
| 27 | +"TODO(developer)" and update the code to use the correct values. |
| 28 | +
|
| 29 | +Usage: |
| 30 | + composer update |
| 31 | + php run_report.php |
| 32 | + */ |
| 33 | + |
| 34 | +// [START analyticsdata_run_report] |
| 35 | +require 'vendor/autoload.php'; |
| 36 | + |
| 37 | +use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; |
| 38 | +use Google\Analytics\Data\V1beta\DateRange; |
| 39 | +use Google\Analytics\Data\V1beta\Dimension; |
| 40 | +use Google\Analytics\Data\V1beta\Metric; |
| 41 | +use Google\Analytics\Data\V1beta\MetricType; |
| 42 | + |
| 43 | +function runReport() |
| 44 | +{ |
| 45 | + /** |
| 46 | + * TODO(developer): Replace this variable with your Google Analytics 4 |
| 47 | + * property ID before running the sample. |
| 48 | + */ |
| 49 | + $property_id = 'YOUR-GA4-PROPERTY-ID'; |
| 50 | + |
| 51 | + // [START analyticsdata_initialize] |
| 52 | + //Imports the Google Analytics Data API client library.' |
| 53 | + |
| 54 | + $client = new BetaAnalyticsDataClient(); |
| 55 | + |
| 56 | + // [END analyticsdata_initialize] |
| 57 | + |
| 58 | + // Make an API call. |
| 59 | + $response = $client->runReport([ |
| 60 | + 'property' => 'properties/' . $property_id, |
| 61 | + 'dateRanges' => [ |
| 62 | + new DateRange([ |
| 63 | + 'start_date' => '2020-09-01', |
| 64 | + 'end_date' => '2020-09-15', |
| 65 | + ]), |
| 66 | + ], |
| 67 | + 'dimensions' => [new Dimension( |
| 68 | + [ |
| 69 | + 'name' => 'country', |
| 70 | + ] |
| 71 | + ), |
| 72 | + ], |
| 73 | + 'metrics' => [new Metric( |
| 74 | + [ |
| 75 | + 'name' => 'activeUsers', |
| 76 | + ] |
| 77 | + ) |
| 78 | + ] |
| 79 | + ]); |
| 80 | + |
| 81 | + printRunReportResponse($response); |
| 82 | +} |
| 83 | + |
| 84 | +// Print results of a runReport call. |
| 85 | +function printRunReportResponse($response) |
| 86 | +{ |
| 87 | + // [START analyticsdata_print_run_report_response_header] |
| 88 | + printf('%s rows received%s', $response->getRowCount(), PHP_EOL); |
| 89 | + foreach ($response->getDimensionHeaders() as $dimensionHeader) { |
| 90 | + printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL); |
| 91 | + } |
| 92 | + foreach ($response->getMetricHeaders() as $metricHeader) { |
| 93 | + printf( |
| 94 | + 'Metric header name: %s (%s)%s', |
| 95 | + $metricHeader->getName(), |
| 96 | + MetricType::name($metricHeader->getType()), |
| 97 | + PHP_EOL |
| 98 | + ); |
| 99 | + } |
| 100 | + // [END analyticsdata_print_run_report_response_header] |
| 101 | + |
| 102 | + // [START analyticsdata_print_run_report_response_rows] |
| 103 | + print 'Report result: ' . PHP_EOL; |
| 104 | + |
| 105 | + foreach ($response->getRows() as $row) { |
| 106 | + print $row->getDimensionValues()[0]->getValue() |
| 107 | + . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL; |
| 108 | + } |
| 109 | + // [END analyticsdata_print_run_report_response_rows] |
| 110 | +} |
| 111 | +// [END analyticsdata_run_report] |
| 112 | +runReport(); |
0 commit comments