Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/n1crack/analytics

Analytics Package for Google Analytics Data API V4
https://github.com/n1crack/analytics

analytics api google-analytics

Last synced: 1 day ago
JSON representation

Analytics Package for Google Analytics Data API V4

Awesome Lists containing this project

README

        

## Analytics Package

Analytics Package for Google Analytics Data API V4

### Installation
Run
```bash
$ composer require ozdemir/analytics
```

### Usage
```php
# Main Library
use Ozdemir\Analytics\Analytics;
use Ozdemir\Analytics\Period;

# Optional reports, you can use these or create your own custom requests.
use Ozdemir\Analytics\Requests\PageViewsByCountry;
use Ozdemir\Analytics\Requests\PageViewsByReferer;

use Ozdemir\Analytics\Requests\PageViews;
use Ozdemir\Analytics\Requests\PageViewsAndUsers;
use Ozdemir\Analytics\Requests\TopBrowsers;
use Ozdemir\Analytics\Requests\TotalUsersByChannels;
use Ozdemir\Analytics\Requests\MostVisitedPages;

$config = [
'property' => '11111111', // Google Analytics Property ID
'credentials' => __DIR__ . '/credentials.json' // credentials.json file path or config array
];

# we load the library
$analytics = new Analytics($config);

# Requests are defined in an array
# Up to 5 requests can be executed in one batch request.
# It is set by Google, so we can't increase this at the moment.
$response = $analytics->fetch([
new PageViewsByReferer(
Period::days(7)
),

new PageViewsByCountry(
Period::days(7),
5 # row limit, only returns 5 rows
),
]);

# We can use the response value as we want.this is quite configurable.
# get($index): index number is the same order of the defined requests above.
# we can also cache the output as we want. It is very easy with the laravel.
return [
'pageViews' => $response->get(0)->toChartJs(), // ->toJson() is also available.
'pageViewsByCountry' => $response->get(1)->toChartJs(),
];

```

#### todo:

- make a better readme / tutorial
- add unit tests