PHPackages                             laravel2016/laravel-analytics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. laravel2016/laravel-analytics

ActiveLibrary[API Development](/categories/api)

laravel2016/laravel-analytics
=============================

A Laravel 5 package to retrieve Google Analytics data.

2.4.0(9y ago)09MITPHPPHP ^7.0

Since Mar 13Pushed 9y agoCompare

[ Source](https://github.com/laravel2016/lara-analytics)[ Packagist](https://packagist.org/packages/laravel2016/laravel-analytics)[ Docs](https://github.com/spatie/laravel-analytics)[ RSS](/packages/laravel2016-laravel-analytics/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (7)Versions (29)Used By (0)

Retrieve data from Google Analytics
===================================

[](#retrieve-data-from-google-analytics)

[![Latest Version](https://camo.githubusercontent.com/8466204fa0fcbc34f743149410057e712138991568d23bd60d2456e41743ced3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f7370617469652f6c61726176656c2d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://github.com/spatie/laravel-analytics/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/a9543b2aba23b2b2f5fe7b30bfa10471d8e4fa0ec989449c7735a0143267bbff/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d616e616c79746963732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-analytics)[![Quality Score](https://camo.githubusercontent.com/e84f0c00627a34510667289eed55fc9fbebefa07ed8e76410faa9f42ff0609aa/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-analytics)[![SensioLabsInsight](https://camo.githubusercontent.com/c7728e8d8428370203571f3f2d5b50f6c5c171d9310e09a09f3b123bc451af6f/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f32326536353261302d303163302d346238632d396164612d6339626135636261313763372e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/22e652a0-01c0-4b8c-9ada-c9ba5cba17c7)[![StyleCI](https://camo.githubusercontent.com/9c068448e03e45f62859a1c3d3a944b612b0c453f66cf2592093c1c694a0c677/68747470733a2f2f7374796c6563692e696f2f7265706f732f33323036373038372f736869656c64)](https://styleci.io/repos/32067087)[![Total Downloads](https://camo.githubusercontent.com/b59e2c97d6e46e329e91338a6281b38ec1f4ab41aaf2b3d75cf541ad3361c5eb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-analytics)

Using this package you can easily retrieve data from Google Analytics.

Here are a few examples of the provided methods:

```
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.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), 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.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

Install
-------

[](#install)

This package can be installed through Composer.

```
composer require spatie/laravel-analytics
```

You must install this service provider.

```
// 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.

```
// config/app.php
'aliases' => [
    ...
    'Analytics' => Spatie\Analytics\AnalyticsFacade::class,
    ...
];
```

You can publish the config file of this package with this command:

```
php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider"
```

The following config file will be published in `config/laravel-analytics.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
------------------------------------------------------------------

[](#how-to-obtain-the-credentials-to-communicate-with-google-analytics)

### Getting credentials

[](#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](https://console.developers.google.com/apis) and click "Select a project" in the header.

[![1](https://camo.githubusercontent.com/c2806a0a3ccb46c93b57571b245a9e4ab33fca8fc931ebb5b479e2ca9a4b0b9c/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f312e6a7067)](https://camo.githubusercontent.com/c2806a0a3ccb46c93b57571b245a9e4ab33fca8fc931ebb5b479e2ca9a4b0b9c/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f312e6a7067)

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".

[![2](https://camo.githubusercontent.com/c4e447f4779243aa4f252dcc8fbd2f69ab75778cd84f7ead0a223d90fabe54a9/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f322e6a7067)](https://camo.githubusercontent.com/c4e447f4779243aa4f252dcc8fbd2f69ab75778cd84f7ead0a223d90fabe54a9/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f322e6a7067)

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".

[![3](https://camo.githubusercontent.com/bc2ea02a506a995d4337450c434ecd1942315607e63d306fa165abe248f0bcd6/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f332e6a7067)](https://camo.githubusercontent.com/bc2ea02a506a995d4337450c434ecd1942315607e63d306fa165abe248f0bcd6/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f332e6a7067)

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.

[![4](https://camo.githubusercontent.com/2fff29572b2baeda063cf4513c36a6a0a46cef5aeb8dd3f0e7635e15b75d48c1/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f342e6a7067)](https://camo.githubusercontent.com/2fff29572b2baeda063cf4513c36a6a0a46cef5aeb8dd3f0e7635e15b75d48c1/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f342e6a7067)

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.

### Granting permissions to your Analytics property

[](#granting-permissions-to-your-analytics-property)

I'm assuming that you've already created a Analytics account on the [Analytics site](https://analytics.google.com/analytics). Go to "User management" in the Admin-section of the property.

[![5](https://camo.githubusercontent.com/393c4779b578e29f1d1deda198520786a485f944db003c6a1290a706047e91a5/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f352e6a7067)](https://camo.githubusercontent.com/393c4779b578e29f1d1deda198520786a485f944db003c6a1290a706047e91a5/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f352e6a7067)

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://camo.githubusercontent.com/40c690440f70774f0ed9134bd68eb3da0833c28e549e5981a45900ad5b15d2ee/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f362e6a7067)](https://camo.githubusercontent.com/40c690440f70774f0ed9134bd68eb3da0833c28e549e5981a45900ad5b15d2ee/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f362e6a7067)

### Getting the view id

[](#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://camo.githubusercontent.com/d5a990edaf2479efae7a76d98de4e8a573e1708f7d0edc77f99c7bfcb62fe165/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f372e6a7067)](https://camo.githubusercontent.com/d5a990edaf2479efae7a76d98de4e8a573e1708f7d0edc77f99c7bfcb62fe165/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f372e6a7067)

You'll need the `View ID` displayed there.

[![8](https://camo.githubusercontent.com/b4971e0220859ff5a4103f6d7abcfa181043d919e7a348f29d44314698929d26/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f382e6a7067)](https://camo.githubusercontent.com/b4971e0220859ff5a4103f6d7abcfa181043d919e7a348f29d44314698929d26/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f382e6a7067)

Usage
-----

[](#usage)

When the installation is done you can easily retrieve Analytics data. Nearly all methods will return an `Illuminate\Support\Collection`-instance.

Here is an example to retrieve visitors and pageview data for the current day and the last seven days.

```
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
```

`$analyticsData` is a `Collection` in which each item is an array that holds keys `date`, `visitors` and `pageViews`

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.

```
$startDate = Carbon::now()->subYear();
$endDate = Carbon::now();

Period::create($startDate, $endDate);
```

Provided methods
----------------

[](#provided-methods)

### Visitors and pageviews

[](#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`.

### Total visitors and pageviews

[](#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`.

### Most visited pages

[](#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`.

### Top referrers

[](#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`.

### Top browsers

[](#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`.

### All other Google Analytics queries

[](#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.

```
public function performQuery(Period $period, string $metrics, array $others = [])
```

You can get access to the underlying `Google_Service_Analytics` object:

```
Analytics::getAnalyticsService();
```

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 77.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~26 days

Recently: every ~38 days

Total

27

Last Release

3396d ago

Major Versions

0.0.1 → 1.0.02015-03-13

1.4.1 → 2.0.02016-06-20

2.3.1 → v3.0.x-dev2017-01-02

PHP version history (3 changes)0.0.1PHP &gt;=5.5.0

1.0.2PHP &gt;=5.4.0

2.0.0PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c4c1b158db1bba6a5b749f653e8feb2187616f56528b071747b311f2421b693d?d=identicon)[laravel2016](/maintainers/laravel2016)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (168 commits)")[![MatthiasDeWinter](https://avatars.githubusercontent.com/u/8791525?v=4)](https://github.com/MatthiasDeWinter "MatthiasDeWinter (14 commits)")[![lukepolo](https://avatars.githubusercontent.com/u/2066668?v=4)](https://github.com/lukepolo "lukepolo (10 commits)")[![juukie](https://avatars.githubusercontent.com/u/2678657?v=4)](https://github.com/juukie "juukie (8 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (5 commits)")[![mbardelmeijer](https://avatars.githubusercontent.com/u/1583095?v=4)](https://github.com/mbardelmeijer "mbardelmeijer (2 commits)")[![maartenbode](https://avatars.githubusercontent.com/u/1608377?v=4)](https://github.com/maartenbode "maartenbode (1 commits)")[![sangnguyenplus](https://avatars.githubusercontent.com/u/6972407?v=4)](https://github.com/sangnguyenplus "sangnguyenplus (1 commits)")[![TheoKouzelis](https://avatars.githubusercontent.com/u/4980126?v=4)](https://github.com/TheoKouzelis "TheoKouzelis (1 commits)")[![blueclock](https://avatars.githubusercontent.com/u/586174?v=4)](https://github.com/blueclock "blueclock (1 commits)")[![threesquared](https://avatars.githubusercontent.com/u/892142?v=4)](https://github.com/threesquared "threesquared (1 commits)")[![BrandonSurowiec](https://avatars.githubusercontent.com/u/5625680?v=4)](https://github.com/BrandonSurowiec "BrandonSurowiec (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")[![jobbrown](https://avatars.githubusercontent.com/u/1885439?v=4)](https://github.com/jobbrown "jobbrown (1 commits)")[![karalan](https://avatars.githubusercontent.com/u/14891163?v=4)](https://github.com/karalan "karalan (1 commits)")[![laravel2016](https://avatars.githubusercontent.com/u/24533220?v=4)](https://github.com/laravel2016 "laravel2016 (1 commits)")

---

Tags

spatielaravelgoogleanalyticsreportsretrieve

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laravel2016-laravel-analytics/health.svg)

```
[![Health](https://phpackages.com/badges/laravel2016-laravel-analytics/health.svg)](https://phpackages.com/packages/laravel2016-laravel-analytics)
```

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[thujohn/analytics

Google Analytics for Laravel 4

113108.7k1](/packages/thujohn-analytics)[ozankurt/google-analytics

Laravel Google Analytics

7616.7k](/packages/ozankurt-google-analytics)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
