Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to fetch the top countries & top operating systems #506

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collecti

The function returns a `Collection` in which each item is an array that holds keys `screenPageViews` and `browser`.

### Top countries

```php
public function fetchTopCountries(Period $period, int $maxResults = 10): Collection
```

The function returns a `Collection` in which each item is an array that holds keys `screenPageViews` and `country`.

### Top operating systems

```php
public function fetchTopOperatingSystems(Period $period, int $maxResults = 10): Collection
```

The function returns a `Collection` in which each item is an array that holds keys `screenPageViews` and `operatingSystem`.

### All other Google Analytics queries

For all other queries you can use the `get` function.
Expand Down
42 changes: 42 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,48 @@ public function fetchTopBrowsers(Period $period, int $maxResults = 10, int $offs
);
}

/**
* @param \Spatie\Analytics\Period $period
* @return \Illuminate\Support\Collection<int, array{
* country: string,
* screenPageViews: int
* }>
*/
public function fetchTopCountries(Period $period, int $maxResults = 10, int $offset = 0): Collection
{
return $this->get(
period: $period,
metrics: ['screenPageViews'],
dimensions: ['country'],
maxResults: $maxResults,
orderBy: [
OrderBy::metric('screenPageViews', true),
],
offset: $offset,
);
}

/**
* @param \Spatie\Analytics\Period $period
* @return \Illuminate\Support\Collection<int, array{
* operatingSystem: string,
* screenPageViews: int
* }>
*/
public function fetchTopOperatingSystems(Period $period, int $maxResults = 10, int $offset = 0): Collection
{
return $this->get(
period: $period,
metrics: ['screenPageViews'],
dimensions: ['operatingSystem'],
maxResults: $maxResults,
orderBy: [
OrderBy::metric('screenPageViews', true),
],
offset: $offset,
);
}

public function get(
Period $period,
array $metrics,
Expand Down
102 changes: 102 additions & 0 deletions tests/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,108 @@
]);
});

it('can fetch the top countries', function () {
$period = Period::create($this->startDate, $this->endDate);

$expectedArguments = [
$this->propertyId,
$period,
['screenPageViews'],
['country'],
3,
[
OrderBy::metric('screenPageViews', true),
],
0,
null,
false,
];

$this
->analyticsClient
->shouldReceive('get')
->withArgs($expectedArguments)
->once()
->andReturn(collect(
[
[
'country' => 'Country 1',
'screenPageViews' => 100,
],
[
'country' => 'Country 2',
'screenPageViews' => 90,
],
[
'country' => 'Country 3',
'screenPageViews' => 60,
],
]
));

$response = $this
->analytics
->fetchTopCountries($period, 3);

expect($response)->toBeInstanceOf(Collection::class)
->and($response->toArray())->toBe([
['country' => 'Country 1', 'screenPageViews' => 100],
['country' => 'Country 2', 'screenPageViews' => 90],
['country' => 'Country 3', 'screenPageViews' => 60],
]);
});

it('can fetch the top operating systems', function () {
$period = Period::create($this->startDate, $this->endDate);

$expectedArguments = [
$this->propertyId,
$period,
['screenPageViews'],
['operatingSystem'],
3,
[
OrderBy::metric('screenPageViews', true),
],
0,
null,
false,
];

$this
->analyticsClient
->shouldReceive('get')
->withArgs($expectedArguments)
->once()
->andReturn(collect(
[
[
'operatingSystem' => 'Operating system 1',
'screenPageViews' => 100,
],
[
'operatingSystem' => 'Operating system 2',
'screenPageViews' => 90,
],
[
'operatingSystem' => 'Operating system 3',
'screenPageViews' => 60,
],
]
));

$response = $this
->analytics
->fetchTopOperatingSystems($period, 3);

expect($response)->toBeInstanceOf(Collection::class)
->and($response->toArray())->toBe([
['operatingSystem' => 'Operating system 1', 'screenPageViews' => 100],
['operatingSystem' => 'Operating system 2', 'screenPageViews' => 90],
['operatingSystem' => 'Operating system 3', 'screenPageViews' => 60],
]);
});

function expectCarbon(Carbon $carbon)
{
return Mockery::on(function (Carbon $argument) use ($carbon) {
Expand Down