PHPackages                             meftahi/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. meftahi/laravel-analytics

ActiveLibrary

meftahi/laravel-analytics
=========================

A Laravel 5 package to retrieve Google Analytics data.

3.1.1(8y ago)0241MITPHPPHP ^7.0

Since Mar 13Pushed 8y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (8)Versions (33)Used By (0)

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

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

[![Latest Version](https://camo.githubusercontent.com/ef2b341ece9aea6cfc3b5bb635e81aad7744793a0096250b51de1f57aa313e70/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f4d6566746168692f6c61726176656c2d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://github.com/Meftahi/laravel-analytics/releases)[![Total Downloads](https://camo.githubusercontent.com/f4d02769a9c1f83e33e86dc7f650bf58e9564f2156782eef6b9915a5d8316e74/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4d6566746168692f6c61726176656c2d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/Meftahi/laravel-analytics)

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

Here are a few examples of the provided methods:

```
use Meftahi\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.

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

> **If you're using v1 of this package, please refer to [an older version of this readme](https://github.com/Meftahi/laravel-analytics/blob/88eb75beadcd8dade2f3ee2423f3716253b2104d/README.md) for the installation and usage guide.**

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: Meftahi, Samberstraat 69D, 2060 Antwerp, Belgium.

All postcards are published [on our website](https://Meftahi.be/en/opensource/postcards).

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

[](#installation)

This package can be installed through Composer.

```
composer require Meftahi/laravel-analytics
```

In Laravel 5.5 the package will autoregister the service provider. In Laravel 5.4 you must install this service provider.

```
// config/app.php
'providers' => [
    ...
    Meftahi\Analytics\AnalyticsServiceProvider::class,
    ...
];
```

In Laravel 5.5 the package will autoregister the facade. In Laravel 5.4 you must install the facade manually.

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

Optionally, you can publish the config file of this package with this command:

```
php artisan vendor:publish --provider="Meftahi\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.
     */
    '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/e06dc55f449b62c5cd8c5d8c1ec03291d57383e21bc795b4eeb7f3e830d1d073/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f312e6a7067)](https://camo.githubusercontent.com/e06dc55f449b62c5cd8c5d8c1ec03291d57383e21bc795b4eeb7f3e830d1d073/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f312e6a7067)

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/c7ed4edc867f59e36ceffca6bd45157718066838751481c9b7d45dc73367272a/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f322e6a7067)](https://camo.githubusercontent.com/c7ed4edc867f59e36ceffca6bd45157718066838751481c9b7d45dc73367272a/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f322e6a7067)

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/db53d4b0c19b933f33029c6dabb8cbe2351fdd0e97a61f6eeb1413d33c929c17/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f332e6a7067)](https://camo.githubusercontent.com/db53d4b0c19b933f33029c6dabb8cbe2351fdd0e97a61f6eeb1413d33c929c17/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f332e6a7067)

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/6b38194896a7c3e28c2ee92b5c4cf1b32d88f8853393cd78e41bfa7d1cefd19f/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f342e6a7067)](https://camo.githubusercontent.com/6b38194896a7c3e28c2ee92b5c4cf1b32d88f8853393cd78e41bfa7d1cefd19f/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f342e6a7067)

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/b64ea40063891a4efea9931662647671f0126fd548c7572747bd4113cc15e193/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f352e6a7067)](https://camo.githubusercontent.com/b64ea40063891a4efea9931662647671f0126fd548c7572747bd4113cc15e193/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f352e6a7067)

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/bc11d581089c6ebf4a2ad8ad0034c6ed98e5245dd37dbb5f1f121a598daa740d/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f362e6a7067)](https://camo.githubusercontent.com/bc11d581089c6ebf4a2ad8ad0034c6ed98e5245dd37dbb5f1f121a598daa740d/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f362e6a7067)

### 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/4892791734d3376200458fda7525d0f0c743d72d86cc7f761d921bafe148cb42/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f372e6a7067)](https://camo.githubusercontent.com/4892791734d3376200458fda7525d0f0c743d72d86cc7f761d921bafe148cb42/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f372e6a7067)

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

[![8](https://camo.githubusercontent.com/01e37c056d71c2b71b6e901ec6b3f32eb0c19f4092bad11edbaead38334fe8f6/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f382e6a7067)](https://camo.githubusercontent.com/01e37c056d71c2b71b6e901ec6b3f32eb0c19f4092bad11edbaead38334fe8f6/68747470733a2f2f4d6566746168692e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76322f382e6a7067)

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)

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 74.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 ~30 days

Recently: every ~40 days

Total

32

Last Release

3139d 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

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/c6f38f1acce5e3bf5e1bfb29f45c929888b89ec06538899fa51b9f87a1df64dc?d=identicon)[meftahi](/maintainers/meftahi)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (188 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)")[![meftahi](https://avatars.githubusercontent.com/u/14029202?v=4)](https://github.com/meftahi "meftahi (6 commits)")[![mbardelmeijer](https://avatars.githubusercontent.com/u/1583095?v=4)](https://github.com/mbardelmeijer "mbardelmeijer (2 commits)")[![adaojunior](https://avatars.githubusercontent.com/u/4921294?v=4)](https://github.com/adaojunior "adaojunior (2 commits)")[![maartenbode](https://avatars.githubusercontent.com/u/1608377?v=4)](https://github.com/maartenbode "maartenbode (1 commits)")[![Rkallenkoot](https://avatars.githubusercontent.com/u/3198607?v=4)](https://github.com/Rkallenkoot "Rkallenkoot (1 commits)")[![sangnguyenplus](https://avatars.githubusercontent.com/u/6972407?v=4)](https://github.com/sangnguyenplus "sangnguyenplus (1 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)")[![threesquared](https://avatars.githubusercontent.com/u/892142?v=4)](https://github.com/threesquared "threesquared (1 commits)")[![thunder87](https://avatars.githubusercontent.com/u/13064503?v=4)](https://github.com/thunder87 "thunder87 (1 commits)")[![yajra](https://avatars.githubusercontent.com/u/2687997?v=4)](https://github.com/yajra "yajra (1 commits)")[![al0mie](https://avatars.githubusercontent.com/u/5223895?v=4)](https://github.com/al0mie "al0mie (1 commits)")[![amaelftah](https://avatars.githubusercontent.com/u/17250137?v=4)](https://github.com/amaelftah "amaelftah (1 commits)")[![blueclock](https://avatars.githubusercontent.com/u/586174?v=4)](https://github.com/blueclock "blueclock (1 commits)")[![BrandonSurowiec](https://avatars.githubusercontent.com/u/5625680?v=4)](https://github.com/BrandonSurowiec "BrandonSurowiec (1 commits)")

---

Tags

laravelgoogleanalyticsreportsretrievemeftahi

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

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

Dashboard and code-driven configuration for Laravel queues.

4.2k84.2M225](/packages/laravel-horizon)

PHPackages © 2026

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