diff --git a/README.md b/README.md index 445407d..4527635 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Analytics.php b/src/Analytics.php index 4d5d594..d70a4eb 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -165,6 +165,48 @@ public function fetchTopBrowsers(Period $period, int $maxResults = 10, int $offs ); } + /** + * @param \Spatie\Analytics\Period $period + * @return \Illuminate\Support\Collection + */ + 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 + */ + 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, diff --git a/tests/AnalyticsTest.php b/tests/AnalyticsTest.php index 774d486..984d0ea 100644 --- a/tests/AnalyticsTest.php +++ b/tests/AnalyticsTest.php @@ -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) {