Skip to content

Commit

Permalink
Added method to fetch the top countries & top operating systems (#506)
Browse files Browse the repository at this point in the history
* Added method to fetch the top countries

* Update README.md

* Added method to fetch the top operating systems

* Update README.md
  • Loading branch information
stijnvanouplines authored Oct 25, 2023
1 parent de11abf commit 0687e10
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
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

0 comments on commit 0687e10

Please sign in to comment.