Skip to content

Commit 26be685

Browse files
authored
feat(analyticsdata): adds samples for run_report with date ranges (GoogleCloudPlatform#1714)
1 parent ac4efb8 commit 26be685

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-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 usage of
20+
* date ranges in a report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.date_ranges
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_date_ranges.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_date_ranges]
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 using two date ranges.
41+
*/
42+
function run_report_with_date_ranges(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+
'dateRanges' => [
51+
new DateRange([
52+
'start_date' => '2019-08-01',
53+
'end_date' => '2019-08-14',
54+
]),
55+
new DateRange([
56+
'start_date' => '2020-08-01',
57+
'end_date' => '2020-08-14',
58+
]),
59+
],
60+
'dimensions' => [new Dimension(['name' => 'platform'])],
61+
'metrics' => [new Metric(['name' => 'activeUsers'])],
62+
]);
63+
64+
printRunReportResponseWithDateRanges($response);
65+
}
66+
67+
/**
68+
* Print results of a runReport call.
69+
* @param RunReportResponse $response
70+
*/
71+
function printRunReportResponseWithDateRanges(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_date_ranges]
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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
* date ranges in a report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange#FIELDS.name
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_named_date_ranges.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_named_date_ranges]
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 using named date ranges.
41+
*/
42+
function run_report_with_named_date_ranges(string $propertyId)
43+
{
44+
// Create and 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+
'dateRanges' => [
51+
new DateRange([
52+
'start_date' => '2020-01-01',
53+
'end_date' => '2020-01-31',
54+
'name' => 'year_ago',
55+
]),
56+
new DateRange([
57+
'start_date' => '2021-01-01',
58+
'end_date' => '2021-01-31',
59+
'name' => 'current_year',
60+
]),
61+
],
62+
'dimensions' => [new Dimension(['name' => 'country'])],
63+
'metrics' => [new Metric(['name' => 'sessions'])],
64+
]);
65+
66+
printRunReportResponseWithNamedDateRanges($response);
67+
}
68+
69+
/**
70+
* Print results of a runReport call.
71+
* @param RunReportResponse $response
72+
*/
73+
function printRunReportResponseWithNamedDateRanges(RunReportResponse $response)
74+
{
75+
// [START analyticsdata_print_run_report_response_header]
76+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
77+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
78+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
79+
}
80+
foreach ($response->getMetricHeaders() as $metricHeader) {
81+
printf(
82+
'Metric header name: %s (%s)' . PHP_EOL,
83+
$metricHeader->getName(),
84+
MetricType::name($metricHeader->getType())
85+
);
86+
}
87+
// [END analyticsdata_print_run_report_response_header]
88+
89+
// [START analyticsdata_print_run_report_response_rows]
90+
print 'Report result: ' . PHP_EOL;
91+
92+
foreach ($response->getRows() as $row) {
93+
printf(
94+
'%s %s' . PHP_EOL,
95+
$row->getDimensionValues()[0]->getValue(),
96+
$row->getMetricValues()[0]->getValue()
97+
);
98+
}
99+
// [END analyticsdata_print_run_report_response_rows]
100+
}
101+
// [END analyticsdata_run_report_with_named_date_ranges]
102+
103+
// The following 2 lines are only needed to run the samples
104+
require_once __DIR__ . '/../../testing/sample_helpers.php';
105+
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,22 @@ public function testClientFromJsonCredentials()
5151
}
5252
}
5353

54+
public function testRunReportWithNamedDateRanges()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('run_report_with_named_date_ranges', [$propertyId]);
58+
59+
$this->assertStringContainsString('Report result', $output);
60+
}
61+
62+
public function testRunReportWithDateRanges()
63+
{
64+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
65+
$output = $this->runFunctionSnippet('run_report_with_date_ranges', [$propertyId]);
66+
67+
$this->assertStringContainsString('Report result', $output);
68+
}
69+
5470
public function testRunReportWithCohorts()
5571
{
5672
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');

0 commit comments

Comments
 (0)