PHP code example of n2ns / laravel-stats

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

    

n2ns / laravel-stats example snippets


   use Illuminate\Support\Facades\Gate;

   Gate::define('viewStats', fn ($user) => $user->is_admin);
   

  use N2ns\Stats\Stats;

  Stats::page(title: $post->title, type: 'article', group: 'post-'.$post->id);
  

  Stats::event('checkout_start', label: 'pro', target: $checkoutUrl);
  

use N2ns\Stats\Http\Middleware\TrackAiFetches;
use N2ns\Stats\Http\Middleware\TrackPageviews;

->withMiddleware(function (Middleware $middleware) {
    $middleware->web(append: [TrackAiFetches::class, TrackPageviews::class]); // TrackPageviews only if wanted
})

// config/stats.php
'dashboard_layout' => ['view' => 'admin.layout', 'section' => 'content', 'title_sections' => ['title'],
    'sections' => ['page-title' => 'Analytics'],   // optional: fixed text for other sections of the layout
    'theme' => 'system'],                           // optional: follow prefers-color-scheme like the layout
// or: 'dashboard_layout' => ['component' => 'layouts.app'],   // page in its slot, title in its title attribute
'dashboard_back_url' => '/admin',   // a "Back" link at the top of the pages

use N2ns\Stats\Filament\StatsPlugin;

$panel->plugin(StatsPlugin::make()->navigationGroup('Analytics')->navigationLabel('Statistics'));
bash
php artisan stats:site list
php artisan stats:site update --site=<public id> --name="New name" --domain=example.com --timezone=UTC
php artisan stats:site delete --site=<public id>          # asks first; --force skips the question
php artisan stats:site key --site=<public id>             # server key for client mode, see below
bash
  php artisan stats:site key --site=<public id>
  
bash
curl -H "Authorization: Bearer $STATS_TOKEN" \
  "https://stats.example.com/api/v1/sites/01JC7…/summary?from=2026-11-01&to=2026-11-30"

curl -H "Authorization: Bearer $STATS_TOKEN" \
  "https://stats.example.com/api/v1/sites/01JC7…/breakdown/pages?from=2026-11-01&to=2026-11-30&limit=10"