Skip to content

Commit 4126e73

Browse files
authored
feat: [AnalyticsData] add samples for metadata requests (GoogleCloudPlatform#1745)
1 parent 079695b commit 4126e73

File tree

3 files changed

+252
-0
lines changed

3 files changed

+252
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_common_metadata.php
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_get_common_metadata]
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 all Google Analytics 4 properties.
37+
*/
38+
function get_common_metadata()
39+
{
40+
// Create an instance of the Google Analytics Data API client library.
41+
$client = new BetaAnalyticsDataClient();
42+
43+
/**
44+
* Set the Property ID to 0 for dimensions and metrics common
45+
* to all properties. In this special mode, this method will
46+
* not return custom dimensions and metrics.
47+
*/
48+
$propertyId = 0;
49+
$formattedName = sprintf('properties/%s/metadata', $propertyId);
50+
51+
// Make an API call.
52+
try {
53+
$response = $client->getMetadata($formattedName);
54+
} catch (ApiException $ex) {
55+
printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage());
56+
}
57+
58+
print('Dimensions and metrics available for all Google Analytics 4 properties:');
59+
printGetCommonMetadata($response);
60+
}
61+
62+
/**
63+
* Print results of a getMetadata call.
64+
* @param Metadata $response
65+
*/
66+
function printGetCommonMetadata(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_common_metadata]
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);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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);

analyticsdata/test/analyticsDataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function testClientFromJsonCredentials()
5151
}
5252
}
5353

54+
public function testGetCommonMetadata()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('get_common_metadata');
58+
59+
$this->assertStringContainsString('Dimensions and metrics', $output);
60+
}
61+
62+
public function testGetMetadataByPropertyId()
63+
{
64+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
65+
$output = $this->runFunctionSnippet('get_metadata_by_property_id', [$propertyId]);
66+
67+
$this->assertStringContainsString('Dimensions and metrics', $output);
68+
}
69+
5470
public function testRunRealtimeReport()
5571
{
5672
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');

0 commit comments

Comments
 (0)