|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2021 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 | +/* Google Analytics Data API sample quickstart application. |
| 19 | +
|
| 20 | + This application demonstrates the usage of the Analytics Data API using |
| 21 | + service account credentials from a JSON file downloaded from |
| 22 | + the Google Cloud Console. |
| 23 | +
|
| 24 | +Before you start the application, please review the comments starting with |
| 25 | +"TODO(developer)" and update the code to use the correct values. |
| 26 | +
|
| 27 | +Usage: |
| 28 | + composer update |
| 29 | + php quickstart_json_credentials.php |
| 30 | + */ |
| 31 | + |
| 32 | +// [START analytics_data_quickstart] |
| 33 | +require 'vendor/autoload.php'; |
| 34 | + |
| 35 | +use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; |
| 36 | +use Google\Analytics\Data\V1beta\DateRange; |
| 37 | +use Google\Analytics\Data\V1beta\Dimension; |
| 38 | +use Google\Analytics\Data\V1beta\Metric; |
| 39 | + |
| 40 | +/** |
| 41 | + * TODO(developer): Replace this variable with your Google Analytics 4 |
| 42 | + * property ID before running the sample. |
| 43 | + */ |
| 44 | +$property_id = 'YOUR-GA4-PROPERTY-ID'; |
| 45 | + |
| 46 | + |
| 47 | +// [START analyticsdata_json_credentials_initialize] |
| 48 | +/* TODO(developer): Replace this variable with a valid path to the |
| 49 | + * credentials.json file for your service account downloaded from the |
| 50 | + * Cloud Console. |
| 51 | +*/ |
| 52 | +$credentials_json_path = '/path/to/credentials.json'; |
| 53 | + |
| 54 | +// Explicitly use service account credentials by specifying |
| 55 | +// the private key file. |
| 56 | +$client = new BetaAnalyticsDataClient(['credentials' => |
| 57 | + $credentials_json_path]); |
| 58 | +// [END analyticsdata_json_credentials_initialize] |
| 59 | + |
| 60 | +// [START analyticsdata_json_credentials_run_report] |
| 61 | +// Make an API call. |
| 62 | +$response = $client->runReport([ |
| 63 | + 'property' => 'properties/' . $property_id, |
| 64 | + 'dateRanges' => [ |
| 65 | + new DateRange([ |
| 66 | + 'start_date' => '2020-03-31', |
| 67 | + 'end_date' => 'today', |
| 68 | + ]), |
| 69 | + ], |
| 70 | + 'dimensions' => [new Dimension( |
| 71 | + [ |
| 72 | + 'name' => 'city', |
| 73 | + ] |
| 74 | + ), |
| 75 | + ], |
| 76 | + 'metrics' => [new Metric( |
| 77 | + [ |
| 78 | + 'name' => 'activeUsers', |
| 79 | + ] |
| 80 | + ) |
| 81 | + ] |
| 82 | +]); |
| 83 | +// [END analyticsdata_json_credentials_run_report] |
| 84 | + |
| 85 | +// [START analyticsdata_json_credentials_run_report_response] |
| 86 | +// Print results of an API call. |
| 87 | +print 'Report result: ' . PHP_EOL; |
| 88 | + |
| 89 | +foreach ($response->getRows() as $row) { |
| 90 | + print $row->getDimensionValues()[0]->getValue() |
| 91 | + . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL; |
| 92 | + // [END analyticsdata_json_credentials_run_report_response] |
| 93 | +} |
| 94 | + |
| 95 | +// [END analytics_data_quickstart] |
0 commit comments