|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2024 Google LLC. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Google Analytics Admin API sample application which prints Google Analytics 4 |
| 21 | + * properties under the specified parent account that are available to the |
| 22 | + * current user. |
| 23 | + * |
| 24 | + * See |
| 25 | + * https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/list |
| 26 | + * for more information. |
| 27 | + */ |
| 28 | + |
| 29 | +namespace Google\Analytics\Admin\Samples; |
| 30 | + |
| 31 | +// [START analyticsadmin_properties_list] |
| 32 | +use Google\Analytics\Admin\V1beta\Client\AnalyticsAdminServiceClient; |
| 33 | +use Google\Analytics\Admin\V1beta\ListPropertiesRequest; |
| 34 | + |
| 35 | +/** |
| 36 | + * @param string $accountId Your GA-4 Account ID |
| 37 | + */ |
| 38 | +function properties_list(string $accountId) |
| 39 | +{ |
| 40 | + // Create an instance of the Google Analytics Admin API client library. |
| 41 | + $client = new AnalyticsAdminServiceClient(); |
| 42 | + |
| 43 | + // Make an API call. |
| 44 | + $request = (new ListPropertiesRequest()) |
| 45 | + ->setFilter('parent:accounts/' . $accountId) |
| 46 | + ->setShowDeleted(true); |
| 47 | + $response = $client->listProperties($request); |
| 48 | + |
| 49 | + print 'Result:' . PHP_EOL; |
| 50 | + $i = 0; |
| 51 | + foreach($response->iterateAllElements() as $property) { |
| 52 | + printf('Property #%d resource name: %s, parent: %s, display name: "%s", currency: %s, time zone: %s, create time: %s, update time: %s%s', |
| 53 | + $i++, |
| 54 | + $property->getName(), |
| 55 | + $property->getParent(), |
| 56 | + $property->getDisplayName(), |
| 57 | + $property->getCurrencyCode(), |
| 58 | + $property->getTimeZone(), |
| 59 | + $property->getCreateTime()->getSeconds(), |
| 60 | + $property->getUpdateTime()->getSeconds(), |
| 61 | + PHP_EOL, |
| 62 | + ); |
| 63 | + } |
| 64 | +} |
| 65 | +// [END analyticsadmin_properties_list] |
| 66 | + |
| 67 | +// The following 2 lines are only needed to run the samples |
| 68 | +require_once __DIR__ . '/../testing/sample_helpers.php'; |
| 69 | +return \Google\Analytics\Admin\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments