PHPackages                             fr30n/ganalytics - 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. fr30n/ganalytics

ActiveLibrary[API Development](/categories/api)

fr30n/ganalytics
================

A Laravel 5 package to retrieve Google Analytics data.

1.0(7y ago)01MITPHPPHP ^7.2

Since Mar 28Pushed 7y agoCompare

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

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

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

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

[![Latest Version](https://camo.githubusercontent.com/8184d23b363386aa658a28b2c313fda9774b399911496122d57d9ed7eaf4302d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f667233306e2f67616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://github.com/fr30n/ganalytics/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/44a52c015b711f968b516453b58a1551469ac88c7c0b9b0ce78dcb09e022ffb5/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f667233306e2f67616e616c79746963732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/fr30n/ganalytics)[![Quality Score](https://camo.githubusercontent.com/27867564430c276726de0ab1fbab28555e4852d435921278878fa75f94c51220/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f667233306e2f67616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/fr30n/ganalytics-analytics)[![StyleCI](https://camo.githubusercontent.com/a8fb3982830f0c93b17b270289dd550314a265586f64f50c331f1cd2f1439705/68747470733a2f2f7374796c6563692e696f2f7265706f732f3137383230363430382f736869656c64)](https://styleci.io/repos/178206408)[![Total Downloads](https://camo.githubusercontent.com/0437e646360398dfd5d15dd8bfea2f99106e3bfba4f53bd033c6f03b295e6c86/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f667233306e2f67616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/fr30n/ganalytics)

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

Here are a few examples of the provided methods:

```
use GAnalytics;
use Fr3on\GAnalytics\Period;

//fetch the most visited pages for today and the past week
GAnalytics::fetchMostVisitedPages(Period::days(7));

//fetch visitors and page views for the past week
GAnalytics::fetchVisitorsAndPageViews(Period::days(7));
```

Most methods will return an `\Illuminate\Support\Collection` object containing the results.

Fr3on is a software agency in Cairo, Egypt. You'll find an overview of all our open source projects [on our website](https://fr30n.com).

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

[](#installation)

This package can be installed through Composer.

```
composer require fr30n/ganalytics
```

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

```
php artisan vendor:publish --provider="Fr30n\GAnalytics\GAnalyticsServiceProvider"
```

The following config file will be published in `config/ganalytics.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/ganalytics/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',
    ],
];
```

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 = GAnalytics::fetchVisitorsAndPageViews(Period::days(7));

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

//retrieve sessions and pageviews with yearMonth dimension since 1 year ago
$analyticsData = GAnalytics::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:

```
GAnalytics::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](CONTRIBUTING.md) for details.

Security
--------

[](#security)

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

Credits
-------

[](#credits)

- [Ahmed Mordy](https://github.com/fr30n)
- [All Contributors](../../contributors)

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

[](#support-us)

Fr3on is a software agency based in Cairo, Egypt. You'll find an overview of all our open source projects [on our website](https://fr30n.com).

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/fr30n). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

License
-------

[](#license)

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

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

2600d ago

### Community

Maintainers

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

---

Top Contributors

[![fr30n](https://avatars.githubusercontent.com/u/48392651?v=4)](https://github.com/fr30n "fr30n (6 commits)")

---

Tags

analyticsganalyticsgooglelaravelphpstatisticslaravelgoogleanalyticsreportsretrievefr30nganalytics

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/fr30n-ganalytics/health.svg)

```
[![Health](https://phpackages.com/badges/fr30n-ganalytics/health.svg)](https://phpackages.com/packages/fr30n-ganalytics)
```

###  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)
