PHP code example of daltcore / laravel-metrics

1. Go to this page and download the library: Download daltcore/laravel-metrics 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/ */

    

daltcore / laravel-metrics example snippets


GurmanAlexander\Metrics\MetricsServiceProvider::class,

class FirstPaymentMetrics extends CountMetrics
{

}

use GurmanAlexander\Metrics\Metricable;
class User extends Model
{
    use Metricable;
    ...
}

// For example, when creating user start Metrics
$user = User::create(['email', 'password']);
$user->startMetrics(new FirstPaymentMetrics($user));

// when user view some news
$user = auth()->user();
$news->startMetrics(new PageViewCountMetrics($user));

// when user make some payment
$user->paySomething();
$user->closeMetrics(new FirstPaymentMetrics($user));

// when user logout
$user = auth()->user();
$news->closeMetrics(new PageViewCountMetrics($user));
auth()->logout();

$user = auth()->user();
$user->onceMetrics(new SomeOnceMetrics());

// to get total payments
$stats = new MetricsStatistics(new FirstPaymentMetrics());

// model
$stats->user(auth()->user());

// array
$stats->user([auth()->user(), User:first()]);

// collection
$stats->user(User::where('is_active', 1)->get());

$start_at = Carbon::now()->startOfMonth();
$stats->startAt($start_at);

$end_at = Carbon::now()->endOfMonth();
$stats->endAt($end_at);

$start_at = Carbon::now()->startOfMonth();
$end_at = Carbon::now()->endOfMonth();
$stats->betweenAt($start_at, $end_at);

$stats->hourly();
$stats->daily();
$stats->weekly();
$stats->monthly();
$stats->yearly();

// result example
$stats->hourly()->count()->toArray();

// [
//     0 => [
//         "count" => 23,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 9
//     ],
//     1 => [
//         "count" => 15,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 8
//     ],
//     2 => [
//         "count" => 32,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 7
//     ],
// ]

// to get total payments
$stats = new MetricsStatistics(new FirstPaymentMetrics());

// to get average time from user registration to first payment (in seconds)
$stats = new MetricsStatistics(new FirstPaymentMetrics())->hourly()->avg()->toArray();

// [
//     0 => [
//         "avg" => 12.13,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 9
//     ],
//     1 => [
//         "avg" => 8.00,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 8
//     ],
//     2 => [
//         "avg" => 5.34,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 7
//     ],
// ]
shell
php artisan vendor:publish --provider="GurmanAlexander\Metrics\MetricsServiceProvider"
shell
php artisan metrics:table
shell
php artisan migrate
shell
php artisan make:metrics PageViewCountMetrics
shell
php artisan make:metrics FirstPaymentMetrics