PHP code example of ohseesoftware / laravel-server-analytics

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

    

ohseesoftware / laravel-server-analytics example snippets


// Kernel.php

protected $middleware = [
  // ... other middlware here
  \OhSeeSoftware\LaravelServerAnalytics\Http\Middleware\LogRequest::class,
];

$userId = $request->user()?->id ?? null

// AppServiceProvider

use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;

public function boot()
{
    // Do not track `/home` or `/admin/*` routes
    ServerAnalytics::addRouteExclusions([
      '/home',
      '/admin/*'
    ]);
}

// AppServiceProvider

use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics;

public function boot()
{
    // Do not track `POST` or `PUT` requests
    ServerAnalytics::addMethodExclusions(['POST', 'PUT']);
}

// config/laravel-server-analytics.php

return [
    'ignore_bot_requests' => true
];

ServerAnalytics::addMetaHook(function (RequestDetails $requestDetails) {
    return [
        'key' => 'some-key',
        'value' => 'some-value'
    ];
});

ServerAnalytics::addRelationHook(function (RequestDetails $requestDetails) use ($post) {
    return [
        'model' => $post,
        'reason' => 'Post that was deleted.'
    ];
});

ServerAnalytics::addMeta('test', 'value');

ServerAnalytics::addRelation($post);


// CustomRequestDetails.php

use OhSeeSoftware\LaravelServerAnalytics\RequestDetails;

class CustomRequestDetails extends RequestDetails
{
    public function getMethod(): string
    {
        // Set the stored `method` as "TEST" for all requests
        return 'TEST';
    }
}


use OhSeeSoftware\LaravelServerAnalytics\Repositories\AnalyticsRepository;

public function loadAnalytics(AnalyticsRepository $analytics)
{
    $records = $analytics->query()->where('method', 'GET')->get();
}

use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;

$analytics = Analytics::relatedTo($user);

use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;

$analytics = Analytics::hasMeta('foo');

use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics;

$analytics = Analytics::withMetaValue('foo', 'bar');
bash
php artisan vendor:publish --provider="OhSeeSoftware\LaravelServerAnalytics\LaravelServerAnalyticsServiceProvider"
bash
config/laravel-server-analytics.php
bash
php artisan migrate