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

ActiveLibrary

phonghaw2/laravel-google-analytics
==================================

A Laravel package to retrieve Google Analytics data.

115PHP

Since Nov 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/phonghaw2/laravel-google-analytics)[ Packagist](https://packagist.org/packages/phonghaw2/laravel-google-analytics)[ RSS](/packages/phonghaw2-laravel-google-analytics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Retrieve data from Google Analytics (v4)
========================================

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

[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/27be07466d40bfc51054e95fc2852443a4c51146233edb2b1161b65cbe7ebec1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70686f6e67686177322f6c61726176656c2d676f6f676c652d616e616c79746963732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phonghaw2/laravel-google-analytics)

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

[](#installation)

> For Laravel ^6.0|^7.0|^8.0

This package can be installed through Composer.

```
composer require phonghaw2/laravel-google-analytics
```

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

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

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('ANALYTICS_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',
    ],
];
```

### Getting credentials

[](#getting-credentials)

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

Usage
-----

[](#usage)

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

Here are a few examples of the provided methods:

```
use Phonghaw2\Analytics\AnalyticsFacade;
use Phonghaw2\Analytics\Period;

// You can set data with
Period::set($startDate, $endDate);
// https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/DateRange
// startDate, $endDate : The format NdaysAgo, yesterday, or today is also accepted

// Fetch the pages view for today and the 20 days ago
AnalyticsFacade::fetchPageViews(Period::set('20daysAgo', 'today'));

// Retrieve pageview data for the current day and the last seven days
$analyticsData = Analytics::fetchPageViews(Period::days(7));

// Retrieve pageviews since the 6 months ago
$analyticsData = Analytics::fetchPageViews(Period::months(6));

// fetch data
use \Google\Service\AnalyticsData\Dimension;
use \Google\Service\AnalyticsData\Metric;
$dimensions = new Dimension(array('name' => 'pageTitle'));
$metrics = new Metric(array('name' => 'screenPageViews'));
Analytics::fetch(Period::days(7), $dimensions, $metrics);
```

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

[](#provided-methods)

### Or The total amount of time (in seconds) your website or app was in the foreground of users' devices.

[](#or-the-total-amount-of-time-in-seconds-your-website-or-app-was-in-the-foreground-of-users-devices)

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

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

### 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/v4) provides more information on which metrics and dimensions might be used.

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

### Changelog

[](#changelog)

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

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

[](#contributing)

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

License
-------

[](#license)

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

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity20

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/77c6e6a0ec8705863f9b64d495e2a45f8b8e3a0bf685774a706be5eff27615f5?d=identicon)[phonghaw2](/maintainers/phonghaw2)

---

Top Contributors

[![phonghaw2](https://avatars.githubusercontent.com/u/77913075?v=4)](https://github.com/phonghaw2 "phonghaw2 (5 commits)")

### Embed Badge

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

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

PHPackages © 2026

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