定制 api-analytics/laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

api-analytics/laravel

最新稳定版本:v0.1.0

Composer 安装命令:

composer require api-analytics/laravel

包简介

API Analytics middleware for Laravel applications.

README 文档

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固