diff --git a/README.md b/README.md index 4527635..f5e861b 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,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 = [], FilterExpression $dimensionFilter = null): Collection +public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = [], FilterExpression $dimensionFilter = null, FilterExpression $metricFilter = null): Collection ``` Here's some extra info on the arguments you can pass: @@ -284,6 +284,28 @@ $dimensionFilter = new FilterExpression([ ]); ``` +`FilterExpression $metricFilter`: filter applied after aggregating the report's rows, similar to SQL having-clause. Dimensions cannot be used in this filter. 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\NumericFilter; +use Google\Analytics\Data\V1beta\NumericValue; +use Google\Analytics\Data\V1beta\Filter\NumericFilter\Operation; + +$metricFilter = new FilterExpression([ + 'filter' => new Filter([ + 'field_name' => 'eventCount', + 'numeric_filter' => new NumericFilter([ + 'operation' => Operation::GREATER_THAN, + 'value' => new NumericValue([ + 'int64_value' => 3, + ]), + ]), + ]), +]); + ## Testing Run the tests with: diff --git a/src/Analytics.php b/src/Analytics.php index 4e286f8..293dfc6 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -214,6 +214,7 @@ public function get( int $offset = 0, ?FilterExpression $dimensionFilter = null, bool $keepEmptyRows = false, + ?FilterExpression $metricFilter = null, ): Collection { return $this->client->get( $this->propertyId, @@ -224,7 +225,8 @@ public function get( $orderBy, $offset, $dimensionFilter, - $keepEmptyRows + $keepEmptyRows, + $metricFilter ); } } diff --git a/src/AnalyticsClient.php b/src/AnalyticsClient.php index d4f5ab5..a8994d9 100644 --- a/src/AnalyticsClient.php +++ b/src/AnalyticsClient.php @@ -37,6 +37,7 @@ public function get( int $offset = 0, ?FilterExpression $dimensionFilter = null, bool $keepEmptyRows = false, + ?FilterExpression $metricFilter = null, ): Collection { $typeCaster = resolve(TypeCaster::class); @@ -52,6 +53,7 @@ public function get( 'orderBys' => $orderBy, 'dimensionFilter' => $dimensionFilter, 'keepEmptyRows' => $keepEmptyRows, + 'metricFilter' => $metricFilter ]); $result = collect(); diff --git a/tests/AnalyticsTest.php b/tests/AnalyticsTest.php index 984d0ea..784aa92 100644 --- a/tests/AnalyticsTest.php +++ b/tests/AnalyticsTest.php @@ -34,6 +34,7 @@ 0, null, false, + null, ]; $this @@ -74,6 +75,7 @@ 0, null, false, + null, ]; $this @@ -116,6 +118,7 @@ 0, null, false, + null, ]; $this @@ -159,6 +162,7 @@ 0, null, false, + null, ]; $this @@ -203,6 +207,7 @@ 0, null, false, + null, ]; $this @@ -243,6 +248,7 @@ 0, null, false, + null, ]; $this @@ -294,6 +300,7 @@ 0, null, false, + null, ]; $this @@ -345,6 +352,7 @@ 0, null, false, + null, ]; $this