Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .kokoro/secrets.sh.enc
Binary file not shown.
37 changes: 37 additions & 0 deletions analyticsdata/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2022 Google Inc.

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.
-->
<phpunit bootstrap="../testing/bootstrap.php">
<testsuites>
<testsuite name="Google Analytics Data API Tests">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
94 changes: 0 additions & 94 deletions analyticsdata/quickstart_json_credentials.php

This file was deleted.

51 changes: 51 additions & 0 deletions analyticsdata/src/client_from_json_credentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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.
*/

namespace Google\Cloud\Samples\Analytics\Data;

/**
* This file is to be used as an example only!
*
* Usage:
* ```
* $credentialsJsonPath = '/path/to/credentials.json';
* $analyticsDataClient = require 'src/client_from_json_credentials.php';
* ```
*/
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;

// [START analyticsdata_json_credentials_initialize]
/**
* @param string $credentialsJsonPath Valid path to the credentials.json file for your service
* account downloaded from the Cloud Console.
* Example: "/path/to/credentials.json"
*/
function client_from_json_credentials(string $credentialsJsonPath)
{
// Explicitly use service account credentials by specifying
// the private key file.
$client = new BetaAnalyticsDataClient([
'credentials' => $credentialsJsonPath
]);

return $client;
}
// [END analyticsdata_json_credentials_initialize]

// 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);
59 changes: 22 additions & 37 deletions analyticsdata/run_report.php → analyticsdata/src/run_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,56 @@
* limitations under the License.
*/

/*

"""Google Analytics Data API sample application demonstrating the creation
of a basic report.
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
for more information.
"""

Before you start the application, please review the comments starting with
"TODO(developer)" and update the code to use the correct values.

Usage:
composer update
php run_report.php
/**
* Google Analytics Data API sample application demonstrating the creation
* of a basic report.
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
* for more information.
*/

// [START analyticsdata_run_report]
require 'vendor/autoload.php';
namespace Google\Cloud\Samples\Analytics\Data;

// [START analyticsdata_run_report]
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;
use Google\Analytics\Data\V1beta\MetricType;
use Google\Analytics\Data\V1beta\RunReportResponse;

function runReport()
function run_report(string $propertyId)
{
/**
* TODO(developer): Replace this variable with your Google Analytics 4
* property ID before running the sample.
*/
$property_id = 'YOUR-GA4-PROPERTY-ID';

// [START analyticsdata_initialize]
//Imports the Google Analytics Data API client library.'

$client = new BetaAnalyticsDataClient();

// [END analyticsdata_initialize]

// Make an API call.
$response = $client->runReport([
'property' => 'properties/' . $property_id,
'property' => 'properties/' . $propertyId,
'dateRanges' => [
new DateRange([
'start_date' => '2020-09-01',
'end_date' => '2020-09-15',
]),
],
'dimensions' => [new Dimension(
[
'dimensions' => [
new Dimension([
'name' => 'country',
]
),
]),
],
'metrics' => [new Metric(
[
'metrics' => [
new Metric([
'name' => 'activeUsers',
]
)
]
]),
],
]);

printRunReportResponse($response);
}

// Print results of a runReport call.
function printRunReportResponse($response)
function printRunReportResponse(RunReportResponse $response)
{
// [START analyticsdata_print_run_report_response_header]
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
Expand All @@ -109,4 +91,7 @@ function printRunReportResponse($response)
// [END analyticsdata_print_run_report_response_rows]
}
// [END analyticsdata_run_report]
runReport();

// 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);
53 changes: 53 additions & 0 deletions analyticsdata/test/analyticsDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright 2022 Google Inc.
*
* 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.
*/

namespace Google\Cloud\Samples\Analytics\Data\Tests;

use Google\ApiCore\ValidationException;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;

class analyticsDataTest extends TestCase
{
use TestTrait;

public function testRunReport()
{
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
$output = $this->runFunctionSnippet('run_report', [$propertyId]);

$this->assertRegExp('/Report result/', $output);
}

public function testClientFromJsonCredentials()
{
$jsonCredentials = self::requireEnv('GOOGLE_APPLICATION_CREDENTIALS');
$this->runFunctionSnippet('client_from_json_credentials', [$jsonCredentials]);

$client = $this->getLastReturnedSnippetValue();

$this->assertInstanceOf(BetaAnalyticsDataClient::class, $client);

try {
$this->runFunctionSnippet('client_from_json_credentials', ['does-not-exist.json']);
$this->fail('Non-existant json credentials should throw exception');
} catch (ValidationException $ex) {
$this->assertStringContainsString('does-not-exist.json', $ex->getMessage());
}
}
}
42 changes: 0 additions & 42 deletions analyticsdata/test/quickstartJsonCredentialsTest.php

This file was deleted.

Loading