-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(analyticsdata): add samples for metadata requests #1745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4ed4112
created blank php files
anweshan e32c555
moved files to analytics data
anweshan b815b97
changed file names of php files in analytics data
anweshan 4f526c7
changed php file names in analytics data
anweshan dc172c5
wrote initial test php code in analytics data
anweshan 9b75a52
fixed typos
anweshan 37125a5
fixed typos
anweshan 6ced3f0
wrote sample code
anweshan 6241b7c
Merge branch 'GoogleCloudPlatform:master' into samples-8
anweshan 2f5805e
Merge branch 'GoogleCloudPlatform:master' into samples-8
anweshan db10889
restructures files
anweshan f9a0740
update file
anweshan 82d7fa5
Updates metadata file
anweshan dcd0cd5
Update analyticsdata/src/get_common_metadata.php
anweshan 16b5377
Update to metadata file
anweshan 5007c15
updates metadata files to be complete
anweshan ade7334
fix merge conflicts
anweshan d8af1ba
Merge branch 'main' into samples-8
anweshan e3d251d
fix kokoro
anweshan b2377de
Merge branch 'samples-8' of github.com:anweshan/php-docs-samples into…
anweshan 86fd2af
Apply suggestions from code review
bshaffer 7ae685a
Update analyticsdata/test/analyticsDataTest.php
anweshan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2022 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Google Analytics Data API sample application retrieving dimension and metrics | ||
| * metadata. | ||
| * See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata | ||
| * for more information. | ||
| * Usage: | ||
| * composer update | ||
| * php get_common_metadata.php | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Analytics\Data; | ||
|
|
||
| // [START analyticsdata_get_common_metadata] | ||
| use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; | ||
| use Google\Analytics\Data\V1beta\Metadata; | ||
| use Google\ApiCore\ApiException; | ||
|
|
||
| /** | ||
| * Retrieves dimensions and metrics available for all Google Analytics 4 properties. | ||
| */ | ||
| function get_common_metadata() | ||
| { | ||
| // Create an instance of the Google Analytics Data API client library. | ||
| $client = new BetaAnalyticsDataClient(); | ||
|
|
||
| /** | ||
| * Set the Property ID to 0 for dimensions and metrics common | ||
| * to all properties. In this special mode, this method will | ||
| * not return custom dimensions and metrics. | ||
| */ | ||
| $propertyId = 0; | ||
| $formattedName = sprintf('properties/%s/metadata', $propertyId); | ||
|
|
||
| // Make an API call. | ||
| try { | ||
| $response = $client->getMetadata($formattedName); | ||
| } catch (ApiException $ex) { | ||
| printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); | ||
| } | ||
|
|
||
| print('Dimensions and metrics available for all Google Analytics 4 properties:'); | ||
| printGetCommonMetadata($response); | ||
| } | ||
|
|
||
| /** | ||
| * Print results of a getMetadata call. | ||
| * @param Metadata $response | ||
| */ | ||
| function printGetCommonMetadata(Metadata $response) | ||
| { | ||
| // [START analyticsdata_print_get_metadata_response] | ||
| foreach ($response->getDimensions() as $dimension) { | ||
| print('DIMENSION' . PHP_EOL); | ||
| printf( | ||
| '%s (%s): %s' . PHP_EOL, | ||
| $dimension->getApiName(), | ||
| $dimension->getUiName(), | ||
| $dimension->getDescription(), | ||
| ); | ||
| printf( | ||
| 'custom definition: %s' . PHP_EOL, | ||
| $dimension->getCustomDefinition()? 'true' : 'false' | ||
| ); | ||
| if ($dimension->getDeprecatedApiNames()->count() > 0) { | ||
| print('Deprecated API names: '); | ||
| foreach ($dimension->getDeprecatedApiNames() as $name) { | ||
| print($name . ','); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
|
|
||
| foreach ($response->getMetrics() as $metric) { | ||
| print('METRIC' . PHP_EOL); | ||
| printf( | ||
| '%s (%s): %s' . PHP_EOL, | ||
| $metric->getApiName(), | ||
| $metric->getUiName(), | ||
| $metric->getDescription(), | ||
| ); | ||
| printf( | ||
| 'custom definition: %s' . PHP_EOL, | ||
| $metric->getCustomDefinition()? 'true' : 'false' | ||
| ); | ||
| if ($metric->getDeprecatedApiNames()->count() > 0) { | ||
| print('Deprecated API names: '); | ||
| foreach ($metric->getDeprecatedApiNames() as $name) { | ||
| print($name . ','); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| // [END analyticsdata_print_get_metadata_response] | ||
| } | ||
| // [END analyticsdata_get_common_metadata] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2022 Google LLC. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Google Analytics Data API sample application retrieving dimension and metrics | ||
| * metadata. | ||
| * See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata | ||
| * for more information. | ||
| * Usage: | ||
| * composer update | ||
| * php get_metadata_by_property_id.php | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\Analytics\Data; | ||
|
|
||
| // [START analyticsdata_get_metadata_by_property_id] | ||
| use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; | ||
| use Google\Analytics\Data\V1beta\Metadata; | ||
| use Google\ApiCore\ApiException; | ||
|
|
||
| /** | ||
| * Retrieves dimensions and metrics available for a Google Analytics 4 | ||
| * property, including custom fields. | ||
| * @param string $propertyId Your GA-4 Property ID | ||
| */ | ||
| function get_metadata_by_property_id(string $propertyId) | ||
| { | ||
| // Create an instance of the Google Analytics Data API client library. | ||
| $client = new BetaAnalyticsDataClient(); | ||
|
|
||
| $formattedName = sprintf('properties/%s/metadata', $propertyId); | ||
|
|
||
| // Make an API call. | ||
| try { | ||
| $response = $client->getMetadata($formattedName); | ||
| } catch (ApiException $ex) { | ||
| printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); | ||
| } | ||
|
|
||
| printf( | ||
| 'Dimensions and metrics available for Google Analytics 4 property' | ||
| . ' %s (including custom fields):' . PHP_EOL, | ||
| $propertyId | ||
| ); | ||
| printGetMetadataByPropertyId($response); | ||
| } | ||
|
|
||
| /** | ||
| * Print results of a getMetadata call. | ||
| * @param Metadata $response | ||
| */ | ||
| function printGetMetadataByPropertyId(Metadata $response) | ||
| { | ||
| // [START analyticsdata_print_get_metadata_response] | ||
| foreach ($response->getDimensions() as $dimension) { | ||
| print('DIMENSION' . PHP_EOL); | ||
| printf( | ||
| '%s (%s): %s' . PHP_EOL, | ||
| $dimension->getApiName(), | ||
| $dimension->getUiName(), | ||
| $dimension->getDescription(), | ||
| ); | ||
| printf( | ||
| 'custom definition: %s' . PHP_EOL, | ||
| $dimension->getCustomDefinition() ? 'true' : 'false' | ||
| ); | ||
| if ($dimension->getDeprecatedApiNames()->count() > 0) { | ||
| print('Deprecated API names: '); | ||
| foreach ($dimension->getDeprecatedApiNames() as $name) { | ||
| print($name . ','); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
|
|
||
| foreach ($response->getMetrics() as $metric) { | ||
| print('METRIC' . PHP_EOL); | ||
| printf( | ||
| '%s (%s): %s' . PHP_EOL, | ||
| $metric->getApiName(), | ||
| $metric->getUiName(), | ||
| $metric->getDescription(), | ||
| ); | ||
| printf( | ||
| 'custom definition: %s' . PHP_EOL, | ||
| $metric->getCustomDefinition() ? 'true' : 'false' | ||
| ); | ||
| if ($metric->getDeprecatedApiNames()->count() > 0) { | ||
| print('Deprecated API names: '); | ||
| foreach ($metric->getDeprecatedApiNames() as $name) { | ||
| print($name . ','); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| print(PHP_EOL); | ||
| } | ||
| // [END analyticsdata_print_get_metadata_response] | ||
| } | ||
| // [END analyticsdata_get_metadata_by_property_id] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.