ohseesoftware/laravel-server-analytics
Composer 安装命令:
composer require ohseesoftware/laravel-server-analytics
包简介
Server side analytics for your Laravel application or website.
README 文档
README
Server side analytics for your Laravel application or website. No cookies, no tracking :)
Vision
There are a lot of choices out there when it comes to analytics for your web project. With the progress of GDPR, a focus on privacy has emerged. Consumers are now learning that they can be tracked across the web, and are not appreciative of this.
As you develop your next website or project, you may wish to add analytics so you can see how many pageviews, etc you're getting. You may not care to track how a specific visitor got to your website, or the actions they took once they were there. You just want some basic metrics on the activity on your web project.
You've come to the right place.
Goals
- Handled entirely by the backend: this means no client-side JS to add to your site, which means no impact on performance for your users
- Write operations should not impact request performance
- Customizable: we'll track the main aspects of a request (path, status, params, duration, user, etc), but also allow you to attach extra data to a request (such as which entity in your database was accessed or metadata)
- Ships with a default dashboard to view your data: we'll give you a default dashboard (and seperate route) to view your data
- Accessible API: in addition to a default dashboard, we'll expose an API you can use to pull custom metrics out of your data
Installation
You can install the package via composer:
composer require ohseesoftware/laravel-server-analytics
Publish the package (config, assets, etc):
php artisan vendor:publish --provider="OhSeeSoftware\LaravelServerAnalytics\LaravelServerAnalyticsServiceProvider"
Review the configuration file and make changes as necessary:
config/laravel-server-analytics.php
Run database migrations:
php artisan migrate
Usage
Basic Usage
For basic request tracking, there's no custom code you need to add to your application other than including the middleware:
// Kernel.php protected $middleware = [ // ... other middlware here \OhSeeSoftware\LaravelServerAnalytics\Http\Middleware\LogRequest::class, ];
If you want to only track a specific middleware group, add it to that group instead of the global $middleware variable.
Tracking authenticated user
v3.0.0+
By default, the package will automatically track the authenticated user who made the request. This is stored directly in the analytics table in the user_id column.
The default migration assumes you users are stored in a users table with a BIGINT field type for the primary key. If your users are not stored like that, you should write your own migration. The package also assumes your User model is located at App\User. If yours is located in a difference namespace, you can update the user_model key in the published config file.
When logging a request, the package will use this code to insert the user_id into the analytics table:
$userId = $request->user()?->id ?? null
Excluding Routes
If you'd like to exclude specific routes (or wildcards) from being tracked, you can do so via the addRouteExclusions() method:
// AppServiceProvider use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics; public function boot() { // Do not track `/home` or `/admin/*` routes ServerAnalytics::addRouteExclusions([ '/home', '/admin/*' ]); }
Excluding Request Methods
If you'd like to exclude specific request methods from being tracked, you can do so via the addMethodExclusions() method:
// AppServiceProvider use OhSeeSoftware\LaravelServerAnalytics\Facades\ServerAnalytics; public function boot() { // Do not track `POST` or `PUT` requests ServerAnalytics::addMethodExclusions(['POST', 'PUT']); }
Excluding Requests From Bots
If you'd like to exclude requests from bots/crawlers, you can set the ignore_bot_requests configuration value to true.
We use the Crawl Detect package to detect bots/crawlers.
// config/laravel-server-analytics.php return [ 'ignore_bot_requests' => true ];
Request hooks
We provide took optional hooks that you can use to automatically associate extra data with your analytics records: addMetaHook and addRelationHook. Both hooks return an array of data that should be associated to the analytics record. You can add as many of each as you'd like throughout your request's lifecycle.
addMetaHook example:
ServerAnalytics::addMetaHook(function (RequestDetails $requestDetails) { return [ 'key' => 'some-key', 'value' => 'some-value' ]; });
addRelationHook example:
ServerAnalytics::addRelationHook(function (RequestDetails $requestDetails) use ($post) { return [ 'model' => $post, 'reason' => 'Post that was deleted.' ]; });
There's also helper methods available which call the above hooks for you:
ServerAnalytics::addMeta('test', 'value'); ServerAnalytics::addRelation($post);
Providing Custom Request Details
We provide sensible defaults for pulling details for the request. However, if you need to pull the details in a different manner, you can provide a custom implementation of the RequestDetails class:
// CustomRequestDetails.php use OhSeeSoftware\LaravelServerAnalytics\RequestDetails; class CustomRequestDetails extends RequestDetails { public function getMethod(): string { // Set the stored `method` as "TEST" for all requests return 'TEST'; } }
There's a config value, request_details_class, which you can set to the FQN of your request details class. We will attempt to resolve that class out of the container when logging requests.
Querying Analytics Records
You can use the AnalyticsRepository to query data out of the analytics tables. If you need to build a custom query, you can use the query() method on the repository instance.
use OhSeeSoftware\LaravelServerAnalytics\Repositories\AnalyticsRepository; public function loadAnalytics(AnalyticsRepository $analytics) { $records = $analytics->query()->where('method', 'GET')->get(); }
There's also a couple query scopes setup on the Analytics model.
Filter Analytics records that are related to the a given model:
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics; $analytics = Analytics::relatedTo($user);
Filter Analytics records that have metadata with a given key:
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics; $analytics = Analytics::hasMeta('foo');
Filter Analytics records that have a given metadata key/value pair:
use OhSeeSoftware\LaravelServerAnalytics\Models\Analytics; $analytics = Analytics::withMetaValue('foo', 'bar');
Testing
./vendor/bin/phpunit
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@ohseesoftware.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
ohseesoftware/laravel-server-analytics 适用场景与选型建议
ohseesoftware/laravel-server-analytics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.32k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2019 年 12 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「analytics」 「laravel」 「laravel-server-analytics」 「ohseesoftware」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ohseesoftware/laravel-server-analytics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ohseesoftware/laravel-server-analytics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ohseesoftware/laravel-server-analytics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Analytics chooser extensions for site settings.
A Laravel Nova Card to show Fathom Analytics stats.
Alfabank REST API integration
Laravel 5 package to retrieve Google Analytics data.
Scout Extended extends Laravel Scout adding algolia-specific features
PHP library for Bluelabs Erwin
统计信息
- 总下载量: 22.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-05