From c3cf1480b6ca7c076490f048004272945636176b Mon Sep 17 00:00:00 2001 From: Stijn Vanouplines Date: Tue, 24 Oct 2023 16:44:03 +0200 Subject: [PATCH 1/4] Added method to fetch the top countries --- src/Analytics.php | 21 +++++++++++++++++ tests/AnalyticsTest.php | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/Analytics.php b/src/Analytics.php index 4d5d594..da5d0f5 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -165,6 +165,27 @@ 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, + ); + } + public function get( Period $period, array $metrics, diff --git a/tests/AnalyticsTest.php b/tests/AnalyticsTest.php index 774d486..89d72e0 100644 --- a/tests/AnalyticsTest.php +++ b/tests/AnalyticsTest.php @@ -279,6 +279,57 @@ ]); }); +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], + ]); +}); + function expectCarbon(Carbon $carbon) { return Mockery::on(function (Carbon $argument) use ($carbon) { From 241b2d8ab647805e30f93339306e726635515406 Mon Sep 17 00:00:00 2001 From: Stijn Vanouplines Date: Wed, 25 Oct 2023 09:40:33 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 445407d..c304afa 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,14 @@ 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`. + ### All other Google Analytics queries For all other queries you can use the `get` function. From bd430be21c1c8686c980f4d9b6ffb114bcdffe81 Mon Sep 17 00:00:00 2001 From: Stijn Vanouplines Date: Wed, 25 Oct 2023 09:51:01 +0200 Subject: [PATCH 3/4] Added method to fetch the top operating systems --- src/Analytics.php | 21 +++++++++++++++++ tests/AnalyticsTest.php | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/Analytics.php b/src/Analytics.php index da5d0f5..d70a4eb 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -186,6 +186,27 @@ public function fetchTopCountries(Period $period, int $maxResults = 10, int $off ); } + /** + * @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 89d72e0..984d0ea 100644 --- a/tests/AnalyticsTest.php +++ b/tests/AnalyticsTest.php @@ -330,6 +330,57 @@ ]); }); +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) { From 53caa1950d08e7efcb98b502a5e93ba05f8b994e Mon Sep 17 00:00:00 2001 From: Stijn Vanouplines Date: Wed, 25 Oct 2023 09:52:32 +0200 Subject: [PATCH 4/4] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index c304afa..4527635 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,14 @@ public function fetchTopCountries(Period $period, int $maxResults = 10): Collect 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.