PHP code example of voidgraphics / laravel-pageview-counter

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

    

voidgraphics / laravel-pageview-counter example snippets


// bootstrap/app.php



use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use PageviewCounter\Middleware\LogRequest;

return Application::configure(basePath: dirname(__DIR__))
    // ...
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->append(LogRequest::class);
    })
    // ...
    ->create();

Pageview::daily()->get(); // Returns the number of views grouped by date and path.

/*
Illuminate\Database\Eloquent\Collection {
    all: [
        PageviewCounter\Models\Pageview {
            views: 156,
            path: "/",
            date: "2024-07-18",
        },
        PageviewCounter\Models\Pageview {
            views: 68,
            path: "about",
            date: "2024-07-18",
        }
        PageviewCounter\Models\Pageview {
            views: 289,
            path: "/",
            date: "2024-07-17",
        },
        PageviewCounter\Models\Pageview {
            views: 32,
            path: "about",
            date: "2024-07-17",
        }
    ],
}
*/

Pageview::byPage()->get(); // Get pageviews, grouped by page

Illuminate\Database\Eloquent\Collection {#3604
    all: [
        App\Models\Pageview {#3603
            views: 230,
            path: "/",
            unique_visitors: 178,
            latest_visit: "2024-07-23 12:59:19",
        },
        App\Models\Pageview {#3601
            views: 132,
            path: "about",
            unique_visitors: 129,
            latest_visit: "2024-07-20 14:27:38",
        },
    ],
}

Pageview::withoutBots()->get(); // Exclude most popular bots by user-agent

/*
Shorthand for: 

$query->where('useragent', 'not like', '%bot%')
    ->where('useragent', 'not like', '%python-requests%')
    ->where('useragent', 'not like', '%http%')
    ->where('useragent', 'not like', '%node-fetch%')
    ->where('useragent', 'not like', '%postman%')
    ->where('useragent', 'not like', '%curl%')
*/

Pageview::uniqueVisitors()->get(); // Returns the count of unique visitors, grouped by date

/*
Illuminate\Database\Eloquent\Collection {
    all: [
        PageviewCounter\Models\Pageview {
            unique_visitors: 176,
            date: "2024-07-18",
        },
        PageviewCounter\Models\Pageview {
            unique_visitors: 302,
            date: "2024-07-17",
        }
    ],
}
*/
bash
php artisan vendor:publish --tag=pageview-counter-migrations
bash
php artisan migrate