PHPackages                             api-analytics/laravel - 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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. api-analytics/laravel

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

api-analytics/laravel
=====================

API Analytics middleware for Laravel applications.

v0.1.0(1w ago)00MITPHPPHP ^8.0

Since Mar 10Pushed 1w agoCompare

[ Source](https://github.com/tom-draper/api-analytics-php-laravel)[ Packagist](https://packagist.org/packages/api-analytics/laravel)[ Docs](https://github.com/tom-draper/api-analytics-php-laravel)[ RSS](/packages/api-analytics-laravel/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (5)Versions (2)Used By (0)

API Analytics for Laravel
=========================

[](#api-analytics-for-laravel)

A free and lightweight API analytics solution, complete with a dashboard.

Getting Started
---------------

[](#getting-started)

### 1. Generate an API key

[](#1-generate-an-api-key)

Head to [apianalytics.dev/generate](https://apianalytics.dev/generate) to generate your unique API key with a single click.

### 2. Install the package

[](#2-install-the-package)

```
composer require api-analytics/laravel
```

### 3. Add your API key

[](#3-add-your-api-key)

Add to your `.env` file:

```
API_ANALYTICS_KEY=your-api-key-here
```

### 4. Register the middleware

[](#4-register-the-middleware)

Add to `app/Http/Kernel.php`:

```
// Global middleware (all routes)
protected $middleware = [
    // ... other middleware
    \ApiAnalytics\Laravel\AnalyticsMiddleware::class,
];

// Or for API routes only
protected $middlewareGroups = [
    'api' => [
        // ... other middleware
        \ApiAnalytics\Laravel\AnalyticsMiddleware::class,
    ],
];
```

### 5. View your analytics

[](#5-view-your-analytics)

Your API will now log incoming requests. View your dashboard at [apianalytics.dev/dashboard](https://apianalytics.dev/dashboard).

Configuration
-------------

[](#configuration)

Publish the config file (optional):

```
php artisan vendor:publish --tag=api-analytics-config
```

This creates `config/api-analytics.php`:

```
return [
    'api_key' => env('API_ANALYTICS_KEY', ''),
    'privacy_level' => env('API_ANALYTICS_PRIVACY_LEVEL', 0),
    'server_url' => env('API_ANALYTICS_SERVER_URL', 'https://www.apianalytics-server.com/'),
    'get_user_id' => null, // Optional callback
];
```

Custom User ID
--------------

[](#custom-user-id)

Track users by authenticated user or API key:

```
// In config/api-analytics.php
'get_user_id' => function (array $context) {
    $request = $context['request'] ?? null;

    // Use authenticated user ID
    if ($request?->user()) {
        return (string) $request->user()->id;
    }

    // Or fall back to API key header
    return $request?->header('X-API-Key') ?? '';
},
```

Or in a service provider for more control:

```
// app/Providers/AppServiceProvider.php
use ApiAnalytics\Core\Config;

public function register(): void
{
    $this->app->singleton(Config::class, function () {
        $config = new Config();
        $config->privacyLevel = 2;

        $config->setGetUserId(function (array $context) {
            $request = $context['request'] ?? null;
            return $request?->user()?->id ?? $request?->header('X-API-Key');
        });

        return $config;
    });
}
```

Privacy Levels
--------------

[](#privacy-levels)

Control IP address handling via environment:

```
API_ANALYTICS_PRIVACY_LEVEL=2
```

- `0` - IP stored, location inferred (default)
- `1` - Location inferred, IP discarded
- `2` - IP never sent

Facade Usage
------------

[](#facade-usage)

Access the analytics client directly:

```
use ApiAnalytics\Laravel\Facade as Analytics;

// Get buffer size
$count = Analytics::getBufferSize();

// Force flush
Analytics::flush();
```

Testing
-------

[](#testing)

Mock the client in tests:

```
use ApiAnalytics\Core\Client;

$this->mock(Client::class, function ($mock) {
    $mock->shouldReceive('logRequest')->andReturnNull();
    $mock->shouldReceive('createRequestData')->andReturn(
        new \ApiAnalytics\Core\RequestData('', null, '', '/', 'GET', 0, 200, null)
    );
});
```

Self-Hosting
------------

[](#self-hosting)

For self-hosted instances:

```
API_ANALYTICS_SERVER_URL=https://your-server.com/
```

More Information
----------------

[](#more-information)

- [Dashboard](https://apianalytics.dev/dashboard)
- [Documentation](https://github.com/tom-draper/api-analytics)
- [Privacy Policy](https://www.apianalytics.dev/privacy-policy)

###  Health Score

34

—

LowBetter than 79% of packages

Maintenance98

Actively maintained with recent releases

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity28

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

12d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5029aa82fac169a3ceb5ca659506358c2321b7b677379b9a5aa72b99e2030e83?d=identicon)[TomDraper](/maintainers/TomDraper)

---

Top Contributors

[![tom-draper](https://avatars.githubusercontent.com/u/41476809?v=4)](https://github.com/tom-draper "tom-draper (1 commits)")

---

Tags

middlewareapilaravelmonitoringanalytics

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[scoutapp/scout-apm-laravel

Scout Application Performance Monitoring Agent - https://scoutapm.com

23825.7k](/packages/scoutapp-scout-apm-laravel)[muhammadsadeeq/laravel-activitylog-ui

A beautiful, modern UI for Spatie's Activity Log with advanced filtering, analytics, and real-time features.

1747.9k](/packages/muhammadsadeeq-laravel-activitylog-ui)

PHPackages © 2026

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