Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Metrics Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Record metrics in your Laravel application

Frequently asked questions about Metrics
How do I install DirectoryTree/Metrics in a Laravel 9+ project?
Run `composer require directorytree/metrics`, publish the migrations with `php artisan vendor:publish --tag=metrics-migrations`, and execute `php artisan migrate`. Optional: Publish the config file with `--tag=metrics-config` for customization.
What Laravel versions does DirectoryTree/Metrics support?
The package officially supports Laravel 9 and 10. For Laravel 10, verify compatibility with new Eloquent features, but the core functionality remains stable. Always check the latest release notes for updates.
Can I track hourly metrics without bloating my database?
Yes, hourly metrics are supported via the `hourly()` method, but use disciplined naming (e.g., `api:requests.hourly`) to avoid confusion. For high-volume apps, consider Redis to offload writes and reduce database load.
How do I record metrics for specific models (e.g., User, Order)?
Use the `HasMetrics` trait on your model and call `recordMetric()` or `incrementMetric()` on instances. Example: `$user->recordMetric('profile_views')`. Model-scoped metrics are stored with the model’s ID for segmentation.
Does DirectoryTree/Metrics support custom attributes for segmentation?
Yes, you can add custom attributes like `user_id` or `plan_type` when recording metrics. These require manual migration updates (e.g., `metrics_attributes` table) and are queried via `whereAttribute()` in Eloquent queries.
When should I use the Redis driver instead of the default database driver?
Use Redis for high-throughput apps (e.g., 10K+ RPS) where database writes are a bottleneck. It improves performance but introduces eventual consistency (5-minute delay) and requires monitoring for TTL expiry or commit failures.
How do I query metrics for a specific date range or category?
Use Eloquent’s query builder: `Metrics::where('name', 'api_calls')->betweenDates('2023-01-01', '2023-01-31')` or `Metrics::whereCategory('auth')->thisMonth()`. Hourly metrics can be filtered with `whereHourly(true)`.
Are there any risks of race conditions when recording metrics?
Yes, high-traffic apps may experience race conditions during commits. Mitigate this by disabling `auto_commit` and manually committing via queues (e.g., `Metrics::commit()` in a job) or using Redis with proper locking mechanisms.
How do I fake metrics in PHPUnit tests?
Use the `Metrics::fake()` helper to stub metric recordings. Example: `Metrics::fake()->shouldRecord('page_views', 5)`. This prevents actual database writes during tests and verifies expected calls.
What alternatives exist for Laravel metrics if DirectoryTree/Metrics doesn’t fit?
Consider `spatie/laravel-analytics` for Google Analytics integration, `laravel-activitylog` for event logging, or `statsd` for high-scale time-series metrics. For custom solutions, Laravel’s Eloquent + queues can manually track metrics with less abstraction.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai