voidgraphics/laravel-pageview-counter
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", } ], } */
voidgraphics/laravel-pageview-counter 适用场景与选型建议
voidgraphics/laravel-pageview-counter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 voidgraphics/laravel-pageview-counter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voidgraphics/laravel-pageview-counter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2024-07-18