1. Go to this page and download the library: Download api-analytics/laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
api-analytics / laravel example snippets
// 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,
],
];
// 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') ?? '';
},
// 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;
});
}
use ApiAnalytics\Laravel\Facade as Analytics;
// Get buffer size
$count = Analytics::getBufferSize();
// Force flush
Analytics::flush();
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)
);
});