Skip to content

Commit 3f2a33f

Browse files
authored
feat: [AnalyticsData] add samples for run_report with dimension exclude and in list filter (GoogleCloudPlatform#1743)
1 parent 0cea67e commit 3f2a33f

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
* dimension and metric filters in a report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_dimension_exclude_filter.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_dimension_exclude_filter]
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\FilterExpression;
37+
use Google\Analytics\Data\V1beta\Filter;
38+
use Google\Analytics\Data\V1beta\Filter\StringFilter;
39+
use Google\Analytics\Data\V1beta\RunReportResponse;
40+
41+
/**
42+
* Runs a report using a filter with `not_expression`. The dimension filter
43+
* selects for when `pageTitle` is not `My Homepage`.
44+
* This sample uses relative date range values. See
45+
* https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange
46+
* for more information.
47+
* @param string $propertyId Your GA-4 Property ID
48+
*/
49+
function run_report_with_dimension_exclude_filter(string $propertyId)
50+
{
51+
// Create an instance of the Google Analytics Data API client library.
52+
$client = new BetaAnalyticsDataClient();
53+
54+
// Make an API call.
55+
$response = $client->runReport([
56+
'property' => 'properties/' . $propertyId,
57+
'dimensions' => [new Dimension(['name' => 'pageTitle'])],
58+
'metrics' => [new Metric(['name' => 'sessions'])],
59+
'dateRanges' => [new DateRange([
60+
'start_date' => '7daysAgo',
61+
'end_date' => 'yesterday',
62+
])
63+
],
64+
'dimension_filter' => new FilterExpression([
65+
'not_expression' => new FilterExpression([
66+
'filter' => new Filter([
67+
'field_name' => 'pageTitle',
68+
'string_filter' => new StringFilter([
69+
'value' => 'My Homepage',
70+
]),
71+
]),
72+
]),
73+
]),
74+
]);
75+
76+
printRunReportResponseWithDimensionExcludeFilter($response);
77+
}
78+
79+
/**
80+
* Print results of a runReport call.
81+
* @param RunReportResponse $response
82+
*/
83+
function printRunReportResponseWithDimensionExcludeFilter(RunReportResponse $response)
84+
{
85+
// [START analyticsdata_print_run_report_response_header]
86+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
87+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
88+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
89+
}
90+
foreach ($response->getMetricHeaders() as $metricHeader) {
91+
printf(
92+
'Metric header name: %s (%s)' . PHP_EOL,
93+
$metricHeader->getName(),
94+
MetricType::name($metricHeader->getType())
95+
);
96+
}
97+
// [END analyticsdata_print_run_report_response_header]
98+
99+
// [START analyticsdata_print_run_report_response_rows]
100+
print 'Report result: ' . PHP_EOL;
101+
102+
foreach ($response->getRows() as $row) {
103+
printf(
104+
'%s %s' . PHP_EOL,
105+
$row->getDimensionValues()[0]->getValue(),
106+
$row->getMetricValues()[0]->getValue()
107+
);
108+
}
109+
// [END analyticsdata_print_run_report_response_rows]
110+
}
111+
// [END analyticsdata_run_report_with_dimension_exclude_filter]
112+
113+
// The following 2 lines are only needed to run the samples
114+
require_once __DIR__ . '/../../testing/sample_helpers.php';
115+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
* dimension and metric filters in a report.
21+
* See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.dimension_filter
22+
* for more information.
23+
* Usage:
24+
* composer update
25+
* php run_report_with_dimension_in_list_filter.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_dimension_in_list_filter]
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\FilterExpression;
37+
use Google\Analytics\Data\V1beta\Filter;
38+
use Google\Analytics\Data\V1beta\Filter\InListFilter;
39+
use Google\Analytics\Data\V1beta\RunReportResponse;
40+
41+
/**
42+
* Runs a report using a dimension filter with `in_list_filter` expression.
43+
* The filter selects for when `eventName` is set to one of three event names
44+
* specified in the query.
45+
* This sample uses relative date range values. See
46+
* https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange
47+
* for more information.
48+
* @param string $propertyId Your GA-4 Property ID
49+
*/
50+
function run_report_with_dimension_in_list_filter(string $propertyId)
51+
{
52+
// Create an instance of the Google Analytics Data API client library.
53+
$client = new BetaAnalyticsDataClient();
54+
55+
// Make an API call.
56+
$response = $client->runReport([
57+
'property' => 'properties/' . $propertyId,
58+
'dimensions' => [new Dimension(['name' => 'eventName'])],
59+
'metrics' => [new Metric(['name' => 'sessions'])],
60+
'dateRanges' => [new DateRange([
61+
'start_date' => '7daysAgo',
62+
'end_date' => 'yesterday',
63+
])
64+
],
65+
'dimension_filter' => new FilterExpression([
66+
'filter' => new Filter([
67+
'field_name' => 'eventName',
68+
'in_list_filter' => new InListFilter([
69+
'values' => [
70+
'purchase',
71+
'in_app_purchase',
72+
'app_store_subscription_renew',
73+
],
74+
]),
75+
]),
76+
]),
77+
]);
78+
79+
printRunReportResponseWithDimensionInListFilter($response);
80+
}
81+
82+
/**
83+
* Print results of a runReport call.
84+
* @param RunReportResponse $response
85+
*/
86+
function printRunReportResponseWithDimensionInListFilter(RunReportResponse $response)
87+
{
88+
// [START analyticsdata_print_run_report_response_header]
89+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
90+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
91+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
92+
}
93+
foreach ($response->getMetricHeaders() as $metricHeader) {
94+
printf(
95+
'Metric header name: %s (%s)' . PHP_EOL,
96+
$metricHeader->getName(),
97+
MetricType::name($metricHeader->getType())
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+
printf(
107+
'%s %s' . PHP_EOL,
108+
$row->getDimensionValues()[0]->getValue(),
109+
$row->getMetricValues()[0]->getValue()
110+
);
111+
}
112+
// [END analyticsdata_print_run_report_response_rows]
113+
}
114+
// [END analyticsdata_run_report_with_dimension_in_list_filter]
115+
116+
// The following 2 lines are only needed to run the samples
117+
require_once __DIR__ . '/../../testing/sample_helpers.php';
118+
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 testRunReportWithDimensionExcludeFilter()
55+
{
56+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
57+
$output = $this->runFunctionSnippet('run_report_with_dimension_exclude_filter', [$propertyId]);
58+
59+
$this->assertStringContainsString('Report result', $output);
60+
}
61+
5462
public function testRunReportWithDimensionAndMetricFilters()
5563
{
5664
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
@@ -83,6 +91,14 @@ public function testRunReportWithMultipleMetrics()
8391
$this->assertStringContainsString('Report result', $output);
8492
}
8593

94+
public function testRunReportWithDimensionInListFilter()
95+
{
96+
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');
97+
$output = $this->runFunctionSnippet('run_report_with_dimension_in_list_filter', [$propertyId]);
98+
99+
$this->assertStringContainsString('Report result', $output);
100+
}
101+
86102
public function testRunReportWithNamedDateRanges()
87103
{
88104
$propertyId = self::requireEnv('GA_TEST_PROPERTY_ID');

0 commit comments

Comments
 (0)