Skip to content

Commit

Permalink
Merge pull request #487 from gmbenedek/main
Browse files Browse the repository at this point in the history
Extend Analytics::get with dimensionFilter parameter
  • Loading branch information
timvandijck authored May 30, 2023
2 parents b5c6dba + 31afad5 commit b51ff78
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Analytics;

use Google\Analytics\Data\V1beta\FilterExpression;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;

Expand Down Expand Up @@ -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,
Expand All @@ -187,6 +189,7 @@ public function get(
$maxResults,
$orderBy,
$offset,
$dimensionFilter,
);
}
}
3 changes: 3 additions & 0 deletions src/AnalyticsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function get(
int $maxResults = 10,
array $orderBy = [],
int $offset = 0,
FilterExpression $dimensionFilter = null,
): Collection {
$typeCaster = resolve(TypeCaster::class);

Expand All @@ -47,6 +49,7 @@ public function get(
'limit' => $maxResults,
'offset' => $offset,
'orderBys' => $orderBy,
'dimensionFilter' => $dimensionFilter,
]);

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

$this
Expand Down Expand Up @@ -70,6 +71,7 @@
OrderBy::dimension('date', true),
],
0,
null,
];

$this
Expand Down Expand Up @@ -110,6 +112,7 @@
OrderBy::dimension('date', true),
],
0,
null,
];

$this
Expand Down Expand Up @@ -151,6 +154,7 @@
OrderBy::metric('screenPageViews', true),
],
0,
null,
];

$this
Expand Down Expand Up @@ -193,6 +197,7 @@
OrderBy::metric('screenPageViews', true),
],
0,
null,
];

$this
Expand Down Expand Up @@ -231,6 +236,7 @@
OrderBy::metric('screenPageViews', true),
],
0,
null,
];

$this
Expand Down

0 comments on commit b51ff78

Please sign in to comment.