PHPackages                             benwilkins/laravel-analyst - 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. benwilkins/laravel-analyst

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

benwilkins/laravel-analyst
==========================

An analytics package for Laravel

v1.1(7y ago)32.7kPHP

Since Nov 18Pushed 7y ago2 watchersCompare

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

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

Laravel Analyst
===============

[](#laravel-analyst)

[![Latest Version](https://camo.githubusercontent.com/a8a42fcf560faebbae3cebc6bbb17c18d74beb9fb288113363f2f48aaf8253a2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f62656e77696c6b696e732f6c61726176656c2d616e616c7973742e7376673f7374796c653d666c61742d737175617265)](https://github.com/benwilkins/laravel-analyst/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Use this package to retrieve analytics about your app, from one or many data sources. Currently, this package supports an internal data source and Google Analytics.

Install
-------

[](#install)

This package can be installed through Composer.

```
composer require benwilkins/laravel-analyst
```

Once installed, add the service provider:

```
// config/app.php
'providers' => [
    ...
    Benwilkins\Analyst\AnalystServiceProvider::class,
    ...
];
```

Publish the config file:

```
php artisan vendor:publish --provider="Benwilkins\Analyst\AnalystServiceProvider"
```

This package also comes with a facade, making it easy to call the class:

```
// config/app.php
'aliases' => [
    ...
    'Analyst' => Benwilkins\Analyst\AnalystFacade::class,
    ...
];
```

The following config file will be published in `config/laravel-analyst.php`

```
return [
    /*
     * Path to the client secret json file.
     */
    'google_account_credentials_json' => storage_path('app/laravel-analyst/Google/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,
    /*
     * The directory where the underlying Google_Client will store it's cache files.
     */
    'cache_location' => storage_path('app/laravel-analyst/'),
    /*
     * The directory where custom internal metrics are stored.
     */
    'custom_metric_location' => '/app/Metrics/',
    /*
     * Default data client
     */
    'default_client' => 'internal',
];
```

Internal Client
---------------

[](#internal-client)

The internal client can be used to pull analytics from within your app's databse. By default, the client comes with one metric: `NewUsersMetric`.

### Custom Metrics

[](#custom-metrics)

Custom metrics for the internal client can be used by creating a Metric class and adding it to the custom metric directory. By default, that directory is `/app/Metrics/`, but can be changed from the config.

To add a custom metric:

1. Create a new class with the namespace `Benwilkins\Analyst\Clients\Internal\Metrics`. The class should extend the Metric abstract class.
2. Your class must implement the `run` method. This method takes two arguments: `$period` and `$params`. The period is an instance of the Period class that defines the date range for the metric. The params argument allows custom parameters to be applied to the metric, such as filters.

```
// app/Metrics/MyNewMetric.php
namespace Benwilkins\Analyst\Clients\Inernal\Metrics;

use Benwilkins\Analyst\Period;

class MyNewMetric extends Metric
{
    public function run(Period $period, $params = [])
    {
        // Code to run the metric
    }
}
```

### Example Usage

[](#example-usage)

To use the Internal client, follow this example:

```
use Benwilkins\Analyst\Period;

$data = Analyst::metric('new users', Period::days(30));
```

Google Analytics Client
-----------------------

[](#google-analytics-client)

The Google Analytics Client uses the Analytics Reporting API. You'll need to get a Google Service Account Key from the Google Developers Console:

1. In the console, follow the steps to create a new app.
2. Enable the Analytics Reporting API
3. Click on the Credentials tab, and follow the steps to create credentials.
4. Select Service Account Key as the credential type.
5. Select a New Service Account and give it a name. Choose JSON as the Key Type.
6. Download the JSON file and save it in the location specified in the package config file.

Finally, you'll need to grant permissions to your Google Analytics property:

1. Go to "User Management" in the admin section of the property.
2. Use the email address found in the `client_email` key from the JSON file you downloaded. Read only access is sufficient.

### Options

[](#options)

The Google Analytics client offers some options for configuration:

- `viewId` (required): The ID for the Google Analytics view you wish to pull from. This can be set as a default in the config file.
- `dimensions`: An array of dimensions to add to the query.
- `alias`: An optional alias to use for the metric name
- `groupByDimensions`: This option allows you to group results by dimensions. Pass an array of the indicies of the dimensions you wish you group by from the `dimensions` option. See example below.

### Example

[](#example)

This example queries for events, accepting the `ga:eventCategory`, `ga:eventAction`, `ga:eventLabel`, and `ga:date` dimensions. The results are grouped by a combination of `ga:eventCategory` and `ga:eventAction`.

```
$metric = Analyst::metric(
    ['ga:totalEvents'],
    Period::days(14),
    Analyst::client('google'),
    [
        'viewId' => 'XXXXXX', //  ['ga:eventCategory', 'ga:eventAction', 'ga:eventLabel', 'ga:date'],
        'alias' => ['events'],
        'groupByDimensions' => [0, 1]
    ]);
```

The value of `$metric` will be an instance of `AnalystDataCollection`.

Accessing your data
-------------------

[](#accessing-your-data)

### Analyst Data Collections

[](#analyst-data-collections)

Both clients return an instance of `AnalystDataCollection`. This collection has some accessor methods making it easy to use your data:

- `getTotal`: Returns the total number for the metric requested.
- `getGroups`: Returns an array of `AnalystDataGroup` objects.
- `getRaw`: Returns the data in the original raw format.

### Analyst Data Groups

[](#analyst-data-groups)

The `AnalystDataGroup` class is a grouping of your data. Each metric call will have a minimum of one group. A group consists of two main properties: `total`, and `points`.

- `getTotal`: Returns the total metric number for that grouping.
- `getPoints`: Returns the data points for each group, formatted in a way that can be used with Google Charts.

Roadmap
-------

[](#roadmap)

- Add caching to Google and Internal clients
- Add Artisan command to make new internal metrics

Security
--------

[](#security)

If you discover any security related issues, please use the issue tracker on GitHub.

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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

Every ~853 days

Total

2

Last Release

2609d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0f40c4512ac775dd4f5eddaefa6de2708291387ff360c0bef5d0529fd89f45c4?d=identicon)[benwilkins](/maintainers/benwilkins)

---

Top Contributors

[![benwilkins](https://avatars.githubusercontent.com/u/1360229?v=4)](https://github.com/benwilkins "benwilkins (12 commits)")

### Embed Badge

![Health badge](/badges/benwilkins-laravel-analyst/health.svg)

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

###  Alternatives

[spatie/laravel-google-calendar

Manage events on a Google Calendar

1.4k1.5M21](/packages/spatie-laravel-google-calendar)[tomatophp/filament-seo

Manage and generate SEO tags and integrate your website with Google SEO services

112.6k1](/packages/tomatophp-filament-seo)[infoweb-internet-solutions/yii2-cms-analytics

Analytics module for Yii2

112.4k1](/packages/infoweb-internet-solutions-yii2-cms-analytics)

PHPackages © 2026

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