|
| 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 | + * Google Analytics Data API sample application retrieving dimension and metrics |
| 20 | + * metadata. |
| 21 | + * See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata |
| 22 | + * for more information. |
| 23 | + * Usage: |
| 24 | + * composer update |
| 25 | + * php get_metadata_by_property_id.php |
| 26 | + */ |
| 27 | + |
| 28 | +namespace Google\Cloud\Samples\Analytics\Data; |
| 29 | + |
| 30 | +// [START analyticsdata_get_metadata_by_property_id] |
| 31 | +use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; |
| 32 | +use Google\Analytics\Data\V1beta\Metadata; |
| 33 | +use Google\ApiCore\ApiException; |
| 34 | + |
| 35 | +/** |
| 36 | + * Retrieves dimensions and metrics available for a Google Analytics 4 |
| 37 | + * property, including custom fields. |
| 38 | + * @param string $propertyId Your GA-4 Property ID |
| 39 | + */ |
| 40 | +function get_metadata_by_property_id(string $propertyId) |
| 41 | +{ |
| 42 | + // Create an instance of the Google Analytics Data API client library. |
| 43 | + $client = new BetaAnalyticsDataClient(); |
| 44 | + |
| 45 | + $formattedName = sprintf('properties/%s/metadata', $propertyId); |
| 46 | + |
| 47 | + // Make an API call. |
| 48 | + try { |
| 49 | + $response = $client->getMetadata($formattedName); |
| 50 | + } catch (ApiException $ex) { |
| 51 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 52 | + } |
| 53 | + |
| 54 | + printf( |
| 55 | + 'Dimensions and metrics available for Google Analytics 4 property' |
| 56 | + . ' %s (including custom fields):' . PHP_EOL, |
| 57 | + $propertyId |
| 58 | + ); |
| 59 | + printGetMetadataByPropertyId($response); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Print results of a getMetadata call. |
| 64 | + * @param Metadata $response |
| 65 | + */ |
| 66 | +function printGetMetadataByPropertyId(Metadata $response) |
| 67 | +{ |
| 68 | + // [START analyticsdata_print_get_metadata_response] |
| 69 | + foreach ($response->getDimensions() as $dimension) { |
| 70 | + print('DIMENSION' . PHP_EOL); |
| 71 | + printf( |
| 72 | + '%s (%s): %s' . PHP_EOL, |
| 73 | + $dimension->getApiName(), |
| 74 | + $dimension->getUiName(), |
| 75 | + $dimension->getDescription(), |
| 76 | + ); |
| 77 | + printf( |
| 78 | + 'custom definition: %s' . PHP_EOL, |
| 79 | + $dimension->getCustomDefinition() ? 'true' : 'false' |
| 80 | + ); |
| 81 | + if ($dimension->getDeprecatedApiNames()->count() > 0) { |
| 82 | + print('Deprecated API names: '); |
| 83 | + foreach ($dimension->getDeprecatedApiNames() as $name) { |
| 84 | + print($name . ','); |
| 85 | + } |
| 86 | + print(PHP_EOL); |
| 87 | + } |
| 88 | + print(PHP_EOL); |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($response->getMetrics() as $metric) { |
| 92 | + print('METRIC' . PHP_EOL); |
| 93 | + printf( |
| 94 | + '%s (%s): %s' . PHP_EOL, |
| 95 | + $metric->getApiName(), |
| 96 | + $metric->getUiName(), |
| 97 | + $metric->getDescription(), |
| 98 | + ); |
| 99 | + printf( |
| 100 | + 'custom definition: %s' . PHP_EOL, |
| 101 | + $metric->getCustomDefinition() ? 'true' : 'false' |
| 102 | + ); |
| 103 | + if ($metric->getDeprecatedApiNames()->count() > 0) { |
| 104 | + print('Deprecated API names: '); |
| 105 | + foreach ($metric->getDeprecatedApiNames() as $name) { |
| 106 | + print($name . ','); |
| 107 | + } |
| 108 | + print(PHP_EOL); |
| 109 | + } |
| 110 | + print(PHP_EOL); |
| 111 | + } |
| 112 | + // [END analyticsdata_print_get_metadata_response] |
| 113 | +} |
| 114 | +// [END analyticsdata_get_metadata_by_property_id] |
| 115 | + |
| 116 | +// The following 2 lines are only needed to run the samples |
| 117 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 118 | +return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments