Skip to content

Commit 0cea67e

Browse files
anweshanbshaffer
andauthored
feat(analyticsdata): add samples for run_report with filters (GoogleCloudPlatform#1742)
Co-authored-by: Brent Shaffer <[email protected]>
1 parent bd96cc9 commit 0cea67e

File tree

5 files changed

+413
-1
lines changed

5 files changed

+413
-1
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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_and_metric_filters.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_dimension_and_metric_filters]
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\NumericValue;
37+
use Google\Analytics\Data\V1beta\FilterExpression;
38+
use Google\Analytics\Data\V1beta\FilterExpressionList;
39+
use Google\Analytics\Data\V1beta\Filter;
40+
use Google\Analytics\Data\V1beta\Filter\StringFilter;
41+
use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;
42+
use Google\Analytics\Data\V1beta\Filter\NumericFilter;
43+
use Google\Analytics\Data\V1beta\Filter\NumericFilter\Operation;
44+
use Google\Analytics\Data\V1beta\RunReportResponse;
45+
46+
/**
47+
* @param string $propertyId Your GA-4 Property ID
48+
* Runs a report using both metric and dimension filters. A dimension filter
49+
* limits the report to include only users who made an in-app purchase using
50+
* Android platform. A metric filter specifies that only users with session
51+
* counts larger than 1,000 should be included.
52+
*/
53+
function run_report_with_dimension_and_metric_filters(string $propertyId)
54+
{
55+
// Create an instance of the Google Analytics Data API client library.
56+
$client = new BetaAnalyticsDataClient();
57+
58+
// Make an API call.
59+
$response = $client->runReport([
60+
'property' => 'properties/' . $propertyId,
61+
'dimensions' => [new Dimension(['name' => 'city'])],
62+
'metrics' => [new Metric(['name' => 'activeUsers'])],
63+
'dateRanges' => [new DateRange([
64+
'start_date' => '2020-03-31',
65+
'end_date' => 'today',
66+
]),
67+
],
68+
'metric_filter' => new FilterExpression([
69+
'filter' => new Filter([
70+
'field_name' => 'sessions',
71+
'numeric_filter' => new NumericFilter([
72+
'operation' => Operation::GREATER_THAN,
73+
'value' => new NumericValue([
74+
'int64_value' => 1000,
75+
]),
76+
]),
77+
]),
78+
]),
79+
'dimension_filter' => new FilterExpression([
80+
'and_group' => new FilterExpressionList([
81+
'expressions' => [
82+
new FilterExpression([
83+
'filter' => new Filter([
84+
'field_name' => 'platform',
85+
'string_filter' => new StringFilter([
86+
'match_type' => MatchType::EXACT,
87+
'value' => 'Android',
88+
])
89+
]),
90+
]),
91+
new FilterExpression([
92+
'filter' => new Filter([
93+
'field_name' => 'eventName',
94+
'string_filter' => new StringFilter([
95+
'match_type' => MatchType::EXACT,
96+
'value' => 'in_app_purchase',
97+
])
98+
])
99+
]),
100+
],
101+
]),
102+
]),
103+
]);
104+
105+
printRunReportResponseWithDimensionAndMetricFilters($response);
106+
}
107+
108+
/**
109+
* Print results of a runReport call.
110+
* @param RunReportResponse $response
111+
*/
112+
function printRunReportResponseWithDimensionAndMetricFilters(RunReportResponse $response)
113+
{
114+
// [START analyticsdata_print_run_report_response_header]
115+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
116+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
117+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
118+
}
119+
foreach ($response->getMetricHeaders() as $metricHeader) {
120+
printf(
121+
'Metric header name: %s (%s)' . PHP_EOL,
122+
$metricHeader->getName(),
123+
MetricType::name($metricHeader->getType())
124+
);
125+
}
126+
// [END analyticsdata_print_run_report_response_header]
127+
128+
// [START analyticsdata_print_run_report_response_rows]
129+
print 'Report result: ' . PHP_EOL;
130+
131+
foreach ($response->getRows() as $row) {
132+
printf(
133+
'%s %s' . PHP_EOL,
134+
$row->getDimensionValues()[0]->getValue(),
135+
$row->getMetricValues()[0]->getValue()
136+
);
137+
}
138+
// [END analyticsdata_print_run_report_response_rows]
139+
}
140+
// [END analyticsdata_run_report_with_dimension_and_metric_filters]
141+
142+
// The following 2 lines are only needed to run the samples
143+
require_once __DIR__ . '/../../testing/sample_helpers.php';
144+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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_filter.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_dimension_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+
* @param string $propertyId Your GA-4 Property ID
43+
* Runs a report using a dimension filter. The call returns a time series
44+
* report of `eventCount` when `eventName` is `first_open` for each date.
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+
*/
49+
function run_report_with_dimension_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' => 'date'])],
58+
'metrics' => [new Metric(['name' => 'eventCount'])],
59+
'dateRanges' => [
60+
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+
'string_filter' => new StringFilter([
69+
'value' => 'first_open'
70+
]),
71+
]),
72+
]),
73+
]);
74+
75+
printRunReportResponseWithDimensionFilter($response);
76+
}
77+
78+
/**
79+
* Print results of a runReport call.
80+
* @param RunReportResponse $response
81+
*/
82+
function printRunReportResponseWithDimensionFilter(RunReportResponse $response)
83+
{
84+
// [START analyticsdata_print_run_report_response_header]
85+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
86+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
87+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
88+
}
89+
foreach ($response->getMetricHeaders() as $metricHeader) {
90+
printf(
91+
'Metric header name: %s (%s)' . PHP_EOL,
92+
$metricHeader->getName(),
93+
MetricType::name($metricHeader->getType())
94+
);
95+
}
96+
// [END analyticsdata_print_run_report_response_header]
97+
98+
// [START analyticsdata_print_run_report_response_rows]
99+
print 'Report result: ' . PHP_EOL;
100+
101+
foreach ($response->getRows() as $row) {
102+
printf(
103+
'%s %s' . PHP_EOL,
104+
$row->getDimensionValues()[0]->getValue(),
105+
$row->getMetricValues()[0]->getValue()
106+
);
107+
}
108+
// [END analyticsdata_print_run_report_response_rows]
109+
}
110+
// [END analyticsdata_run_report_with_dimension_filter]
111+
112+
// The following 2 lines are only needed to run the samples
113+
require_once __DIR__ . '/../../testing/sample_helpers.php';
114+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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_multiple_dimension_filters.php YOUR-GA4-PROPERTY-ID
26+
*/
27+
28+
namespace Google\Cloud\Samples\Analytics\Data;
29+
30+
// [START analyticsdata_run_report_with_multiple_dimension_filters]
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\FilterExpressionList;
38+
use Google\Analytics\Data\V1beta\Filter;
39+
use Google\Analytics\Data\V1beta\Filter\StringFilter;
40+
use Google\Analytics\Data\V1beta\RunReportResponse;
41+
42+
/**
43+
* @param string $propertyId Your GA-4 Property ID
44+
* Runs a report using multiple dimension filters joined as `and_group`
45+
* expression. The filter selects for when both `browser` is `Chrome` and
46+
* `countryId` is `US`.
47+
* This sample uses relative date range values. See
48+
* https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange
49+
* for more information.
50+
*/
51+
function run_report_with_multiple_dimension_filters(string $propertyId)
52+
{
53+
// Create an instance of the Google Analytics Data API client library.
54+
$client = new BetaAnalyticsDataClient();
55+
56+
// Make an API call.
57+
$response = $client->runReport([
58+
'property' => 'properties/' . $propertyId,
59+
'dimensions' => [new Dimension(['name' => 'browser'])],
60+
'metrics' => [new Metric(['name' => 'activeUsers'])],
61+
'dateRanges' => [
62+
new DateRange([
63+
'start_date' => '7daysAgo',
64+
'end_date' => 'yesterday',
65+
]),
66+
],
67+
'dimension_filter' => new FilterExpression([
68+
'and_group' => new FilterExpressionList([
69+
'expressions' => [
70+
new FilterExpression([
71+
'filter' => new Filter([
72+
'field_name' => 'browser',
73+
'string_filter' => new StringFilter([
74+
'value' => 'Chrome',
75+
])
76+
]),
77+
]),
78+
new FilterExpression([
79+
'filter' => new Filter([
80+
'field_name' => 'countryId',
81+
'string_filter' => new StringFilter([
82+
'value' => 'US',
83+
])
84+
]),
85+
]),
86+
],
87+
]),
88+
]),
89+
]);
90+
91+
printRunReportResponseWithMultipleDimensionFilters($response);
92+
}
93+
94+
/**
95+
* Print results of a runReport call.
96+
* @param RunReportResponse $response
97+
*/
98+
function printRunReportResponseWithMultipleDimensionFilters(RunReportResponse $response)
99+
{
100+
// [START analyticsdata_print_run_report_response_header]
101+
printf('%s rows received%s', $response->getRowCount(), PHP_EOL);
102+
foreach ($response->getDimensionHeaders() as $dimensionHeader) {
103+
printf('Dimension header name: %s%s', $dimensionHeader->getName(), PHP_EOL);
104+
}
105+
foreach ($response->getMetricHeaders() as $metricHeader) {
106+
printf(
107+
'Metric header name: %s (%s)' . PHP_EOL,
108+
$metricHeader->getName(),
109+
MetricType::name($metricHeader->getType())
110+
);
111+
}
112+
// [END analyticsdata_print_run_report_response_header]
113+
114+
// [START analyticsdata_print_run_report_response_rows]
115+
print 'Report result: ' . PHP_EOL;
116+
117+
foreach ($response->getRows() as $row) {
118+
printf(
119+
'%s %s' . PHP_EOL,
120+
$row->getDimensionValues()[0]->getValue(),
121+
$row->getMetricValues()[0]->getValue()
122+
);
123+
}
124+
// [END analyticsdata_print_run_report_response_rows]
125+
}
126+
// [END analyticsdata_run_report_with_multiple_dimension_filters]
127+
128+
// The following 2 lines are only needed to run the samples
129+
require_once __DIR__ . '/../../testing/sample_helpers.php';
130+
return \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)