Skip to content

Commit e0f9fbe

Browse files
committed
Add properties_list and test
1 parent f7d8757 commit e0f9fbe

File tree

5 files changed

+133
-23
lines changed

5 files changed

+133
-23
lines changed

CONTRIBUTING.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ accept your pull requests.
3535

3636
Write samples according to the [sample style guide](https://googlecloudplatform.github.io/samples-style-guide/).
3737

38-
## Testing your code changes.
38+
## Testing your code changes
3939

4040
### Install dependencies
4141

42-
To run the tests in a samples directory, you will need to install
43-
[Composer](http://getcomposer.org/doc/00-intro.md).
44-
45-
First install dependencies as described in the
46-
[README.md](google-analytics-data/README.md).
42+
Change into the directory of the project you want to test (either
43+
`google-analytics-admin` or `google-analytics-data`), configure
44+
[Composer](http://getcomposer.org/doc/00-intro.md) and install dependencies as
45+
described in the directory's `README.md`.
4746

4847
### Environment variables
4948
Some tests require specific environment variables to run. PHPUnit will skip the tests
@@ -54,12 +53,15 @@ to run against any sample project as follows:
5453
```
5554
export GOOGLE_PROJECT_ID=YOUR_PROJECT_ID
5655
export GA_TEST_PROPERTY_ID=YOUR_GA4_PROPERTY_ID
56+
# This value is only required by Admin API samples tests.
57+
export GA_TEST_ACCOUNT_ID=
5758
```
5859

5960
### Run the tests
6061

6162
Once the dependencies are installed and the environment variables set, you can run the
62-
tests in a samples directory.
63+
tests in a samples directory. For example:
64+
6365
```
6466
cd google-analytics-data
6567
# Execute the "phpunit" installed for the shared dependencies

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
PHP samples for [Google Analytics APIs][ga].
44

5-
Check out the [`README.md`](google-analytics-data/README.md) in the
6-
`google-analytics-data` directory to get started.
5+
Check out the `README.md` in one of the following directories to get started:
6+
7+
- Admin API: [README.md](google-analytics-admin/README.md)
8+
- Data API: [README.md](google-analytics-data/README.md)
79

810
## Contributing
911

google-analytics-admin/quickstart.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2021 Google LLC.
3+
* Copyright 2024 Google LLC.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,19 +15,20 @@
1515
* limitations under the License.
1616
*/
1717

18-
/* Google Analytics Admin API sample quickstart application.
19-
20-
This application demonstrates the usage of the Analytics Admin API to list
21-
all Google Analytics accounts available to the authenticated user, using
22-
service account credentials.
23-
24-
Before you start the application, please review the comments starting with
25-
"TODO(developer)" and update the code to use the correct values.
26-
27-
Usage:
28-
composer update
29-
php quickstart.php
30-
*/
18+
/**
19+
* Google Analytics Admin API sample quickstart application.
20+
*
21+
* This application demonstrates the usage of the Analytics Admin API to list
22+
* all Google Analytics accounts available to the authenticated user, using
23+
* service account credentials.
24+
*
25+
* Before you start the application, please review the comments starting with
26+
* "TODO(developer)" and update the code to use the correct values.
27+
*
28+
* Usage:
29+
* composer update
30+
* php quickstart.php
31+
*/
3132

3233
// [START analytics_admin_quickstart]
3334
require 'vendor/autoload.php';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2022 Google Inc.
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+
namespace Google\Analytics\Admin\Samples\Tests;
20+
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class AnalyticsAdminTest extends TestCase
25+
{
26+
use TestTrait;
27+
28+
public function testPropertiesList()
29+
{
30+
$accountId = self::requireEnv('GA_TEST_ACCOUNT_ID');
31+
$output = $this->runFunctionSnippet('properties_list', [$accountId]);
32+
33+
$this->assertStringContainsString('Result:', $output);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)