Skip to content

Commit 8cfb092

Browse files
authored
feat: [AnalyticsData] add sample for run_report with aggregations (GoogleCloudPlatform#1707)
1 parent 2ad4b3f commit 8cfb092

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

analyticsdata/src/run_report.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
use Google\Analytics\Data\V1beta\MetricType;
3333
use Google\Analytics\Data\V1beta\RunReportResponse;
3434

35+
/**
36+
* @param string $propertyID Your GA-4 Property ID
37+
*/
3538
function run_report(string $propertyId)
3639
{
3740
// [START analyticsdata_initialize]
@@ -63,7 +66,10 @@ function run_report(string $propertyId)
6366
printRunReportResponse($response);
6467
}
6568

66-
// Print results of a runReport call.
69+
/**
70+
* Print results of a runReport call.
71+
* @param RunReportResponse $response
72+
*/
6773
function printRunReportResponse(RunReportResponse $response)
6874
{
6975
// [START analyticsdata_print_run_report_response_header]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 usage of
20+
* metric aggregations in a report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.metric_aggregations
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_aggregations.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_aggregations]
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\MetricAggregation;
37+
use Google\Analytics\Data\V1beta\RunReportResponse;
38+
39+
/**
40+
* @param string $propertyID Your GA-4 Property ID
41+
* Runs a report which includes total, maximum and minimum values
42+
* for each metric.
43+
*/
44+
function run_report_with_aggregations(string $propertyId)
45+
{
46+
// [START analyticsdata_initialize]
47+
// Imports the Google Analytics Data API client library.
48+
$client = new BetaAnalyticsDataClient();
49+
// [END analyticsdata_initialize]
50+
51+
// Make an API call.
52+
$response = $client->runReport([
53+
'property' => 'properties/' . $propertyId,
54+
'dimensions' => [new Dimension(['name' => 'country'])],
55+
'metrics' => [new Metric(['name' => 'sessions'])],
56+
'dateRanges' => [
57+
new DateRange([
58+
'start_date' => '365daysAgo',
59+
'end_date' => 'today',
60+
]),
61+
],
62+
'metricAggregations' => [
63+
MetricAggregation::TOTAL,
64+
MetricAggregation::MAXIMUM,
65+
MetricAggregation::MINIMUM
66+
]
67+
]);
68+
69+
printRunReportResponseWithAggregations($response);
70+
}
71+
72+
/**
73+
* Print results of a runReport call.
74+
* @param RunReportResponse $response
75+
*/
76+
function printRunReportResponseWithAggregations($response)
77+
{
78+
// [START analyticsdata_print_run_report_response_header]
79+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
80+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
81+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
82+
}
83+
foreach ($response->getMetricHeaders() as $metricHeader) {
84+
printf(
85+
'Metric header name: %s (%s)' . PHP_EOL,
86+
$metricHeader->getName(),
87+
MetricType::name($metricHeader->getType())
88+
);
89+
}
90+
// [END analyticsdata_print_run_report_response_header]
91+
92+
// [START analyticsdata_print_run_report_response_rows]
93+
print 'Report result: ' . PHP_EOL;
94+
95+
foreach ($response->getRows() as $row) {
96+
printf(
97+
'%s %s' . PHP_EOL,
98+
$row->getDimensionValues()[0]->getValue(),
99+
$row->getMetricValues()[0]->getValue()
100+
);
101+
}
102+
// [END analyticsdata_print_run_report_response_rows]
103+
}
104+
// [END analyticsdata_run_report_with_aggregations]
105+
106+
// The following 2 lines are only needed to run the samples
107+
require_once __DIR__ . '/../../testing/sample_helpers.php';
108+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

analyticsdata/test/analyticsDataTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,12 @@ public function testClientFromJsonCredentials()
5050
$this->assertStringContainsString('does-not-exist.json', $ex->getMessage());
5151
}
5252
}
53+
54+
public function testRunReportWithAggregations()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('run_report_with_aggregations', [$propertyId]);
58+
59+
$this->assertRegExp('/Report result/', $output);
60+
}
5361
}

0 commit comments

Comments
 (0)