Skip to content

Commit bd96cc9

Browse files
anweshanbshaffer
andauthored
feat(analyticsdata): add samples for run_report with multiple dimensions and metrics (GoogleCloudPlatform#1737)
Co-authored-by: Brent Shaffer <[email protected]>
1 parent 26be685 commit bd96cc9

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 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.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_multiple_dimensions.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_multiple_dimensions]
31+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
32+
use Google\Analytics\Data\V1beta\DateRange;
33+
use Google\Analytics\Data\V1beta\Dimension;
34+
use Google\Analytics\Data\V1beta\Metric;
35+
use Google\Analytics\Data\V1beta\MetricType;
36+
use Google\Analytics\Data\V1beta\RunReportResponse;
37+
38+
/**
39+
* @param string $propertyID Your GA-4 Property ID
40+
* Runs a report of active users grouped by three dimensions.
41+
*/
42+
function run_report_with_multiple_dimensions(string $propertyId)
43+
{
44+
// Create an instance of the Google Analytics Data API client library.
45+
$client = new BetaAnalyticsDataClient();
46+
47+
// Make an API call.
48+
$response = $client->runReport([
49+
'property' => 'properties/' . $propertyId,
50+
'dimensions' => [
51+
new Dimension(['name' => 'country']),
52+
new Dimension(['name' => 'region']),
53+
new Dimension(['name' => 'city']),
54+
],
55+
'metrics' => [new Metric(['name' => 'activeUsers'])],
56+
'dateRanges' => [
57+
new DateRange([
58+
'start_date' => '7daysAgo',
59+
'end_date' => 'today',
60+
])
61+
],
62+
]);
63+
64+
printRunReportResponseWithMultipleDimensions($response);
65+
}
66+
67+
/**
68+
* Print results of a runReport call.
69+
* @param RunReportResponse $response
70+
*/
71+
function printRunReportResponseWithMultipleDimensions(RunReportResponse $response)
72+
{
73+
// [START analyticsdata_print_run_report_response_header]
74+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
75+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
76+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
77+
}
78+
foreach ($response->getMetricHeaders() as $metricHeader) {
79+
printf(
80+
'Metric header name: %s (%s)' . PHP_EOL,
81+
$metricHeader->getName(),
82+
MetricType::name($metricHeader->getType())
83+
);
84+
}
85+
// [END analyticsdata_print_run_report_response_header]
86+
87+
// [START analyticsdata_print_run_report_response_rows]
88+
print 'Report result: ' . PHP_EOL;
89+
90+
foreach ($response->getRows() as $row) {
91+
printf(
92+
'%s %s' . PHP_EOL,
93+
$row->getDimensionValues()[0]->getValue(),
94+
$row->getMetricValues()[0]->getValue()
95+
);
96+
}
97+
// [END analyticsdata_print_run_report_response_rows]
98+
}
99+
// [END analyticsdata_run_report_with_multiple_dimensions]
100+
101+
// The following 2 lines are only needed to run the samples
102+
require_once __DIR__ . '/../../testing/sample_helpers.php';
103+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 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.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_multiple_metrics.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_multiple_metrics]
31+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
32+
use Google\Analytics\Data\V1beta\DateRange;
33+
use Google\Analytics\Data\V1beta\Dimension;
34+
use Google\Analytics\Data\V1beta\Metric;
35+
use Google\Analytics\Data\V1beta\MetricType;
36+
use Google\Analytics\Data\V1beta\RunReportResponse;
37+
38+
/**
39+
* @param string $propertyID Your GA-4 Property ID
40+
* Runs a report of active users grouped by three metrics.
41+
*/
42+
function run_report_with_multiple_metrics(string $propertyId)
43+
{
44+
// Create an instance of the Google Analytics Data API client library.
45+
$client = new BetaAnalyticsDataClient();
46+
47+
// Make an API call.
48+
$response = $client->runReport([
49+
'property' => 'properties/' . $propertyId,
50+
'dimensions' => [new Dimension(['name' => 'date'])],
51+
'metrics' => [
52+
new Metric(['name' => 'activeUsers']),
53+
new Metric(['name' => 'newUsers']),
54+
new Metric(['name' => 'totalRevenue'])
55+
],
56+
'dateRanges' => [
57+
new DateRange([
58+
'start_date' => '7daysAgo',
59+
'end_date' => 'today',
60+
])
61+
],
62+
]);
63+
64+
printRunReportResponseWithMultipleMetrics($response);
65+
}
66+
67+
/**
68+
* Print results of a runReport call.
69+
* @param RunReportResponse $response
70+
*/
71+
function printRunReportResponseWithMultipleMetrics(RunReportResponse $response)
72+
{
73+
// [START analyticsdata_print_run_report_response_header]
74+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
75+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
76+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
77+
}
78+
foreach ($response->getMetricHeaders() as $metricHeader) {
79+
printf(
80+
'Metric header name: %s (%s)' . PHP_EOL,
81+
$metricHeader->getName(),
82+
MetricType::name($metricHeader->getType())
83+
);
84+
}
85+
// [END analyticsdata_print_run_report_response_header]
86+
87+
// [START analyticsdata_print_run_report_response_rows]
88+
print 'Report result: ' . PHP_EOL;
89+
90+
foreach ($response->getRows() as $row) {
91+
printf(
92+
'%s %s' . PHP_EOL,
93+
$row->getDimensionValues()[0]->getValue(),
94+
$row->getMetricValues()[0]->getValue()
95+
);
96+
}
97+
// [END analyticsdata_print_run_report_response_rows]
98+
}
99+
// [END analyticsdata_run_report_with_multiple_metrics]
100+
101+
// The following 2 lines are only needed to run the samples
102+
require_once __DIR__ . '/../../testing/sample_helpers.php';
103+
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,14 @@ public function testClientFromJsonCredentials()
5151
}
5252
}
5353

54+
public function testRunReportWithMultipleMetrics()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('run_report_with_multiple_metrics', [$propertyId]);
58+
59+
$this->assertStringContainsString('Report result', $output);
60+
}
61+
5462
public function testRunReportWithNamedDateRanges()
5563
{
5664
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
@@ -59,6 +67,14 @@ public function testRunReportWithNamedDateRanges()
5967
$this->assertStringContainsString('Report result', $output);
6068
}
6169

70+
public function testRunReportWithMultipleDimensions()
71+
{
72+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
73+
$output = $this->runFunctionSnippet('run_report_with_multiple_dimensions', [$propertyId]);
74+
75+
$this->assertStringContainsString('Report result', $output);
76+
}
77+
6278
public function testRunReportWithDateRanges()
6379
{
6480
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');

0 commit comments

Comments
 (0)