Download the PHP package directorytree/metrics without Composer

On this page you can find all versions of the php package directorytree/metrics. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package metrics

Metrics

A simple and elegant way to record metrics in your Laravel application.

Tests Total Downloads Latest Version License

Contents · Installation · Usage · Testing


Metrics provides a simple, elegant way to record and query metrics in your Laravel application.

Track page views, API calls, user signups, or any other countable events.

Contents

Requirements

Installation

You may install the package via composer:

Then, publish the migrations:

Finally, run the migrations:

Setup

Optionally, you may publish the configuration file:

This will create a config/metrics.php file where you may configure the driver and queueing behavior.

Using the Redis Driver

For distributed applications or high-traffic scenarios, you may use the redis driver to store captured metrics in Redis before committing them to the database in batches.

First, set the driver to redis in your configuration:

Or via environment variable:

Then, you may schedule the metrics:commit command to periodically commit metrics from Redis to your database:

[!important] Remember to disable auto_commit in the configuration file if you plan on committing metrics using the commit command.

You may also run the command manually:

This approach provides reduced database load since metrics can be committed in bulk at an expected interval instead of at the end of the request life-cycle.

The Redis driver uses a hash to store pending metrics with a configurable TTL (default of 1 day). This ensures metrics are eventually committed even if the scheduled command fails temporarily.

Usage

Recording Metrics

Record a metric using the Metrics facade:

Or using the metric global helper:

Or using the PendingMetric class:

Which ever method you use, metrics are recorded in the same way. Use whichever you prefer.

For the rest of the documentation, we will use the metric helper for consistency and brevity.

Recording with Values

By default, metrics have a value of 1. You may specify a custom value in the record method:

If you record the same metric multiple times, the values will be summed:

Recording with Categories

Organize metrics into categories:

These will be stored as separate metrics, allowing you to track the same metric across different contexts.

Recording with Dates

By default, metrics are recorded with today's date. You may specify a custom date using the date method:

Recording Hourly Metrics

By default, metrics are recorded at the daily level. For metrics that require hour-level granularity, you may use the hourly() method:

Hourly metrics include the hour (0-23) in addition to the year, month, and day, allowing you to track metrics at a more granular level:

Hourly metrics are stored separately from daily metrics, even for the same metric name:

You can query hourly metrics using the thisHour(), lastHour(), and onDateTime() methods:

[!tip] Use hourly metrics sparingly, as they create 24x more database rows than daily metrics. Reserve hourly tracking for metrics that genuinely benefit from hour-level granularity, or if you need the ability to query recorded metrics within a timezone.

Recording for Models

Associate metrics with Eloquent models using the HasMetrics trait:

Then record metrics for a specific model:

Query metrics for a model:

Recording with Custom Attributes

Store additional context with your metrics by adding custom attributes. This is useful for segmenting metrics by various dimensions like source, country, device type, or any other custom data.

First, create a migration to add custom columns to the metrics table:

Then, use the with() method to record metrics with custom attributes:

Custom attributes are included in the metric's uniqueness check, meaning metrics with different attribute values are stored separately:

This allows you to segment and analyze metrics by any dimension:

You can also use custom attributes with the MetricData class:

Or with the PendingMetric class:

[!important] Core metric attributes (name, category, year, month, day, measurable_type, measurable_id, value) cannot be overridden via custom attributes. They are protected and will always use the values set through their respective methods.

Capturing & Committing

For high-performance scenarios, you may capture metrics in memory and commit them in batches:

Captured metrics are automatically committed when the application terminates. You may also stop capturing manually:

To enable capturing for your entire application, start capturing in your AppServiceProvider:

This will batch all metrics recorded during the request and commit them automatically when the application terminates, reducing database queries and improving performance.

Querying Metrics

The Metric model includes a powerful query builder with date filtering methods:

Chain with standard Eloquent methods:

Testing

Metrics includes a fake implementation for testing:

Access recorded metrics in tests:

Extending & Customizing

Custom Metric Models

By default, metrics are stored using the included DirectoryTree\Metrics\Metric model. You may use a custom model globally or per-metric.

Global Custom Model

To use a custom metric model for all metrics, you may create your own model instance with the below requirements:

  1. Be fully unguarded (so the model can fill'ed appropriately)
  2. Include the same columns as the default metrics table
  3. Include the same casts as the default Metric model

Once you have created your custom model, you may set it as the default using the useModel() method on the DatabaseMetricManager.

This is typically done in your AppServiceProvider:

Per-Metric Custom Model

To use different metric models for different metrics, use the model() method on PendingMetric.

Keep in mind you will have to duplicate the metrics table and its columns for each custom model:

Custom Metric Manager

Create your own metric manager by implementing the MetricManager interface:

Then bind it in your AppServiceProvider:

Custom Metric Repository

Create a custom repository for storing captured metrics:

Then bind it in your AppServiceProvider:


All versions of metrics with dependencies

PHP Build Version
Package Version
Requires illuminate/redis Version ^9.0|^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^9.0|^10.0|^11.0|^12.0|^13.0
illuminate/database Version ^9.0|^10.0|^11.0|^12.0|^13.0
illuminate/collections Version ^9.0|^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package directorytree/metrics contains the following files

Loading the files please wait ...