Skip to content

Commit c2b0655

Browse files
authored
feat: [AnalyticsData] add samples for run_realtime_report (GoogleCloudPlatform#1747)
1 parent c02b4c7 commit c2b0655

File tree

4 files changed

+309
-0
lines changed

4 files changed

+309
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 of
20+
* a realtime report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run__realtime_report.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_realtime_report]
31+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
32+
use Google\Analytics\Data\V1beta\Dimension;
33+
use Google\Analytics\Data\V1beta\Metric;
34+
use Google\Analytics\Data\V1beta\MetricType;
35+
use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
36+
37+
/**
38+
* Runs a realtime report on a Google Analytics 4 property.
39+
* @param string $propertyId Your GA-4 Property ID
40+
*/
41+
function run_realtime_report(string $propertyId)
42+
{
43+
// Create an instance of the Google Analytics Data API client library.
44+
$client = new BetaAnalyticsDataClient();
45+
46+
// Make an API call.
47+
$response = $client->runRealtimeReport([
48+
'property' => 'properties/' . $propertyId,
49+
'dimensions' => [new Dimension(['name' => 'country'])],
50+
'metrics' => [new Metric(['name' => 'activeUsers'])],
51+
]);
52+
53+
printRunRealtimeReportResponse($response);
54+
}
55+
56+
/**
57+
* Print results of a runRealtimeReport call.
58+
* @param RunRealtimeReportResponse $response
59+
*/
60+
function printRunRealtimeReportResponse(RunRealtimeReportResponse $response)
61+
{
62+
// [START analyticsdata_print_run_realtime_report_response_header]
63+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
64+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
65+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
66+
}
67+
foreach ($response->getMetricHeaders() as $metricHeader) {
68+
printf(
69+
'Metric header name: %s (%s)%s',
70+
$metricHeader->getName(),
71+
MetricType::name($metricHeader->getType()),
72+
PHP_EOL
73+
);
74+
}
75+
// [END analyticsdata_print_run_realtime_report_response_header]
76+
77+
// [START analyticsdata_print_run_realtime_report_response_rows]
78+
print 'Report result: ' . PHP_EOL;
79+
80+
foreach ($response->getRows() as $row) {
81+
printf(
82+
'%s %s' . PHP_EOL,
83+
$row->getDimensionValues()[0]->getValue(),
84+
$row->getMetricValues()[0]->getValue()
85+
);
86+
}
87+
// [END analyticsdata_print_run_realtime_report_response_rows]
88+
}
89+
// [END analyticsdata_run_realtime_report]
90+
91+
// The following 2 lines are only needed to run the samples
92+
require_once __DIR__ . '/../../testing/sample_helpers.php';
93+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 of
20+
* a realtime report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_realtime_report_with_multiple_dimensions.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_realtime_report_with_multiple_dimensions]
31+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
32+
use Google\Analytics\Data\V1beta\Dimension;
33+
use Google\Analytics\Data\V1beta\Metric;
34+
use Google\Analytics\Data\V1beta\MetricType;
35+
use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
36+
37+
/**
38+
* Runs a realtime report on a Google Analytics 4 property.
39+
* @param string $propertyId Your GA-4 Property ID
40+
*/
41+
function run_realtime_report_with_multiple_dimensions(string $propertyId)
42+
{
43+
// Create an instance of the Google Analytics Data API client library.
44+
$client = new BetaAnalyticsDataClient();
45+
46+
// Make an API call.
47+
$response = $client->runRealtimeReport([
48+
'property' => 'properties/' . $propertyId,
49+
'dimensions' => [
50+
new Dimension(['name' => 'country']),
51+
new Dimension(['name' => 'city']),
52+
],
53+
'metrics' => [new Metric(['name' => 'activeUsers'])],
54+
]);
55+
56+
printRunRealtimeReportWithMultipleDimensionsResponse($response);
57+
}
58+
59+
/**
60+
* Print results of a runRealtimeReport call.
61+
* @param RunRealtimeReportResponse $response
62+
*/
63+
function printRunRealtimeReportWithMultipleDimensionsResponse(RunRealtimeReportResponse $response)
64+
{
65+
// [START analyticsdata_print_run_realtime_report_response_header]
66+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
67+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
68+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
69+
}
70+
foreach ($response->getMetricHeaders() as $metricHeader) {
71+
printf(
72+
'Metric header name: %s (%s)%s',
73+
$metricHeader->getName(),
74+
MetricType::name($metricHeader->getType()),
75+
PHP_EOL
76+
);
77+
}
78+
// [END analyticsdata_print_run_realtime_report_response_header]
79+
80+
// [START analyticsdata_print_run_realtime_report_response_rows]
81+
print 'Report result: ' . PHP_EOL;
82+
83+
foreach ($response->getRows() as $row) {
84+
printf(
85+
'%s %s' . PHP_EOL,
86+
$row->getDimensionValues()[0]->getValue(),
87+
$row->getMetricValues()[0]->getValue()
88+
);
89+
}
90+
// [END analyticsdata_print_run_realtime_report_response_rows]
91+
}
92+
// [END analyticsdata_run_realtime_report_with_multiple_dimensions]
93+
94+
// The following 2 lines are only needed to run the samples
95+
require_once __DIR__ . '/../../testing/sample_helpers.php';
96+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 of
20+
* a realtime report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runRealtimeReport
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run__realtime_report_with_multiple_metrics.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_realtime_report_with_multiple_metrics]
31+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
32+
use Google\Analytics\Data\V1beta\Dimension;
33+
use Google\Analytics\Data\V1beta\Metric;
34+
use Google\Analytics\Data\V1beta\MetricType;
35+
use Google\Analytics\Data\V1beta\RunRealtimeReportResponse;
36+
37+
/**
38+
* Runs a realtime report on a Google Analytics 4 property.
39+
* @param string $propertyId Your GA-4 Property ID
40+
*/
41+
function run_realtime_report_with_multiple_metrics(string $propertyId)
42+
{
43+
// Create an instance of the Google Analytics Data API client library.
44+
$client = new BetaAnalyticsDataClient();
45+
46+
// Make an API call.
47+
$response = $client->runRealtimeReport([
48+
'property' => 'properties/' . $propertyId,
49+
'dimensions' => [new Dimension(['name' => 'unifiedScreenName'])],
50+
'metrics' => [
51+
new Metric(['name' => 'screenPageViews']),
52+
new Metric(['name' => 'conversions']),
53+
],
54+
]);
55+
56+
printRunRealtimeReportWithMultipleMetricsResponse($response);
57+
}
58+
59+
/**
60+
* Print results of a runRealtimeReport call.
61+
* @param RunRealtimeReportResponse $response
62+
*/
63+
function printRunRealtimeReportWithMultipleMetricsResponse(RunRealtimeReportResponse $response)
64+
{
65+
// [START analyticsdata_print_run_realtime_report_response_header]
66+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
67+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
68+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
69+
}
70+
foreach ($response->getMetricHeaders() as $metricHeader) {
71+
printf(
72+
'Metric header name: %s (%s)%s',
73+
$metricHeader->getName(),
74+
MetricType::name($metricHeader->getType()),
75+
PHP_EOL
76+
);
77+
}
78+
// [END analyticsdata_print_run_realtime_report_response_header]
79+
80+
// [START analyticsdata_print_run_realtime_report_response_rows]
81+
print 'Report result: ' . PHP_EOL;
82+
83+
foreach ($response->getRows() as $row) {
84+
printf(
85+
'%s %s' . PHP_EOL,
86+
$row->getDimensionValues()[0]->getValue(),
87+
$row->getMetricValues()[0]->getValue()
88+
);
89+
}
90+
// [END analyticsdata_print_run_realtime_report_response_rows]
91+
}
92+
// [END analyticsdata_run_realtime_report_with_multiple_metrics]
93+
94+
// The following 2 lines are only needed to run the samples
95+
require_once __DIR__ . '/../../testing/sample_helpers.php';
96+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

analyticsdata/test/analyticsDataTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function testClientFromJsonCredentials()
5151
}
5252
}
5353

54+
public function testRunRealtimeReport()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('run_realtime_report', [$propertyId]);
58+
59+
$this->assertStringContainsString('Report result', $output);
60+
}
61+
62+
public function testRunRealtimeReportWithMultipleDimensions()
63+
{
64+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
65+
$output = $this->runFunctionSnippet('run_realtime_report_with_multiple_dimensions', [$propertyId]);
66+
67+
$this->assertStringContainsString('Report result', $output);
68+
}
69+
5470
public function testRunBatchReport()
5571
{
5672
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
@@ -68,6 +84,14 @@ public function testRunPivotReport()
6884
$this->assertStringContainsString('Report result', $output);
6985
}
7086

87+
public function testRunRunRealtimeReportWithMultipleMetrics()
88+
{
89+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
90+
$output = $this->runFunctionSnippet('run_realtime_report_with_multiple_metrics', [$propertyId]);
91+
92+
$this->assertStringContainsString('Report result', $output);
93+
}
94+
7195
public function testRunReportWithDimensionExcludeFilter()
7296
{
7397
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');

0 commit comments

Comments
 (0)