Skip to content

Commit 1d6b3af

Browse files
authored
fix: [AnalyticsAdmin] standardize run_report sample (GoogleCloudPlatform#1676)
1 parent 1fc1b7b commit 1d6b3af

9 files changed

+166
-218
lines changed

.kokoro/secrets.sh.enc

0 Bytes
Binary file not shown.

analyticsdata/phpunit.xml.dist

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="Google Analytics Data API Tests">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./srcdirectory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
<php>
35+
<env name="PHPUNIT_TESTS" value="1"/>
36+
php>
37+
phpunit>

analyticsdata/quickstart_json_credentials.php

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
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+
namespace Google\Cloud\Samples\Analytics\Data;
19+
20+
/**
21+
* This file is to be used as an example only!
22+
*
23+
* Usage:
24+
* ```
25+
* $credentialsJsonPath = '/path/to/credentials.json';
26+
* $analyticsDataClient = require 'src/client_from_json_credentials.php';
27+
* ```
28+
*/
29+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
30+
31+
// [START analyticsdata_json_credentials_initialize]
32+
/**
33+
* @param string $credentialsJsonPath Valid path to the credentials.json file for your service
34+
* account downloaded from the Cloud Console.
35+
* Example: "/path/to/credentials.json"
36+
*/
37+
function client_from_json_credentials(string $credentialsJsonPath)
38+
{
39+
// Explicitly use service account credentials by specifying
40+
// the private key file.
41+
$client = new BetaAnalyticsDataClient([
42+
'credentials' => $credentialsJsonPath
43+
]);
44+
45+
return $client;
46+
}
47+
// [END analyticsdata_json_credentials_initialize]
48+
49+
// The following 2 lines are only needed to run the samples
50+
require_once __DIR__ . '/../../testing/sample_helpers.php';
51+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

analyticsdata/run_report.php renamed to analyticsdata/src/run_report.php

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,56 @@
1515
* limitations under the License.
1616
*/
1717

18-
/*
19-
20-
"""Google Analytics Data API sample application demonstrating the creation
21-
of a basic report.
22-
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
23-
for more information.
24-
"""
25-
26-
Before you start the application, please review the comments starting with
27-
"TODO(developer)" and update the code to use the correct values.
28-
29-
Usage:
30-
composer update
31-
php run_report.php
18+
/**
19+
* Google Analytics Data API sample application demonstrating the creation
20+
* of a basic report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
22+
* for more information.
3223
*/
3324

34-
// [START analyticsdata_run_report]
35-
require 'vendor/autoload.php';
25+
namespace Google\Cloud\Samples\Analytics\Data;
3626

27+
// [START analyticsdata_run_report]
3728
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
3829
use Google\Analytics\Data\V1beta\DateRange;
3930
use Google\Analytics\Data\V1beta\Dimension;
4031
use Google\Analytics\Data\V1beta\Metric;
4132
use Google\Analytics\Data\V1beta\MetricType;
33+
use Google\Analytics\Data\V1beta\RunReportResponse;
4234

43-
function runReport()
35+
function run_report(string $propertyId)
4436
{
45-
/**
46-
* TODO(developer): Replace this variable with your Google Analytics 4
47-
* property ID before running the sample.
48-
*/
49-
$property_id = 'YOUR-GA4-PROPERTY-ID';
50-
5137
// [START analyticsdata_initialize]
52-
//Imports the Google Analytics Data API client library.'
53-
5438
$client = new BetaAnalyticsDataClient();
5539

5640
// [END analyticsdata_initialize]
5741

5842
// Make an API call.
5943
$response = $client->runReport([
60-
'property' => 'properties/' . $property_id,
44+
'property' => 'properties/' . $propertyId,
6145
'dateRanges' => [
6246
new DateRange([
6347
'start_date' => '2020-09-01',
6448
'end_date' => '2020-09-15',
6549
]),
6650
],
67-
'dimensions' => [new Dimension(
68-
[
51+
'dimensions' => [
52+
new Dimension([
6953
'name' => 'country',
70-
]
71-
),
54+
]),
7255
],
73-
'metrics' => [new Metric(
74-
[
56+
'metrics' => [
57+
new Metric([
7558
'name' => 'activeUsers',
76-
]
77-
)
78-
]
59+
]),
60+
],
7961
]);
8062

8163
printRunReportResponse($response);
8264
}
8365

8466
// Print results of a runReport call.
85-
function printRunReportResponse($response)
67+
function printRunReportResponse(RunReportResponse $response)
8668
{
8769
// [START analyticsdata_print_run_report_response_header]
8870
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
@@ -109,4 +91,7 @@ function printRunReportResponse($response)
10991
// [END analyticsdata_print_run_report_response_rows]
11092
}
11193
// [END analyticsdata_run_report]
112-
runReport();
94+
95+
// The following 2 lines are only needed to run the samples
96+
require_once __DIR__ . '/../../testing/sample_helpers.php';
97+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/**
3+
* Copyright 2022 Google Inc.
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+
namespace Google\Cloud\Samples\Analytics\Data\Tests;
19+
20+
use Google\ApiCore\ValidationException;
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use PHPUnit\Framework\TestCase;
23+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
24+
25+
class analyticsDataTest extends TestCase
26+
{
27+
use TestTrait;
28+
29+
public function testRunReport()
30+
{
31+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
32+
$output = $this->runFunctionSnippet('run_report', [$propertyId]);
33+
34+
$this->assertRegExp('/Report result/', $output);
35+
}
36+
37+
public function testClientFromJsonCredentials()
38+
{
39+
$jsonCredentials = self::requireEnv('GOOGLE_APPLICATION_CREDENTIALS');
40+
$this->runFunctionSnippet('client_from_json_credentials', [$jsonCredentials]);
41+
42+
$client = $this->getLastReturnedSnippetValue();
43+
44+
$this->assertInstanceOf(BetaAnalyticsDataClient::class, $client);
45+
46+
try {
47+
$this->runFunctionSnippet('client_from_json_credentials', ['does-not-exist.json']);
48+
$this->fail('Non-existant json credentials should throw exception');
49+
} catch (ValidationException $ex) {
50+
$this->assertStringContainsString('does-not-exist.json', $ex->getMessage());
51+
}
52+
}
53+
}

analyticsdata/test/quickstartJsonCredentialsTest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)