directorytree/metrics
Record and query metrics in Laravel with a simple, elegant API. Track page views, API calls, signups, and other events with optional values, categories, dates, hourly buckets, model-scoped metrics, and custom attributes. Supports Redis and extensible drivers.
source, country). This enables fine-grained analytics without requiring external tools like Mixpanel or Google Analytics.HasMetrics trait integrates seamlessly with Eloquent, enabling per-model tracking (e.g., user-specific metrics). This is particularly useful for SaaS platforms or applications with user-centric analytics.metrics:commit command), further decoupling metric recording from database writes.metrics table requires manual migration and may not scale for arbitrary attributes. Mitigation: Use a JSON column or a separate pivot table for dynamic attributes.metrics:commit) to flush metrics to the database. If the job fails or Redis is unavailable, metrics may be lost. Mitigation: Monitor the job and implement retries/fallbacks.date(Carbon::parse('2025-01-15'))) requires careful handling to avoid duplicates or conflicts. Mitigation: Implement idempotent recording logic or use transactions.Metric::fake()) may require mocking the underlying storage layer. The package lacks a robust fake implementation out of the box.measurable() and custom attributes.metrics:commit) for Redis-based batching? If not, in-memory capturing may be preferable.sum('value') matches expected totals.name, category, measurable_type/id, and custom attributes for query performance.api:requests, signups).metrics:commit hourly or via queue workers.source, user_id).Metric model.spatie/laravel-analytics).Metric model to add soft deletes or additional scopes.composer require directorytree/metrics.php artisan vendor:publish --tag="metrics-migrations" --tag="metrics-config".php artisan migrate.database or redis) in config/metrics.php.// app/Http/Middleware/RecordApiRequests.php
public function handle(Request $request, Closure $next) {
metric('api:requests')->record();
return $next($request);
}
metric('test')->record() increments the count).Metric::today()->sum('value')).metrics:commit jobs.directorytree/metrics package. Since it’s MIT-licensed and actively maintained (last release: 2026-04-17), updates are likely to be backward-compatible.config/metrics.php, making it easy to adjust drivers, TTLs, or queue settings.auto_commit is enabled (disables Redis batching) or if the metrics:commit job is failing.metrics table for name, category, measurable_type, measurable_id, and custom attributes.metrics:commit job runs reliably.How can I help you explore Laravel packages today?