PHP code example of justawebdev / laravel-simple-analytics

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

    

justawebdev / laravel-simple-analytics example snippets


return [
    'enabled' => true,  // Enable/disable analytics globally

    'ignore_routes' => [
        'telescope/*',  // Ignore Laravel Telescope routes
        'horizon/*',    // Ignore Laravel Horizon routes
        // Add any routes you want to ignore
    ],
];

// In routes/web.php or api.php
Route::middleware(['analytics'])->group(function () {
    // Your routes here
});

// Or globally in Kernel.php
protected $middlewareGroups = [
    'web' => [
        // ... other middleware
        \JustAWebDev\Analytics\Middleware\TrackPageView::class,
    ],
];

use JustAWebDev\Analytics\Facades\Analytics;

// Track a simple event
Analytics::track('user_login');

// Track with metadata
Analytics::track('purchase', [
    'product_id' => 123,
    'amount' => 99.99,
    'currency' => 'USD'
]);

// Global helper function
analytics()->track('button_click', ['button' => 'hero_cta']);

// Count total page views
$totalViews = analytics()->count('pageview');

// Count views for a specific page
$pageViews = analytics()->count('pageview', '/home');

// Get top 5 pages
$topPages = analytics()->topPages(5);

// Returns collection with 'name' and 'total' columns
foreach ($topPages as $page) {
    echo "{$page->name}: {$page->total} views\n";
}



namespace App\Http\Middleware;

use Closure;
use JustAWebDev\Analytics\Facades\Analytics;

class CustomAnalytics
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Only track GET requests
        if ($request->isMethod('get')) {
            Analytics::pageView([
                'name' => $request->path(),
                'method' => $request->method(),
                'status' => $response->getStatusCode(),
                'user_agent' => $request->userAgent(),
            ]);
        }

        return $response;
    }
}
bash
php artisan vendor:publish --provider="JustAWebDev\Analytics\AnalyticsServiceProvider"
bash
php artisan migrate
bash
php artisan analytics:report