PHPackages                             iammolinadev/lumen-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. iammolinadev/lumen-analytics

ActiveLibrary

iammolinadev/lumen-analytics
============================

A laravel package to retrieve Google Analytics data.

5.0.6(4y ago)05MITPHPPHP ^7.3|^8.0

Since Mar 13Pushed 4y agoCompare

[ Source](https://github.com/iammolinadev/iammolinadev-laravel-analytics)[ Packagist](https://packagist.org/packages/iammolinadev/lumen-analytics)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/iammolinadev-lumen-analytics/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (2)Dependencies (10)Versions (59)Used By (0)

[![Social Card of Laravel Analytics](/art/socialcard.png)](/art/socialcard.png)

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)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Test Status](https://camo.githubusercontent.com/afe7ab9a13b4afca94ca039fed40d7975d15cd441d1e826c526c46bd4d6f6f58/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d616e616c79746963732f72756e2d74657374733f6c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/afe7ab9a13b4afca94ca039fed40d7975d15cd441d1e826c526c46bd4d6f6f58/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d616e616c79746963732f72756e2d74657374733f6c6162656c3d7465737473267374796c653d666c61742d737175617265)[![Check & fix styling](https://github.com/spatie/laravel-analytics/workflows/Check%20&%20fix%20styling/badge.svg)](https://github.com/spatie/laravel-analytics/workflows/Check%20&%20fix%20styling/badge.svg)[![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 Analytics;
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.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/93cbc0998d5a5183fe03b7b615f83e0379b01dc8d92ef5b2a4dad1e75e654807/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d616e616c79746963732e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-analytics)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

This package can be installed through Composer.

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

Optionally, 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/analytics.php`

```
return [

    /*
     * The view id of which you want to display data.
     */
    'view_id' => env('ANALYTICS_VIEW_ID'),

    /*
     * Path to the client secret json file. Take a look at the README of this package
     * to learn how to get this file. You can also pass the credentials as an array
     * instead of a file path.
     */
    'service_account_credentials_json' => storage_path('app/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,

    /*
     * 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' => [
        'store' => 'file',
    ],
];
```

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 are a few examples using periods

```
//retrieve visitors and pageview data for the current day and the last seven days
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

//retrieve visitors and pageviews since the 6 months ago
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::months(6));

//retrieve sessions and pageviews with yearMonth dimension since 1 year ago
$analyticsData = Analytics::performQuery(
    Period::years(1),
    'ga:sessions',
    [
        'metrics' => 'ga:sessions, ga:pageviews',
        'dimensions' => 'ga:yearMonth'
    ]
);
```

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

### User Types

[](#user-types)

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

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

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](.github/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)

And a special thanks to [Caneco](https://twitter.com/caneco) for the logo ✨

License
-------

[](#license)

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

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 70.9% 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 ~45 days

Recently: every ~52 days

Total

58

Last Release

1486d ago

Major Versions

0.0.1 → 1.0.02015-03-13

1.4.1 → 2.0.02016-06-20

v2.x-dev → v3.0.x-dev2017-04-27

3.11.0 → 4.0.02021-03-26

4.0.1 → 5.0.02021-09-15

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

1.0.2PHP &gt;=5.4.0

2.0.0PHP ^7.0

3.7.0PHP ^7.2

3.10.2PHP ^7.3|^8.0

4.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/272fdf3e48559beffdb17bbd2205cc3cfa28e9c90adba8dc012dba86c9b34425?d=identicon)[iammolinadev](/maintainers/iammolinadev)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (227 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (17 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 (6 commits)")[![jfuenzalida-Keiron](https://avatars.githubusercontent.com/u/63429836?v=4)](https://github.com/jfuenzalida-Keiron "jfuenzalida-Keiron (5 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (4 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (3 commits)")[![daveismynamecom](https://avatars.githubusercontent.com/u/60222583?v=4)](https://github.com/daveismynamecom "daveismynamecom (2 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (2 commits)")[![mbardelmeijer](https://avatars.githubusercontent.com/u/1583095?v=4)](https://github.com/mbardelmeijer "mbardelmeijer (2 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![adaojunior](https://avatars.githubusercontent.com/u/4921294?v=4)](https://github.com/adaojunior "adaojunior (2 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")[![TheoKouzelis](https://avatars.githubusercontent.com/u/4980126?v=4)](https://github.com/TheoKouzelis "TheoKouzelis (1 commits)")[![thunder87](https://avatars.githubusercontent.com/u/13064503?v=4)](https://github.com/thunder87 "thunder87 (1 commits)")[![karalan](https://avatars.githubusercontent.com/u/14891163?v=4)](https://github.com/karalan "karalan (1 commits)")[![maartenbode](https://avatars.githubusercontent.com/u/1608377?v=4)](https://github.com/maartenbode "maartenbode (1 commits)")[![mansoorkhan96](https://avatars.githubusercontent.com/u/51432274?v=4)](https://github.com/mansoorkhan96 "mansoorkhan96 (1 commits)")

---

Tags

laravelgooglelumenanalyticsreportsretrieveiammolinadev

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/iammolinadev-lumen-analytics/health.svg)

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

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M56](/packages/spatie-laravel-analytics)[outerweb/image-library

Store and link files to your models

1113.0k2](/packages/outerweb-image-library)

PHPackages © 2026

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