- How do I install and set up 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`. Optionally publish the config file with `--tag=metrics-config` for customization.
- What Laravel versions and PHP versions does DirectoryTree/Metrics support?
- The package requires PHP 8.1 or higher and Laravel 9.0 or newer. It follows Laravel’s conventions and integrates with Eloquent, Facades, and Artisan commands seamlessly.
- Can I track hourly metrics or only daily aggregates with this package?
- Yes, DirectoryTree/Metrics supports both hourly and daily metrics. Hourly metrics are stored as separate rows, while daily metrics aggregate data. Use `recordHourly()` for granular time-based tracking.
- How do I associate metrics with Eloquent models (e.g., tracking user actions)?
- Use the `HasMetrics` trait on your Eloquent model. This enables model-scoped metrics, allowing you to track actions like `user->recordMetric('login')` directly from the model instance.
- Does DirectoryTree/Metrics support custom attributes for segmentation (e.g., tracking by country or device)?
- Yes, you can attach custom attributes like `source`, `country`, or `device` to metrics. These are stored as JSON in the database and can be queried dynamically using Laravel’s query builder.
- How do I enable Redis buffering for high-volume metric collection?
- Publish the config file (`php artisan vendor:publish --tag=metrics-config`) and set `driver` to `redis` in the configuration. Metrics will buffer in Redis until committed via `php artisan metrics:commit`.
- What are the performance implications of hourly metrics in large-scale applications?
- Hourly metrics create 24x more rows than daily metrics, which can impact storage and query performance. Optimize with database indexing (e.g., composite index on `name`, `category`, `date`) and monitor Redis TTL settings for buffering.
- Can I extend the metrics table schema or create custom metric models?
- Yes, you can customize the schema by publishing and modifying migrations. For custom models, extend the base `Metric` model and bind a custom repository via the service provider’s `extend` method.
- How do I test metrics recording and querying in PHPUnit?
- Use Laravel’s `fake()` helper to mock the `Metrics` facade or `HasMetrics` trait. Wrap tests in transactions to avoid persisting fake data, and verify queries with `expectsQueries()` or database assertions.
- What alternatives exist for Laravel metrics if I need real-time aggregations or dashboards?
- For real-time analytics, consider packages like `spatie/laravel-analytics` or `laravel-ignition` for debugging. For dashboards, integrate with third-party tools like Grafana or Mixpanel, or use Laravel Scout for search-based metrics.