Skip to content

Commit 831c4c1

Browse files
anweshanikuleshov
andauthored
samples: add new sample for analytics data api (GoogleCloudPlatform#1671)
* added blank php * renamed runReportTest to run_report_test * moved files to analytics data * changed pfp test file name in analytics data * changed name of runReport to run_report * wrote initial php test code in analytics data * wrote run_report.php code * fixed typo * Adds run report * Reorders tag * linting change * removes changed date * adds newline at end of file * fix region tag * Fix linting * Fixes linting issues Co-authored-by: ikuleshov <[email protected]>
1 parent e243b76 commit 831c4c1

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

analyticsdata/run_report.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
20+
"""Google Analytics Data API sample application demonstrating the creation
21+
of a basic report.
22+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
23+
for more information.
24+
"""
25+
26+
Before you start the application, please review the comments starting with
27+
"TODO(developer)" and update the code to use the correct values.
28+
29+
Usage:
30+
composer update
31+
php run_report.php
32+
*/
33+
34+
// [START analyticsdata_run_report]
35+
require 'vendor/autoload.php';
36+
37+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
38+
use Google\Analytics\Data\V1beta\DateRange;
39+
use Google\Analytics\Data\V1beta\Dimension;
40+
use Google\Analytics\Data\V1beta\Metric;
41+
use Google\Analytics\Data\V1beta\MetricType;
42+
43+
function runReport()
44+
{
45+
/**
46+
* TODO(developer): Replace this variable with your Google Analytics 4
47+
* property ID before running the sample.
48+
*/
49+
$property_id = 'YOUR-GA4-PROPERTY-ID';
50+
51+
// [START analyticsdata_initialize]
52+
//Imports the Google Analytics Data API client library.'
53+
54+
$client = new BetaAnalyticsDataClient();
55+
56+
// [END analyticsdata_initialize]
57+
58+
// Make an API call.
59+
$response = $client->runReport([
60+
'property' => 'properties/' . $property_id,
61+
'dateRanges' => [
62+
new DateRange([
63+
'start_date' => '2020-09-01',
64+
'end_date' => '2020-09-15',
65+
]),
66+
],
67+
'dimensions' => [new Dimension(
68+
[
69+
'name' => 'country',
70+
]
71+
),
72+
],
73+
'metrics' => [new Metric(
74+
[
75+
'name' => 'activeUsers',
76+
]
77+
)
78+
]
79+
]);
80+
81+
printRunReportResponse($response);
82+
}
83+
84+
// Print results of a runReport call.
85+
function printRunReportResponse($response)
86+
{
87+
// [START analyticsdata_print_run_report_response_header]
88+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
89+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
90+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
91+
}
92+
foreach ($response->getMetricHeaders() as $metricHeader) {
93+
printf(
94+
'Metric header name: %s (%s)%s',
95+
$metricHeader->getName(),
96+
MetricType::name($metricHeader->getType()),
97+
PHP_EOL
98+
);
99+
}
100+
// [END analyticsdata_print_run_report_response_header]
101+
102+
// [START analyticsdata_print_run_report_response_rows]
103+
print 'Report result: ' . PHP_EOL;
104+
105+
foreach ($response->getRows() as $row) {
106+
print $row->getDimensionValues()[0]->getValue()
107+
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
108+
}
109+
// [END analyticsdata_print_run_report_response_rows]
110+
}
111+
// [END analyticsdata_run_report]
112+
runReport();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google Inc.
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+
use Google\Cloud\TestUtils\TestTrait;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class runReportTest extends TestCase
22+
{
23+
use TestTrait;
24+
25+
public function testRunReport()
26+
{
27+
$file = sys_get_temp_dir() . '/analyticsdata_run_report';
28+
$contents = file_get_contents(__DIR__ . '/../run_report.php');
29+
$test_property_id = self::$GA_TEST_PROPERTY_ID || '222596558';
30+
$contents = str_replace(
31+
['YOUR-GA4-PROPERTY-ID', '__DIR__'],
32+
[$test_property_id, sprintf('"%s/.."', __DIR__)],
33+
$contents
34+
);
35+
file_put_contents($file, $contents);
36+
37+
// Invoke run_report.php
38+
$output = $this->runSnippet($file);
39+
40+
$this->assertRegExp('/Report result/', $output);
41+
}
42+
}

0 commit comments

Comments
 (0)