api-analytics/laravel

API Analytics middleware for Laravel applications.

Maintainers

Package info

github.com/tom-draper/api-analytics-php-laravel

pkg:composer/api-analytics/laravel

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.1.0 2026-03-10 11:34 UTC

This package is not auto-updated.

Last update: 2026-03-12 05:09:04 UTC


README

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

Getting Started

1. Generate an API key

Head to apianalytics.dev/generate to generate your unique API key with a single click.

2. Install the package

composer require api-analytics/laravel

3. Add your API key

Add to your .env file:

API_ANALYTICS_KEY=your-api-key-here

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

Your API will now log incoming requests. View your dashboard at apianalytics.dev/dashboard.

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

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

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

Access the analytics client directly:

use ApiAnalytics\Laravel\Facade as Analytics;

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

// Force flush
Analytics::flush();

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

For self-hosted instances:

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

More Information