Skip to content

Commit

Permalink
Added the possibility to fetch userTypes based on a period (#229)
Browse files Browse the repository at this point in the history
* Added fetchUserTypes method.

* Fixed minor bug.

* Corrected a typo.

* Style CI fix.

* Minor clean up.

* Update readme with user types example.
  • Loading branch information
pactode authored and freekmurze committed Oct 30, 2017
1 parent 3e2e54a commit 3d8e82a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 = [])
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3d8e82a

Please sign in to comment.