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

ActiveLibrary[API Development](/categories/api)

teodoriu/laravel-analytics
==========================

A Laravel package to retrieve Google Analytics (GA4) data.

0.0.1(5y ago)44.5k↓50%2[1 PRs](https://github.com/teodoriu/laravel-analytics/pulls)MITPHPPHP ^7.3|^8.0

Since Apr 27Pushed 3y ago2 watchersCompare

[ Source](https://github.com/teodoriu/laravel-analytics)[ Packagist](https://packagist.org/packages/teodoriu/laravel-analytics)[ Docs](https://github.com/teodoriu/laravel-analytics)[ Fund](https://www.buymeacoffee.com/teodor)[ RSS](/packages/teodoriu-laravel-analytics/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

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

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

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

Here are a few examples of the provided methods:

```
$start = \Illuminate\Support\Carbon::now()->startOfDay();
$end = \Illuminate\Support\Carbon::now()->endOfDay();

$totalUsers = Teodoriu\Analytics\Facades\Analytics::setDateRanges($start, $end)
                               ->setDimensions(
                                   [
                                       'hostName',
                                   ]
                               )
                               ->setMetrics(
                                   [
                                       'totalUsers',
                                   ]
                               )
                               ->runReport()
                               ->toArray();
```

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

[](#installation)

This package can be installed through Composer.

```
composer require teodoriu/laravel-analytics
```

Add in your .env

```
GOOGLE_CLOUD_PROJECT=project_id_from_google_console
GOOGLE_APPLICATION_CREDENTIALS=full_path_to_google_credentials_json
ANALYTICS_PROPERTY_ID=analytics_property_id
```

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

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

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

```
return [

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

    /*
     * 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,
];
```

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.

Next up we must specify which API’s the project may consume. In the list of available API’s click "Google Analytics Data API". On the next screen click "Enable".

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

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.

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.

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.

### Getting the property id

[](#getting-the-property-id)

The last thing you'll have to do is fill in the `property_id` in the config file. You can get the right value on the [Analytics site](https://analytics.google.com/analytics). Go to "Property Settings" in the Admin-section of the property.

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

Usage
-----

[](#usage)

### All other Google Analytics queries

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

To perform all other queries on the Google Analytics resource use `runReport`. [Google Analytics Data API (GA4)](https://developers.google.com/analytics/devguides/reporting/data/v1/basics) provides more information on which metrics and dimensions might be used.

[API Dimensions &amp; Metrics](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema) that are available

You can get access to the underlying `BetaAnalyticsDataClient` object:

```
Teodoriu\Analytics\Facades\Analytics::getAnalyticsService();
```

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

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

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

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

1841d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11290723?v=4)[Teodor B.](/maintainers/teodoriu)[@teodoriu](https://github.com/teodoriu)

---

Top Contributors

[![teodoriu](https://avatars.githubusercontent.com/u/11290723?v=4)](https://github.com/teodoriu "teodoriu (3 commits)")[![mend-bolt-for-github[bot]](https://avatars.githubusercontent.com/in/16809?v=4)](https://github.com/mend-bolt-for-github[bot] "mend-bolt-for-github[bot] (1 commits)")

---

Tags

laravelgoogleanalyticsreportsretrievega4teodoriu

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

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

The Statamic CMS Core Package

4.8k3.2M720](/packages/statamic-cms)[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)
