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

ActiveLibrary[API Development](/categories/api)

gnahotelsolutions/laravel-analytics
===================================

A Laravel package to retrieve Google Analytics data.

1.2(2y ago)041MITPHPPHP ^7.3|^8.0|^8.1

Since Jun 19Pushed 2y agoCompare

[ Source](https://github.com/gnahotelsolutions/laravel-analytics)[ Packagist](https://packagist.org/packages/gnahotelsolutions/laravel-analytics)[ Docs](https://github.com/gnahotelsolutions/laravel-analytics)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/gnahotelsolutions-laravel-analytics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (11)Versions (4)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)[![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 Spatie\Analytics\Facades\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 --tag="analytics-config"
```

The following config file will be published in `config/analytics.php`

```
return [

    /*
     * The property id of which you want to display data.
     */
    'property_id' => env('PROPERTY_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 select or create a project.

[![1](https://camo.githubusercontent.com/880944be41de033ddbe3154dcb243e43627fa4a6367eda7d9359afa1537e9896/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f312e706e67)](https://camo.githubusercontent.com/880944be41de033ddbe3154dcb243e43627fa4a6367eda7d9359afa1537e9896/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f312e706e67)

Next up we must specify which API’s the project may consume. Go to the API Library and search for "Google Analytics Data API".

[![2](https://camo.githubusercontent.com/d603e2ab12926c240e2c565ceeaee88880dd06586b4adfd4194348acb2edbb17/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f322e706e67)](https://camo.githubusercontent.com/d603e2ab12926c240e2c565ceeaee88880dd06586b4adfd4194348acb2edbb17/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f322e706e67)[![3](https://camo.githubusercontent.com/43f360e3114631af82276338f609c96c4d113f988e718cc014fcfae460b5bea5/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f332e706e67)](https://camo.githubusercontent.com/43f360e3114631af82276338f609c96c4d113f988e718cc014fcfae460b5bea5/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f332e706e67)

Choose enable to enable the API. [![4](https://camo.githubusercontent.com/b4a9df90a8ea7d8a5ded02ec5919031d81e0f16e0efe7b954f97a06cc5ae40e4/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f342e706e67)](https://camo.githubusercontent.com/b4a9df90a8ea7d8a5ded02ec5919031d81e0f16e0efe7b954f97a06cc5ae40e4/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f342e706e67)

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". [![5](https://camo.githubusercontent.com/4e530a670b5a1d64377454ac2cfce3eba516ad6a25b1f34f5dd3baad1ca353f8/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f352e706e67)](https://camo.githubusercontent.com/4e530a670b5a1d64377454ac2cfce3eba516ad6a25b1f34f5dd3baad1ca353f8/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f352e706e67)

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.

[![6](https://camo.githubusercontent.com/8eeec9e10b47fc91b8fa379c05c57b10bfd0287be47beb81a5faabbe769db6f4/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f362e706e67)](https://camo.githubusercontent.com/8eeec9e10b47fc91b8fa379c05c57b10bfd0287be47beb81a5faabbe769db6f4/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f362e706e67)

Go to the details screen of your created service account and select "keys", from the "Add key" dropdown select "Create new key".

[![7](https://camo.githubusercontent.com/273f5c7cdd946e4c6d21991bacf23033224315e6be158e478bc55c09c93ce6a1/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f372e706e67)](https://camo.githubusercontent.com/273f5c7cdd946e4c6d21991bacf23033224315e6be158e478bc55c09c93ce6a1/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f372e706e67)

Select "JSON" as the key type and click "Create" to download the JSON file.

[![8](https://camo.githubusercontent.com/232fdbf7663fd4cf6f75ed4b448931e527c9d57bddfa666a845234ded1f16067/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f382e706e67)](https://camo.githubusercontent.com/232fdbf7663fd4cf6f75ed4b448931e527c9d57bddfa666a845234ded1f16067/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f382e706e67)

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) and are using the new GA4 properties.

First you will need to know your property ID. In Analytics, go to Settings &gt; Property Settings. Here you will be able to copy your property ID. Use this value for the `ANALYTICS_PROPERTY_ID` key in your .env file.

[![a1](https://camo.githubusercontent.com/44fc0289799fe83868a86f1fedc67e61bdad6003148428f700e7342b41002e4b/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f61312e706e67)](https://camo.githubusercontent.com/44fc0289799fe83868a86f1fedc67e61bdad6003148428f700e7342b41002e4b/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f61312e706e67)

Now we will need to give access to the service account you created. Go to "Property Access Management" in the Admin-section of the property. Click the plus sign in the top right corner to add a new user.

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. Analyst role is enough.

[![a2](https://camo.githubusercontent.com/7b72b573b8d26ff4b8c35a90cd0a0ce37dc2405107d1bcf1964ef193a86cc31e/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f61322e706e67)](https://camo.githubusercontent.com/7b72b573b8d26ff4b8c35a90cd0a0ce37dc2405107d1bcf1964ef193a86cc31e/68747470733a2f2f7370617469652e6769746875622e696f2f6c61726176656c2d616e616c79746963732f76352f61322e706e67)

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

```
use Spatie\Analytics\Facades\Analytics;

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

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

`$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 page views

[](#visitors-and-page-views)

```
public function fetchVisitorsAndPageViews(Period $period): Collection
```

The function returns a `Collection` in which each item is an array that holds keys `activeUsers`, `screenPageViews` and `pageTitle`.

### Visitors and page views by date

[](#visitors-and-page-views-by-date)

```
public function fetchVisitorsAndPageViewsByDate(Period $period): Collection
```

The function returns a `Collection` in which each item is an array that holds keys `date`, `activeUsers`, `screenPageViews` and `pageTitle`.

### 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`, `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 `fullPageUrl`, `pageTitle` and `screenPageViews`.

### 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 `screenPageViews` and `pageReferrer`.

### 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 `activeUsers` and `newVsReturning` which can equal to `new` or `returning`.

### 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 `screenPageViews` and `browser`.

### All other Google Analytics queries

[](#all-other-google-analytics-queries)

For all other queries you can use the `get` function.

```
public function get(Period $period, array $metrics, array $dimensions = [], int $limit = 10, array $orderBy = [], FilterExpression $dimensionFilter = null): Collection
```

Here's some extra info on the arguments you can pass:

`Period $period`: a Spatie\\Analytics\\Period object to indicate that start and end date for your query.

`array $metrics`: an array of metrics to retrieve. You can find a list of all metrics [here](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema#metrics).

`array $dimensions`: an array of dimensions to group the results by. You can find a list of all dimensions [here](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema#dimensions).

`int $limit`: the maximum number of results to return.

`array $orderBy`: of OrderBy objects to sort the results by.

`array $offset`: Defaults to 0, you can use this in combination with the $limit param to have pagination.

For example:

```
$orderBy = [
    OrderBy::dimension('date', true),
    OrderBy::metric('pageViews', false),
];
```

`FilterExpression $dimensionFilter`: filter the result to include only specific dimension values. You can find more details [here](https://cloud.google.com/php/docs/reference/analytics-data/latest/V1beta.RunReportRequest).

For example:

```
use Google\Analytics\Data\V1beta\Filter;
use Google\Analytics\Data\V1beta\FilterExpression;
use Google\Analytics\Data\V1beta\Filter\StringFilter;
use Google\Analytics\Data\V1beta\Filter\StringFilter\MatchType;

$dimensionFilter = new FilterExpression([
    'filter' => new Filter([
        'field_name' => 'eventName',
        'string_filter' => new StringFilter([
            'match_type' => MatchType::EXACT,
            'value' => 'click',
        ]),
    ]),
]);
```

Testing
-------

[](#testing)

Run the tests with:

```
vendor/bin/pest
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  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

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.6% 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 ~4 days

Total

3

Last Release

1050d ago

PHP version history (2 changes)1.0PHP ^7.3

1.2PHP ^7.3|^8.0|^8.1

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (243 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)")[![timvandijck](https://avatars.githubusercontent.com/u/4528796?v=4)](https://github.com/timvandijck "timvandijck (9 commits)")[![juukie](https://avatars.githubusercontent.com/u/2678657?v=4)](https://github.com/juukie "juukie (8 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (7 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (6 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (5 commits)")[![torralbodavid](https://avatars.githubusercontent.com/u/17132682?v=4)](https://github.com/torralbodavid "torralbodavid (4 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (4 commits)")[![mansoorkhan96](https://avatars.githubusercontent.com/u/51432274?v=4)](https://github.com/mansoorkhan96 "mansoorkhan96 (3 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![Lloople](https://avatars.githubusercontent.com/u/5665466?v=4)](https://github.com/Lloople "Lloople (3 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (3 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)")[![gmbenedek](https://avatars.githubusercontent.com/u/7132072?v=4)](https://github.com/gmbenedek "gmbenedek (2 commits)")[![daveismynamecom](https://avatars.githubusercontent.com/u/60222583?v=4)](https://github.com/daveismynamecom "daveismynamecom (2 commits)")

---

Tags

laravelgoogleanalyticsreportsretrieve

###  Code Quality

TestsPest

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/gnahotelsolutions-laravel-analytics/health.svg)](https://phpackages.com/packages/gnahotelsolutions-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)
