From 3a12d67e163211483156d4038113bc12ee7bd3a6 Mon Sep 17 00:00:00 2001 From: Mate Date: Wed, 17 May 2023 20:27:21 +0200 Subject: [PATCH 1/2] Extend Analytics::get with dimensionFilter parameter --- README.md | 22 +++++++++++++++++++++- src/Analytics.php | 3 +++ src/AnalyticsClient.php | 3 +++ tests/AnalyticsTest.php | 6 ++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f2c5de..d5e0c43 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,7 @@ The function returns a `Collection` in which each item is an array that holds ke For all other queries you can use the `get` function. ```php -public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = []): Collection +public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = [], FilterExpression $dimensionFilter = null): Collection ``` Here's some extra info on the arguments you can pass: @@ -246,6 +246,26 @@ $orderBy = [ ]; ``` +`FilterExpression $dimensionFilter`: filter the result to include only specific dimension values. You can find more details [here](https://cloud.google.com/php/docs/reference/analytics-data/latest/V1beta.RunReportRequest). + +For example: +```php +use Google\Analytics\Data\V1beta\Filter; +use Google\Analytics\Data\V1beta\FilterExpression; +use Google\Analytics\Data\V1beta\Filter\StringFilter; +use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType; + +$dimensionFilter = new FilterExpression ([ + 'filter' => new Filter([ + 'field_name' => 'eventName', + 'string_filter' => new StringFilter ([ + 'match_type' => MatchType::EXACT, + 'value' => 'click', + ]), + ]), +]); +``` + ## Testing Run the tests with: diff --git a/src/Analytics.php b/src/Analytics.php index e6f38be..e0ce836 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -2,6 +2,7 @@ namespace Spatie\Analytics; +use Google\Analytics\Data\V1beta\FilterExpression; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; @@ -178,6 +179,7 @@ public function get( int $maxResults = 10, array $orderBy = [], int $offset = 0, + FilterExpression $dimensionFilter = null, ): Collection { return $this->client->get( $this->propertyId, @@ -187,6 +189,7 @@ public function get( $maxResults, $orderBy, $offset, + $dimensionFilter, ); } } diff --git a/src/AnalyticsClient.php b/src/AnalyticsClient.php index 90b7e01..1e86e44 100644 --- a/src/AnalyticsClient.php +++ b/src/AnalyticsClient.php @@ -4,6 +4,7 @@ use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient; use Google\Analytics\Data\V1beta\Dimension; +use Google\Analytics\Data\V1beta\FilterExpression; use Google\Analytics\Data\V1beta\Metric; use Google\Analytics\Data\V1beta\RunReportResponse; use Illuminate\Contracts\Cache\Repository; @@ -34,6 +35,7 @@ public function get( int $maxResults = 10, array $orderBy = [], int $offset = 0, + FilterExpression $dimensionFilter = null, ): Collection { $typeCaster = resolve(TypeCaster::class); @@ -47,6 +49,7 @@ public function get( 'limit' => $maxResults, 'offset' => $offset, 'orderBys' => $orderBy, + 'dimensionFilter' => $dimensionFilter, ]); $result = collect(); diff --git a/tests/AnalyticsTest.php b/tests/AnalyticsTest.php index 373465a..551958d 100644 --- a/tests/AnalyticsTest.php +++ b/tests/AnalyticsTest.php @@ -32,6 +32,7 @@ 10, [], 0, + null, ]; $this @@ -70,6 +71,7 @@ OrderBy::dimension('date', true), ], 0, + null, ]; $this @@ -110,6 +112,7 @@ OrderBy::dimension('date', true), ], 0, + null, ]; $this @@ -151,6 +154,7 @@ OrderBy::metric('screenPageViews', true), ], 0, + null, ]; $this @@ -193,6 +197,7 @@ OrderBy::metric('screenPageViews', true), ], 0, + null, ]; $this @@ -231,6 +236,7 @@ OrderBy::metric('screenPageViews', true), ], 0, + null, ]; $this From 31afad5ec4fe331ed14846f59871b4c3f4011fd8 Mon Sep 17 00:00:00 2001 From: mate Date: Tue, 23 May 2023 17:01:13 +0200 Subject: [PATCH 2/2] Update README.md, fix code style Co-authored-by: Arkaitz Garro --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d5e0c43..734e8b9 100644 --- a/README.md +++ b/README.md @@ -255,10 +255,10 @@ use Google\Analytics\Data\V1beta\FilterExpression; use Google\Analytics\Data\V1beta\Filter\StringFilter; use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType; -$dimensionFilter = new FilterExpression ([ +$dimensionFilter = new FilterExpression([ 'filter' => new Filter([ 'field_name' => 'eventName', - 'string_filter' => new StringFilter ([ + 'string_filter' => new StringFilter([ 'match_type' => MatchType::EXACT, 'value' => 'click', ]),