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

ActiveLibrary[API Development](/categories/api)

backstage/laravel-google-analytics-v4
=====================================

Laravel package to get insights in Google Analytics v4 data

v3.0.0(5mo ago)36783[1 PRs](https://github.com/backstagephp/laravel-google-analytics-v4/pulls)MITPHPPHP ^8.1CI failing

Since Feb 22Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/backstagephp/laravel-google-analytics-v4)[ Packagist](https://packagist.org/packages/backstage/laravel-google-analytics-v4)[ Docs](https://github.com/backstagephp/laravel-google-analytics-v4)[ GitHub Sponsors](https://github.com/vormkracht10)[ RSS](/packages/backstage-laravel-google-analytics-v4/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (14)Versions (33)Used By (0)

Laravel Google Analytics
========================

[](#laravel-google-analytics)

[![Latest Version on Packagist](https://camo.githubusercontent.com/25787414d41d76c6e74a0becd442273a5641be610e42047fd7096183e69cf7c7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6261636b73746167652f6c61726176656c2d676f6f676c652d616e616c79746963732d76342e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/backstage/laravel-google-analytics-v4)[![Tests](https://github.com/backstagephp/laravel-google-analytics-v4/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/backstagephp/laravel-google-analytics-v4/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/ebf82f48f3741c80940d5938d7294cc5578dc471d93d070eaa2347d67efbe5af/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6261636b73746167652f6c61726176656c2d676f6f676c652d616e616c79746963732d76342e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/backstage/laravel-google-analytics-v4)[![Packagist PHP Version Support](https://camo.githubusercontent.com/232b1cbd50cbc774bd541a9b53c5df005c9035a3ff919384461c75fb6d9cc19e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6261636b73746167652f6c61726176656c2d676f6f676c652d616e616c79746963732d7634)](https://packagist.org/packages/backstage/laravel-google-analytics-v4)[![PHPStan](https://github.com/backstagephp/laravel-google-analytics-v4/actions/workflows/phpstan.yml/badge.svg?branch=main)](https://github.com/backstagephp/laravel-google-analytics-v4/actions/workflows/phpstan.yml)[![GitHub release (latest by date)](https://camo.githubusercontent.com/d2eaa5d5cfc279d7c31c462ddbf7568eee94edd3691c831e1a19e09b36bb14da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6261636b73746167657068702f6c61726176656c2d676f6f676c652d616e616c79746963732d7634)](https://camo.githubusercontent.com/d2eaa5d5cfc279d7c31c462ddbf7568eee94edd3691c831e1a19e09b36bb14da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6261636b73746167657068702f6c61726176656c2d676f6f676c652d616e616c79746963732d7634)

About Laravel Google Analytics v4
---------------------------------

[](#about-laravel-google-analytics-v4)

Retrieve all data like pageviews, events, ecommerce transactions and more from Google Analytics version 4 using the Google API.

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

[](#installation)

You can install the package via composer:

```
composer require backstage/laravel-google-analytics-v4
```

You can publish the config file with:

```
php artisan vendor:publish --tag="google-analytics-config"
```

This is the contents of the published config file:

```
return [

    /*
    |--------------------------------------------------------------------------
    | Google Analytics ID
    |--------------------------------------------------------------------------
    |
    | The Google Analytics ID of the website you want to track.
    |
    */
    'property_id' => env('GOOGLE_ANALYTICS_PROPERTY_ID', null),

    /*
    |--------------------------------------------------------------------------
    | Google Analytics Client Secret
    |--------------------------------------------------------------------------
    |
    | The Google Analytics Client Secret of the website you want to track.
    |
    */
    'credentials' => env('GOOGLE_ANALYTICS_CREDENTIALS', storage_path('app/analytics/google-credentials.json')),
];
```

Usage
-----

[](#usage)

First you need to create a service account in the Google Cloud Console. You can find the instructions here: .

After you have created the service account, you need to download the credentials and save them in the `storage/app/analytics` folder. You can change the location of the credentials in the config file if you want.

After you have done this, you can use the package like this:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the average session duration for the last 7 days:
$averageSessionDuration = Analytics::averageSessionDuration(Period::days(7));
```

### Available periods

[](#available-periods)

```
// Set the period to the last x minutes:
Period::minutes(30);

// Set the period to the last x hours:
Period::hours(1);

// Set the period to the last x days:
Period::days(2);

// Set the period to the last x weeks:
Period::weeks(3);

// Set the period to the last x months:
Period::months(4);

// Set the period to the last x years:
Period::years(5);

// Set the period to a custom date range using a Carbon object:
$startDate = Carbon::now()->subDays(7);
$endDate = Carbon::now();

Period::create($startDate, $endDate);

// Set the period to since the given date:
Period::since(Carbon::now()->subDays(7));
```

> **Note:** Using multiple Carbon objects in the `Period::create()` method may possibly result in unexpected behaviour. It is recommended to pass the start and end date as a separate Carbon instance. For example: `Period::create(Carbon::instance($startDate), Carbon::instance($endDate))`. This will ensure that the start and end date are set correctly.

Available methods
-----------------

[](#available-methods)

### Demographic Analytics

[](#demographic-analytics)

Methods to retrieve demographic analytics data for your website or application. You can use these methods to get information such as the top used languages, total users by city or country and total users per gender. All of the methods take a Period object as a parameter to specify the time range for the analytics data.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the top users by language for the last 7 weeks, limit to top 10:
$data = Analytics::topUsersByLanguage(period: Period::weeks(7), limit: 10);

// Get the total users by language for the last 7 weeks:
$data = Analytics::totalUsersByLanguage(Period::weeks(7));

// Get the top users by city for the last 7 weeks, limit to top 5:
$data = Analytics::topUsersByCity(period: Period::weeks(7), limit: 5);

// Get the total users by city for the last 7 weeks:
$data = Analytics::totalUsersByCity(Period::weeks(7));

// Get the top users by country for the last 7 weeks, limit to top 10:
$data = Analytics::topUsersByCountry(period: Period::weeks(7), limit: 10);

// Get the total users by country for the last 7 weeks:
$data = Analytics::totalUsersByCountry(Period::weeks(7));

// Get the total users by gender for the last 7 weeks:
$data = Analytics::totalUsersByGender(Period::weeks(7));

// Get the total users by age group for the last 7 weeks
$data = Analytics::totalUsersByAgeGroup(Period::weeks(7));

// Get the total users by date per page for the last 7 weeks:
$data = Analytics::totalUsersByDatePerPage(Period::weeks(7));
```

### Device and OS Analytics

[](#device-and-os-analytics)

Methods to retrieve device and operating system analytics data for your website or application. You can use these methods to get information such as the top popular browsers, screen resolutions, and mobile devices used by your visitors. All of the methods take a Period object as a parameter to specify the time range for the analytics data.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the top users by device category for the last 1 year:
$data = Analytics::topUsersByDeviceCategory(Period::years(1));

// Get the top users by operating system for the last 1 year:
$data = Analytics::topUsersByOperatingSystem(Period::years(1));

// Get the top users by browser for the last 1 year, limit to top 10:
$data = Analytics::topUsersByBrowser(period: Period::years(1), limit: 10);

// Get the total users by browser for the last 1 year:
$data = Analytics::totalUsersByBrowser(Period::years(1));

// Get the top users by screen resolution for the last 1 year, limit to top 5:
$data = Analytics::topUsersByScreenResolution(period: Period::years(1), limit: 5);

// Get the total users by operating system for the last 1 year:
$data = Analytics::totalUsersByOperatingSystem(Period::years(1));

// Get the total users by device category for the last 1 year:
$data = Analytics::totalUsersByDeviceCategory(Period::years(1));

// Get the top users by mobile device branding for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceBranding(period: Period::years(1), limit: 10);

// Get the total users by mobile device branding for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceBranding(Period::years(1));

// Get the top users by mobile device model for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceModel(period: Period::years(1), limit: 10);

// Get the total users by mobile device model for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceModel(Period::years(1));

// Get the top users by mobile input selector for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileInputSelector(period: Period::years(1), limit: 10);

// Get the total users by mobile input selector for the last 1 year:
$data = Analytics::totalUsersByMobileInputSelector(Period::years(1));

// Get the top users by mobile device info for the last 1 year, limit to top 10:
$data = Analytics::topUsersByMobileDeviceInfo(period: Period::years(1), limit: 10);

// Get the total users by mobile device info for the last 1 year:
$data = Analytics::totalUsersByMobileDeviceInfo(Period::years(1));

// Get the top users by mobile device marketing name for the last 1 year, limit to top 10:
$data = Analytics::getTopUsersByMobileDeviceMarketingName(period: Period::years(1), limit: 10);

// Get the total users by mobile device marketing name for the last 1 year:
$data = Analytics::getTotalUsersByMobileDeviceMarketingName(Period::years(1));
```

### Pageviews Analytics

[](#pageviews-analytics)

Methods to retrieve pageview analytics data for your website or application. You can use these methods to get information such as the total views or by page from your visitors. All of the methods take a Period object as a parameter to specify the time range for the analytics data.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the total pageviews for the last 14 days:
$data = Analytics::totalViews(Period::days(14));

// Get the total pageviews for the last 14 days, grouped by date:
$data = Analytics::totalViewsByDate(Period::days(14));

// Get the total pageviews for the last 14 days, grouped by page:
$data = Analytics::totalViewsByPage(Period::days(14));

// Get the top viewed pages for the last 14 days:
$data = Analytics::topViewsByPage(Period::days(14));

// Get the least viewed pages for the last 14 days:
$data = Analytics::leastViewsByPage(Period::days(14));

// Get the total views for the last 14 days, grouped by page path:
$data = Analytics::totalViewsByPagePath(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page path:
$data = Analytics::topViewsByPagePath(Period::days(14));

// Get the total views for the last 14 days, grouped by page title:
$data = Analytics::totalViewsByPageTitle(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page title:
$data = Analytics::topViewsByPageTitle(Period::days(14));

// Get the total views for the last 14 days, grouped by page url:
$data = Analytics::totalViewsByPageUrl(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by page url:
$data = Analytics::topViewsByPageUrl(Period::days(14));

// Get the total views for the last 14 days, grouped by country:
$data = Analytics::totalViewsByCountry(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by country:
$data = Analytics::topViewsByCountry(Period::days(14));

// Get the total views for the last 14 days, grouped by city:
$data = Analytics::totalViewsByCity(Period::days(14));

// Get the top viewed pages for the last 14 days, grouped by city:
$data = Analytics::topViewsByCity(Period::days(14));
```

### Realtime Analytics

[](#realtime-analytics)

Methods to retrieve realtime analytics data for your website or application. You can use these methods to get information such as the current active visitors on your website. All of the methods take a Period object as a parameter to specify the time range for the analytics data. The default time range is set to 30 minutes when no Period object is passed.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the total active users for the last 30 minutes:
$data = Analytics::activeUsers();
```

### Resource Analytics

[](#resource-analytics)

Methods to retrieve resource analytics data for your website or application. You can use these methods to get information such as the top landing pages, exit pages and referrers but also your most important social media

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the top 10 referrals for the last 14 days:
$data = Analytics::getTopReferrers(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, by page path:
$data = Analytics::getTopReferrersByPagePath(period: Period::days(14), limit: 10);

// Get the top 10 referrals for the last 14 days, grouped by page title:
$data = Analytics::getTopReferrersByPageTitle(period: Period::days(14), limit: 10);

// Get the top 20 landing pages by page title for the last 14 days:
$data = Analytics::getTopLandingPagesByPageTitle(period: Period::days(14), limit: 20);

// Get the top 20 landing pages for the last 14 days:
$data = Analytics::getTopLandingPages(period: Period::days(14), limit: 20);

// Get the top 20 landing pages with query string by page title for the last 14 days, grouped by page title:
$data = Analytics::getLandingPagesPlusQueryStringAByPageTitle(period: Period::days(14), limit: 20);

// Get the top 5 traffic sources for the last 14 days:
$data = Analytics::getTopTrafficSources(period: Period::days(14), limit: 5);

// Get the top 10 backlinks for the last 14 days:
$data = Analytics::getTopBacklinks(period: Period::days(14), limit: 10);
```

### Sessions Analytics

[](#sessions-analytics)

Methods to retrieve session duration analytics data for your website or application. You can use these methods to get information such as the average session time from your visitors. All of the methods take a Period object as a parameter to specify the time range for the analytics data.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get total sessions for the last 7 days:
$data = Analytics::sessions(Period::days(7));

// Get total sessions for the last 7 days per page:
$data = Analytics::sessionsPerPage(Period::days(7));

// Get the average session duration for the last 7 days:
$data = Analytics::averageSessionDuration(Period::days(7));

// Get the average session duration for the last 7 days, grouped by date:
$data = Analytics::averageSessionDurationByDate(Period::days(7));

// Get the average session duration in seconds for the last 7 days, grouped by page:
$data = Analytics::averageSessionDurationInSecondsByPage(Period::days(7));

// Get the average page views per session for the last 7 days:
$data = Analytics::averagePageViewsPerSession(Period::days(7));

// Get the average page views per session for the last 7 days, grouped by date:
$data = Analytics::averagePageViewsPerSessionByDate(Period::days(7));

// Get the average session duration in seconds for the last 7 days:
$data = Analytics::averageSessionDurationInSeconds(Period::days(7));

// Get the average session duration in seconds for the last 7 days, grouped by date:
$data = Analytics::averageSessionDurationInSecondsByDate(Period::days(7));

// Get bounce rate for the last 7 days:
$data = Analytics::bounceRate(Period::days(7));

// Get bounce rate for the last 7 days, grouped by date:
$data = Analytics::bounceRateByDate(Period::days(7));

// Get bounce rate by page for the last 7 days:
$data = Analytics::bounceRateByPage(Period::days(7));
```

### Users Analytics

[](#users-analytics)

Methods to retrieve user analytics data for your website or application. You can use these methods to get information such as the total visitors or amount of visitors per device. All of the methods take a Period object as a parameter to specify the time range for the analytics data.

Here are some examples of how to use the methods:

```
use Backstage\Analytics\Facades\Analytics;
use Backstage\Analytics\Period;

// Get the total users for the last 5 weeks:
$data = Analytics::totalUsers(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by date:
$data = Analytics::totalUsersByDate(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session source:
$data = Analytics::totalUsersBySessionSource(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session medium:
$data = Analytics::totalUsersBySessionMedium(Period::weeks(5));

// Get the total users for the last 5 weeks, grouped by session device:
$data = Analytics::totalUsersBySessionDevice(Period::weeks(5));
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

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

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Bas van Dinther](https://github.com/backstagephp)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance82

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 63.5% 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 ~38 days

Recently: every ~73 days

Total

27

Last Release

162d ago

Major Versions

v0.5.1 → v1.0.02023-09-29

v1.2.7 → v2.0.02025-06-24

v2.0.2 → v3.0.02025-12-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c6a425dc8645907a118a007438172d58c2016773f54ab3a834beff172632f13?d=identicon)[ux](/maintainers/ux)

---

Top Contributors

[![Baspa](https://avatars.githubusercontent.com/u/10845460?v=4)](https://github.com/Baspa "Baspa (73 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (27 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (6 commits)")[![markvaneijk](https://avatars.githubusercontent.com/u/1925388?v=4)](https://github.com/markvaneijk "markvaneijk (6 commits)")[![Casmo](https://avatars.githubusercontent.com/u/385764?v=4)](https://github.com/Casmo "Casmo (3 commits)")

---

Tags

analyticsgooglegoogle-analyticsgoogle-analytics-4laravellaravel-packagelaravelbackstagelaravel-google-analytics-v4

###  Code Quality

TestsPest

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

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

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

###  Alternatives

[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.0k7.8M57](/packages/dedoc-scramble)[vormkracht10/laravel-google-analytics-v4

Laravel package to get insights in Google Analytics v4 data

367.1k](/packages/vormkracht10-laravel-google-analytics-v4)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[ryangjchandler/bearer

Minimalistic token-based authentication for Laravel API endpoints.

8129.8k](/packages/ryangjchandler-bearer)[combindma/laravel-facebook-pixel

Meta pixel integration for Laravel

4956.9k](/packages/combindma-laravel-facebook-pixel)[stechstudio/laravel-hubspot

A Laravel SDK for the HubSpot CRM Api

2971.0k](/packages/stechstudio-laravel-hubspot)

PHPackages © 2026

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