Skip to content

Commit

Permalink
feat: add new param 'metricFilter' in signature method 'get' (#523)
Browse files Browse the repository at this point in the history
* feat: add new param 'metricFilter' in signature method 'get'

* fix: add new param to AnalyticsTest

* fix: move new param to the last position

* fix: move new param to the last position in analyticsClient
  • Loading branch information
laudefe authored Jul 22, 2024
1 parent 36ffe36 commit bfbb989
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -224,7 +225,8 @@ public function get(
$orderBy,
$offset,
$dimensionFilter,
$keepEmptyRows
$keepEmptyRows,
$metricFilter
);
}
}
2 changes: 2 additions & 0 deletions src/AnalyticsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function get(
int $offset = 0,
?FilterExpression $dimensionFilter = null,
bool $keepEmptyRows = false,
?FilterExpression $metricFilter = null,
): Collection {
$typeCaster = resolve(TypeCaster::class);

Expand All @@ -52,6 +53,7 @@ public function get(
'orderBys' => $orderBy,
'dimensionFilter' => $dimensionFilter,
'keepEmptyRows' => $keepEmptyRows,
'metricFilter' => $metricFilter
]);

$result = collect();
Expand Down
8 changes: 8 additions & 0 deletions tests/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
0,
null,
false,
null,
];

$this
Expand Down Expand Up @@ -74,6 +75,7 @@
0,
null,
false,
null,
];

$this
Expand Down Expand Up @@ -116,6 +118,7 @@
0,
null,
false,
null,
];

$this
Expand Down Expand Up @@ -159,6 +162,7 @@
0,
null,
false,
null,
];

$this
Expand Down Expand Up @@ -203,6 +207,7 @@
0,
null,
false,
null,
];

$this