PHP code example of ibrahim-kaya / visit-tracker

1. Go to this page and download the library: Download ibrahim-kaya/visit-tracker 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/ */

    

ibrahim-kaya / visit-tracker example snippets


return [
    'excluded_paths' => [
        'admin/*',
        'telescope/*',
    ],

    'excluded_methods' => [
        'POST',
        'PATCH',
    ],

    'log_bots' => false,

    'ip_info_cache_duration' => 86400, // seconds
    
    'use_queue' => true, // Use Laravel queues for processing

    // Optional: only needed if Horizon/worker listens on a different connection
    // than QUEUE_CONNECTION (e.g. Horizon=redis, QUEUE_CONNECTION=database)
    'queue_connection' => env('VISIT_TRACKER_QUEUE_CONNECTION'),
    'queue_name' => env('VISIT_TRACKER_QUEUE'),

    'log_payload' => false, // Set to true to log request payload/body data

    'excluded_payload_fields' => [
        'password',
        'password_confirmation',
        'token',
        '_token',
    ],

    // Assign anonymous visits to the user after login/register
    'attribute_on_auth' => true,
    'visitor_cookie' => 'visit_tracker_vid',
    'visitor_cookie_minutes' => 60 * 24 * 365 * 2, // 2 years
];

use IbrahimKaya\VisitTracker\Models\PageVisitLog;

PageVisitLog::attributeToUser(
    auth()->id(),
    request()->cookie(config('visit-tracker.visitor_cookie')),
    session()->getId()
);

use IbrahimKaya\VisitTracker\Models\PageVisitLog;

$recentVisits = PageVisitLog::latest()->take(5)->get();

foreach ($recentVisits as $visit) {
    echo $visit->ip_address;
    echo $visit->browser;
    echo $visit->device_type;
    echo $visit->method; // HTTP method (GET, POST, etc.)
    echo $visit->payload; // Request payload (array, null for GET requests)
}

protected $middleware = [
    \IbrahimKaya\VisitTracker\Middleware\VisitTracker::class,
];

use IbrahimKaya\VisitTracker\Models\PageVisitLog;

// Get total visits (including bots)
$total = PageVisitLog::totalVisits();

// Get total visits excluding bots
$total = PageVisitLog::totalVisits(true);

// Counts unique visitors using user_id (if logged in) or session_id
// This ensures logged-in users are counted correctly even if their session_id changes
$unique = PageVisitLog::uniqueVisitors();
$unique = PageVisitLog::uniqueVisitors(true); // Exclude bots

$uniqueIps = PageVisitLog::uniqueIpAddresses();
$uniqueIps = PageVisitLog::uniqueIpAddresses(true); // Exclude bots

// Get top 10 most visited pages
$topPages = PageVisitLog::mostVisitedPages(10);

// Get top 5 most visited pages excluding bots
$topPages = PageVisitLog::mostVisitedPages(5, true);

// Access results
foreach ($topPages as $page) {
    echo $page->page_url . ': ' . $page->visit_count . ' visits';
}

// Get visits for a specific date range
$visits = PageVisitLog::visitsByDateRange('2024-01-01', '2024-01-31');

// Get visits from a specific date to today
$visits = PageVisitLog::visitsByDateRange('2024-01-01');

// Get visits up to a specific date
$visits = PageVisitLog::visitsByDateRange(null, '2024-01-31');

// Exclude bots
$visits = PageVisitLog::visitsByDateRange('2024-01-01', '2024-01-31', true);

$deviceStats = PageVisitLog::statisticsByDeviceType();
$deviceStats = PageVisitLog::statisticsByDeviceType(true); // Exclude bots

// Access results
foreach ($deviceStats as $stat) {
    echo $stat->device_type . ': ' . $stat->count . ' visits';
}

$browserStats = PageVisitLog::statisticsByBrowser();
$browserStats = PageVisitLog::statisticsByBrowser(true); // Exclude bots

foreach ($browserStats as $stat) {
    echo $stat->browser . ': ' . $stat->count . ' visits';
}

$platformStats = PageVisitLog::statisticsByPlatform();
$platformStats = PageVisitLog::statisticsByPlatform(true); // Exclude bots

foreach ($platformStats as $stat) {
    echo $stat->platform . ': ' . $stat->count . ' visits';
}

// Get top 10 referrers
$referrers = PageVisitLog::statisticsByReferrer(10);
$referrers = PageVisitLog::statisticsByReferrer(10, true); // Exclude bots

foreach ($referrers as $referrer) {
    echo $referrer->referrer . ': ' . $referrer->count . ' visits';
}

// Get daily statistics for the last 30 days
$daily = PageVisitLog::dailyStatistics(30);
$daily = PageVisitLog::dailyStatistics(30, true); // Exclude bots

foreach ($daily as $day) {
    echo $day->date . ': ' . $day->count . ' visits';
}

// Requires detailed IP info to be enabled in config
$countryStats = PageVisitLog::statisticsByCountry();
$countryStats = PageVisitLog::statisticsByCountry(true); // Exclude bots

foreach ($countryStats as $stat) {
    echo $stat['country'] . ': ' . $stat['count'] . ' visits';
}

// Get summary statistics for the last 30 days
$summary = PageVisitLog::summaryStatistics(30);
$summary = PageVisitLog::summaryStatistics(30, true); // Exclude bots

// Returns an array with:
// - total_visits
// - unique_visitors
// - unique_ips
// - top_pages (top 5)
// - by_device
// - by_browser
// - by_platform

echo $summary['total_visits'];
echo $summary['unique_visitors'];

// Use the scope to filter out bots
$visits = PageVisitLog::excludeBots()->get();

// Filter visits by date range
$visits = PageVisitLog::dateRange('2024-01-01', '2024-01-31')->get();

// Combine scopes
$visits = PageVisitLog::excludeBots()
    ->dateRange('2024-01-01', '2024-01-31')
    ->get();
bash
php artisan vendor:publish --provider="IbrahimKaya\VisitTracker\VisitTrackerServiceProvider" --tag=visit-tracker-config
bash
php artisan migrate
bash
php artisan queue:table
php artisan migrate
bash
php artisan queue:work
bash
php artisan queue:work --daemon