定制 voidgraphics/laravel-pageview-counter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

voidgraphics/laravel-pageview-counter

最新稳定版本:v0.1.0

Composer 安装命令:

composer require voidgraphics/laravel-pageview-counter

包简介

A stupid simple analytics tool to keep track of page views

README 文档

README

A stupid simple analytics tool to keep track of page views in Laravel projects. It logs requests to a database table and you can access the data with an Eloquent model.

Based on this article by Bastiaan Rudolf.

Installation

composer require voidgraphics/laravel-pageview-counter

You need to publish the migration that will add the pageviews table:

php artisan vendor:publish --tag=pageview-counter-migrations

Then run the migrations

php artisan migrate

After this, register the LogRequest middleware. You can do it globally or on specific routes. Here's how to install it globally in Laravel 11

// bootstrap/app.php

<?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();

Accessing the data

Every request hitting those routes will now be logged. You can access that data using the PageviewCounter\Models\Pageview model. It has 3 scopes to help you query the data.

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",
        }
    ],
}
*/

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2024-07-18