PHPackages                             educare/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. educare/analytics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

educare/analytics
=================

Get whatever data you want from google analytics.

12PHP

Since Jan 9Pushed 3y ago1 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Get easily whatever data you want from google analytics API V4 using laravel php framework.
===========================================================================================

[](#get-easily-whatever-data-you-want-from-google-analytics-api-v4-using-laravel-php-framework)

This package helps php developers to use Google Analytics API V4 with convenient way. The code is clean and was written with good OOP practices. Any help to improve this package would be appreciated.

The package is compatible with laravel framework.

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

[](#installation)

Install the package via composer:

```
composer require educare/analytics
```

To use it with laravel add the GoogleAnalyticsServiceProvider to the `config/app.php`:

```
'providers' => [
...
Educare\Analytics\GoogleAnalyticsServiceProvider::class
]
```

If you want to use the facade of the package add it to the `config/app.php`:

```
'aliases' => [
...
'Analytics' => Educare\Analytics\Facades\Analytics::class
]
```

Copy the analytics config file with the command:

```
php artisan vendor:publish --provider="Educare\Analytics\GoogleAnalyticsServiceProvider"
```

For those who have a credential with google analytics api continue here otherwise look at [how to create credential with google analytics api](#create-credential-with-google-analytics-api-v4).

Be sure that you have `service-account-credentials.json` file within `storage\app\google-analytics\`.

Add the view id to `.env` file:

```
GOOGLE_ANALYTICS_VIEW_ID=324235464

```

Usage
-----

[](#usage)

#### You can get analytics data simply using the facade

[](#you-can-get-analytics-data-simply-using-the-facade)

`Educare\Analytics\Facades\Analytics`

##### To get all sessions of the last week

[](#to-get-all-sessions-of-the-last-week)

`Analytics::get();`

##### To get all sessions depends on the specific date range

[](#to-get-all-sessions-depends-on-the-specific-date-range)

```
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::get();
```

##### To get whatever data you want

[](#to-get-whatever-data-you-want)

```
Analytics::setDateRange('2016-10-01', '2016-11-25');
Analytics::setMaxResults(20);
Analytics::setMetrics(['ga:entrances', 'ga:pageviews', 'ga:bounceRate']);
Analytics::setDimension(['ga:pagePath', 'ga:pageTitle']);
Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '/i-want-to-get-all-data-that-has-this-page-path');
Analytics::setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
return Analytics::get();
```

`setMetrics` and `setDimension` methods accept an array containing the wanted metrics and dimension. Available [metrics and dimensions](https://developers.google.com/analytics/devguides/reporting/core/dimsmets)

`setDimensionFilter` accept 3 parameters. First parameter get the dimension name in which you want to filter analytics data. Second parameter get the [operator](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#operator) (REGEXP, BEGINS\_WITH, ENDS\_WITH and more) you want. Third parameter get the expression. For example if you want to get all analytics data in which the page path include `play` or `simple` words you can use: `Analytics::setDimensionFilter('ga:pagePath', 'REGEXP', '(\/play\/|\/simple\/)');`

If you want to get analytics data for multiple pages in a single request by their exact paths, you can use the 'IN\_LIST' operator and pass an array of paths as the third parameter. E.g. `Analytics::setDimensionFilter('ga:pagePath', 'IN_LIST', ['/i-want-data-for-this-path', '/and/this-path-too']);`

`setOrder` method accept 3 parameters. First parameter get the name in which you want to order the data. Second get [OrderType](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#ordertype) usually `VALUE`. Third get the [SortOrder](https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#sortorder) usually `ASCENDING` or `DESCENDING`.

#### Get data using Analytics contract

[](#get-data-using-analytics-contract)

`Educare\Analytics\Contracts\Analytics`

Instead of facade you can get analytics data using the Analytics interface:

```
use Educare\Analytics\Contracts\Analytics;

class GoogleAnalyticsController
{
    //inject analytics interface
    public function get(Analytics $analytics)
    {
        $analytics->setDateRange('2016-12-01', '2016-12-20');
        $analytics->setMaxResults(11);
        $analytics->setMetrics(['ga:pageviews', 'ga:uniquePageviews', 'ga:avgTimeOnPage', 'ga:entrances', 'ga:bounceRate']);
        $analytics->setDimension(['ga:pagePath', 'ga:pageTitle']);
        $analytics->setDimensionFilter('ga:pagePath', 'REGEXP', '(\/this-value-in-path\/|\/or-this-value-in-path\/)');
        $analytics->setOrder('ga:pageviews', 'VALUE', 'DESCENDING');
        return $analytics->get();
    }
}
```

Create credential with google analytics api v4
----------------------------------------------

[](#create-credential-with-google-analytics-api-v4)

Guide from google analytics api v4:

- Create a project using [Google API Console](https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key):
- Open the [Service accounts page](https://console.developers.google.com/permissions/serviceaccounts). If prompted, select your project that you have created and click open.
- Click `Create service account`.
- In the Create service account window, type a name for the service account, and select Furnish a new private key. In the key type select JSON and then click create. If you want to grant G Suite domain-wide authority to the service account, also select Enable G Suite Domain-wide Delegation.
- Copy Service account ID you have created and then go to [google analytics admin panel](https://analytics.google.com/analytics/web/#management/Settings) select `User Management` and add the account you have copied with the permissions that you want.
- Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
- Rename the **json** file to `service-account-credentials.json`.
- To get the **view id** you can use the [account explorer](https://ga-dev-tools.appspot.com/account-explorer/) or from google analytics `view settings` from [admin panel](https://analytics.google.com/analytics/web/#management/Settings).

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity23

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/f3d517e5c6de47e8ef8fb846217bedcae5222bec65e76f429c2a2ad90f4a9fde?d=identicon)[shahzadahmad](/maintainers/shahzadahmad)

---

Top Contributors

[![ShahzadAhmadShahzad](https://avatars.githubusercontent.com/u/16838671?v=4)](https://github.com/ShahzadAhmadShahzad "ShahzadAhmadShahzad (27 commits)")

### Embed Badge

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

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

###  Alternatives

[96qbhy/agora

声网php-SDK

121.5k](/packages/96qbhy-agora)

PHPackages © 2026

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