diff --git a/README.md b/README.md index b6926bb..5f8a16b 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,12 @@ return [ * If you set this to zero, the responses won't be cached at all. */ 'cache_lifetime_in_minutes' => 60 * 24, - + /* * Here you may configure the "store" that the underlying Google_Client will * use to store it's data. You may also add extra parameters that will * be passed on setCacheConfig (see docs for google-api-php-client). - * + * * Optional parameters: "lifetime", "prefix" */ 'cache' => [ @@ -130,9 +130,9 @@ I'm assuming that you've already created a Analytics account on the [Analytics s On this screen you can grant access to the email address found in the `client_email` key from the json file you download in the previous step. Read only access is enough. ![6](https://spatie.github.io/laravel-analytics/v2/6.jpg) - + ### Getting the view id - + The last thing you'll have to do is fill in the `view_id` in the config file. You can get the right value on the [Analytics site](https://analytics.google.com/analytics). Go to "View setting" in the Admin-section of the property. ![7](https://spatie.github.io/laravel-analytics/v2/7.jpg) @@ -196,6 +196,14 @@ public function fetchTopReferrers(Period $period, int $maxResults = 20): Collect The function returns a `Collection` in which each item is an array that holds keys `url` and `pageViews`. +### User Types + +```php +public function fetchUserTypes(Period $period): Collection +``` + +The function returns a `Collection` in which each item is an array that holds keys `type` and `sessions`. + ### Top browsers ```php @@ -206,7 +214,7 @@ The function returns a `Collection` in which each item is an array that holds ke ### All other Google Analytics queries -To perform all other queries on the Google Analytics resource use `performQuery`. [Google's Core Reporting API](https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries) provides more information on which metrics and dimensions might be used. +To perform all other queries on the Google Analytics resource use `performQuery`. [Google's Core Reporting API](https://developers.google.com/analytics/devguides/reporting/core/v3/common-queries) provides more information on which metrics and dimensions might be used. ```php public function performQuery(Period $period, string $metrics, array $others = []) @@ -255,7 +263,7 @@ We publish all received postcards [on our company website](https://spatie.be/en/ Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). -Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). +Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/spatie). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff. ## License diff --git a/src/Analytics.php b/src/Analytics.php index b0ea456..d54ec6f 100644 --- a/src/Analytics.php +++ b/src/Analytics.php @@ -116,6 +116,24 @@ public function fetchTopReferrers(Period $period, int $maxResults = 20): Collect }); } + public function fetchUserTypes(Period $period): Collection + { + $response = $this->performQuery( + $period, + 'ga:sessions', + [ + 'dimensions' => 'ga:userType', + ] + ); + + return collect($response->rows ?? [])->map(function (array $userRow) { + return [ + 'type' => $userRow[0], + 'sessions' => (int) $userRow[1], + ]; + }); + } + public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection { $response = $this->performQuery(