only to be compatible with 5.6 and publishable in packagist :(
Retrieve data from Google Analytics
, (*1)
Using this package you can easily retrieve data from Google Analytics., (*2)
Here are a few examples of the provided methods:, (*3)
use Spatie\Analytics\Period;
//fetch the most visited pages for today and the past week
Analytics::fetchMostVisitedPages(Period::days(7));
//fetch visitors and page views for the past week
Analytics::fetchVisitorsAndPageViews(Period::days(7));
Most methods will return an \Illuminate\Support\Collection
object containing the results., (*4)
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*5)
Postcardware
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment you are required to send us a postcard from your hometown, mentioning which of our package(s) you are using., (*6)
Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*7)
The best postcards will get published on the open source page on our website., (*8)
Install
This package can be installed through Composer., (*9)
``` bash
composer require spatie/laravel-analytics, (*10)
You must install this service provider.
```php
// config/app.php
'providers' => [
...
Spatie\Analytics\AnalyticsServiceProvider::class,
...
];
This package also comes with a facade, which provides an easy way to call the the class., (*11)
// config/app.php
'aliases' => [
...
'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
...
];
You can publish the config file of this package with this command:, (*12)
``` bash
php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider", (*13)
The following config file will be published in `config/laravel-analytics.php`
```php
return [
/*
* The view id of which you want to display data.
*/
'view_id' => env('ANALYTICS_VIEW_ID'),
/*
* Path to the json file with service account credentials. Take a look at the README of this package
* to learn how to get this file.
*/
'service_account_credentials_json' => storage_path('app/laravel-google-analytics/service-account-credentials.json'),
/*
* The amount of minutes the Google API responses will be cached.
* If you set this to zero, the responses won't be cached at all.
*/
'cache_lifetime_in_minutes' => 60 * 24,
/*
* The directory where the underlying Google_Client will store it's cache files.
*/
'cache_location' => storage_path('app/laravel-google-analytics/google-cache/'),
];
How to obtain the credentials to communicate with Google Analytics
Getting credentials
The first thing youâll need to do is to get some credentials to use Google APIâs. Iâm assuming that youâve already created a Google account and are signed in. Head over to Google APIâs site and click "Select a project" in the header., (*14)
, (*15)
Next up we must specify which APIâs the project may consume. In the list of available APIâs click "Google Analytics API". On the next screen click "Enable"., (*16)
, (*17)
Now that youâve created a project that has access to the Analytics API itâs time to download a file with these credentials. Click "Credentials" in the sidebar. Youâll want to create a "Service account key"., (*18)
, (*19)
On the next screen you can give the service account a name. You can name it anything youâd like. In the service account id youâll see an email address. Weâll use this email address later on in this guide. Select "JSON" as the key type and click "Create" to download the JSON file., (*20)
, (*21)
Save the json inside your Laravel project at the location specified in the service_account_credentials_json
key of the config file of this package. Because the json file contains potentially sensitive information I don't recommend committing it to your git repository., (*22)
Granting permissions to your Analytics property
I'm assuming that you've already created a Analytics account on the Analytics site. Go to "User management" in the Admin-section of the property., (*23)
, (*24)
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., (*25)
, (*26)
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. Go to "View setting" in the Admin-section of the property., (*27)
, (*28)
You'll need the View ID
displayed there., (*29)
, (*30)
Usage
When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an Illuminate\Support\Collection
-instance., (*31)
Here is an example to retrieve visitors and pageview data for the current day and the last seven days., (*32)
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
$analyticsData
is a Collection
in which each item is an array that holds keys date
, visitors
and pageViews
, (*33)
If you want to have more control over the period you want to fetch data for, you can pass a startDate
and an endDate
to the period object., (*34)
$startDate = Carbon::now()->subYear();
$endDate = Carbon::now();
Period::create($startDate, $endDate);
Provided methods
Visitors and pageviews
public function fetchVisitorsAndPageViews(Period $period): Collection
The function returns a Collection
in which each item is an array that holds keys date
, visitors
, pageTitle
and pageViews
., (*35)
Total visitors and pageviews
public function fetchTotalVisitorsAndPageViews(Period $period): Collection
The function returns a Collection
in which each item is an array that holds keys date
, visitors
, and pageViews
., (*36)
Most visited pages
public function fetchMostVisitedPages(Period $period, int $maxResults = 20): Collection
The function returns a Collection
in which each item is an array that holds keys url
, pageTitle
and pageViews
., (*37)
Top referrers
public function fetchTopReferrers(Period $period, int $maxResults = 20): Collection
The function returns a Collection
in which each item is an array that holds keys url
and pageViews
., (*38)
Top browsers
public function fetchTopBrowsers(Period $period, int $maxResults = 10): Collection
The function returns a Collection
in which each item is an array that holds keys browser
and sessions
., (*39)
All other Google Analytics queries
To perform all other queries on the Google Analytics resource use performQuery
. Google's Core Reporting API provides more information on which metrics and dimensions might be used., (*40)
public function performQuery(Period $period, string $metrics, array $others = [])
You can get access to the underlying Google_Service_Analytics
object:, (*41)
Analytics::getAnalyticsService();
Testing
Run the tests with:, (*42)
bash
vendor/bin/phpunit
, (*43)
Contributing
Please see CONTRIBUTING for details., (*44)
Security
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*45)
Credits
About Spatie
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*46)
License
The MIT License (MIT). Please see License File for more information., (*47)