|
1 | 1 | <?php |
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. |
| 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 | + */ |
15 | 17 |
|
16 | | -$property_id = 'YOUR-GA4-PROPERTY-ID'; |
17 | | -// [START analytics_data_quickstart] |
| 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. |
| 22 | +
|
| 23 | +Before you start the application, please review the comments starting with |
| 24 | +"TODO(developer)" and update the code to use the correct values. |
18 | 25 |
|
| 26 | +Usage: |
| 27 | + composer update |
| 28 | + php quickstart.php |
| 29 | + */ |
| 30 | + |
| 31 | +// [START analytics_data_quickstart] |
19 | 32 | require 'vendor/autoload.php'; |
20 | 33 |
|
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; |
| 34 | +use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; |
| 35 | +use Google\Analytics\Data\V1beta\DateRange; |
| 36 | +use Google\Analytics\Data\V1beta\Dimension; |
| 37 | +use Google\Analytics\Data\V1beta\Entity; |
| 38 | +use Google\Analytics\Data\V1beta\Metric; |
26 | 39 |
|
27 | 40 | /** |
28 | | - * TODO(developer): Uncomment this variable and replace with your GA4 |
| 41 | + * TODO(developer): Replace this variable with your Google Analytics 4 |
29 | 42 | * property ID before running the sample. |
30 | 43 | */ |
31 | | -// $property_id = 'YOUR-GA4-PROPERTY-ID'; |
| 44 | +$property_id = 'YOUR-GA4-PROPERTY-ID'; |
32 | 45 |
|
| 46 | +// [START google_analytics_data_initialize] |
| 47 | +// Using a default constructor instructs the client to use the credentials |
| 48 | +// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable. |
| 49 | +$client = new BetaAnalyticsDataClient(); |
| 50 | +// [END google_analytics_data_initialize] |
33 | 51 |
|
| 52 | +// [START google_analytics_data_run_report] |
34 | 53 | // Make an API call. |
35 | | -$client = new AlphaAnalyticsDataClient(); |
36 | 54 | $response = $client->runReport([ |
37 | | - 'entity' => new Entity([ |
38 | | - 'property_id' => $property_id |
39 | | - ]), |
| 55 | + 'property' => 'properties/' . $property_id, |
40 | 56 | 'dateRanges' => [ |
41 | 57 | new DateRange([ |
42 | 58 | 'start_date' => '2020-03-31', |
|
54 | 70 | ]) |
55 | 71 | ] |
56 | 72 | ]); |
| 73 | +// [END google_analytics_data_run_report] |
57 | 74 |
|
| 75 | +// [START google_analytics_data_run_report_response] |
58 | 76 | // Print results of an API call. |
59 | 77 | print 'Report result: ' . PHP_EOL; |
60 | 78 |
|
61 | 79 | foreach ($response->getRows() as $row) { |
62 | 80 | print $row->getDimensionValues()[0]->getValue() |
63 | 81 | . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL; |
| 82 | +// [END google_analytics_data_run_report_response] |
64 | 83 | } |
65 | | - |
66 | | - |
67 | 84 | // [END analytics_data_quickstart] |
0 commit comments