|
| 1 | +
|
| 2 | +// Copyright 2021 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 | +$property_id = 'YOUR-GA4-PROPERTY-ID'; |
| 17 | +// [START analytics_data_quickstart] |
| 18 | + |
| 19 | +require 'vendor/autoload.php'; |
| 20 | + |
| 21 | +use Google\Analytics\Data\V1alpha\AlphaAnalyticsDataClient; |
| 22 | +use Google\Analytics\Data\V1alpha\DateRange; |
| 23 | +use Google\Analytics\Data\V1alpha\Dimension; |
| 24 | +use Google\Analytics\Data\V1alpha\Entity; |
| 25 | +use Google\Analytics\Data\V1alpha\Metric; |
| 26 | + |
| 27 | +/** |
| 28 | + * TODO(developer): Uncomment this variable and replace with your GA4 |
| 29 | + * property ID before running the sample. |
| 30 | + */ |
| 31 | +// $property_id = 'YOUR-GA4-PROPERTY-ID'; |
| 32 | + |
| 33 | + |
| 34 | +// Make an API call. |
| 35 | +$client = new AlphaAnalyticsDataClient(); |
| 36 | +$response = $client->runReport([ |
| 37 | + 'entity' => new Entity([ |
| 38 | + 'property_id' => $property_id |
| 39 | + ]), |
| 40 | + 'dateRanges' => [ |
| 41 | + new DateRange([ |
| 42 | + 'start_date' => '2020-03-31', |
| 43 | + 'end_date' => 'today', |
| 44 | + ]), |
| 45 | + ], |
| 46 | + 'dimensions' => [new Dimension( |
| 47 | + [ |
| 48 | + 'name' => 'city', |
| 49 | + ]), |
| 50 | + ], |
| 51 | + 'metrics' => [new Metric( |
| 52 | + [ |
| 53 | + 'name' => 'activeUsers', |
| 54 | + ]) |
| 55 | + ] |
| 56 | +]); |
| 57 | + |
| 58 | +// Print results of an API call. |
| 59 | +print 'Report result: ' . PHP_EOL; |
| 60 | + |
| 61 | +foreach ($response->getRows() as $row) { |
| 62 | + print $row->getDimensionValues()[0]->getValue() |
| 63 | + . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL; |
| 64 | +} |
| 65 | + |
| 66 | + |
| 67 | +// [END analytics_data_quickstart] |
0 commit comments