njoguamos/laravel-plausible
Composer 安装命令:
composer require njoguamos/laravel-plausible
包简介
A laravel package for interacting with plausible analytics api.
README 文档
README
Plausible is intuitive, lightweight and open source web analytics. Plausible has no cookies and fully compliant with GDPR, CCPA and PECR.
Installation
| Version | Supported Laravel |
|---|---|
| 1.x | 10.x, 11.x |
| 2.x | 11.x, 12.x |
| 3.x | 12.x, 13.x |
You can install the package via composer:
composer require njoguamos/laravel-plausible
You can initialise the package with:
php artisan plausible:install
Install command will publish the config file.
Ensure that you have updated your application .env with credentials from Plausible i.e.
#.env file PLAUSIBLE_SITE_ID= PLAUSIBLE_API_KEY= #PLAUSIBLE_BASE_URL= <-- (Optional) for self-hosted
Note If you are using a self-hosted version of plausible, ensure that you define
PLAUSIBLE_BASE_URLto point to your custom domain.
Usage
1. Getting Realtime Visitors
To get the current visitors on your default site, run a request as follows.
use NjoguAmos\Plausible\Facades\Plausible; $visitors = Plausible::realtime();
The response in a single digit number
12
If you prefer using facades, then you can do as follows.
use NjoguAmos\Plausible\Facades\Plausible; $all = Plausible::aggregates();
2. Getting Aggregates
To get the aggregates, run a request as follows.
use NjoguAmos\Plausible\Facades\Plausible; // Simple with default $aggregates = Plausible::aggregates(); // Or with optional custom parameters $aggregates = Plausible::aggregates( period: 'custom', metrics: ['visitors', 'visits', 'pageviews'], filters: ['event:page==/blog**'], date: '2023-01-01,2023-01-31', withImported: true );
[Note] Filtering by imported data is limited. The general rule is that you cannot filter by two different properties at the same time. Learn more from Plausible Imported stats documentation
A successful response will be a json. Example;
{
"bounce_rate": {
"change": 4,
"value": 71
},
"events": {
"change": -24,
"value": 1166
},
"pageviews": {
"change": -24,
"value": 1166
},
"views_per_visit": {
"change": -26,
"value": 3
},
"visit_duration": {
"change": -37,
"value": 132
},
"visitors": {
"change": 1,
"value": 360
},
"visits": {
"change": 3,
"value": 389
}
}
Aggregates parameters explained expand to view more details.
Period - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::aggregates(period: '7d')
The period MUST be either of the allowed ones i.e 12mo,6mo,month,0d,7d,day, or custom. If not provided, period will default to 30d;
Metrics - array, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::aggregates(metrics: ['visitors', 'visits'])
The metrics must contain either of the allowed ones i.e visitors,visits,pageviews,views_per_visit,bounce_rate,visit_duration, or events. If not provided, all metrics will be included.
Compare - boolean, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::aggregates(compare: false )
compare defaults to true, meaning that the percent difference with the previous period for each metric will be calculated.
Filters - array, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::aggregates(filters: ['event:page==/blog**', 'visit:country==KE|DE'])
Your filters must be properly formed as per plausible instructions. Filters defaults to null.
Date - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::aggregates(period: 'custom', date: '2023-01-01,2023-01-31')
Date in Y-m-d format. Individual date e.g 2023-01-04 or a range 2023-01-01,2023-01-31. When not provided, date defaults to current date.
Info You must include
period: 'custom'when you provide a date range.
3. Getting Time Series
To get the timeseries data over a certain time period, run a request as follows.
use NjoguAmos\Plausible\Facades\Plausible; // Simple with default $aggregates = Plausible::timeSeries(); // Or with optional custom parameters $aggregates = Plausible::timeSeries( period: 'custom', metrics: ['visitors', 'visits', 'pageviews', 'views_per_visit', 'bounce_rate', 'visit_duration'], filters: ['event:page==/blog**'], interval: 'month', date: '2023-01-01,2023-01-31' );
A successful response will be a json. Example;
[
{
"bounce_rate": 17,
"date": "2023-01-01",
"pageviews": 60,
"views_per_visit": 19,
"visit_duration": 525,
"visitors": 6,
"visits": 6
},
{
"bounce_rate": 12,
"date": "2023-01-02",
"pageviews": 22,
"views_per_visit": 4,
"visit_duration": 149,
"visitors": 6,
"visits": 8
},
{
"bounce_rate": 57,
"date": "2023-01-03",
"pageviews": 9,
"views_per_visit": 2.57,
"visit_duration": 48,
"visitors": 7,
"visits": 7
},
{
"bounce_rate": 71,
"date": "2023-01-04",
"pageviews": 48,
"views_per_visit": 8.43,
"visit_duration": 301,
"visitors": 7,
"visits": 7
}
]
Timeseries parameters explained expand to view more details.
Period - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::timeSeries(period: '6mo')
The period MUST be either of the allowed ones i.e 12mo,6mo,month,0d,7d,day, or custom. If not provided, period will default to 30d;
Metrics - array, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::timeSeries(metrics: ['visits', 'pageviews', 'views_per_visit'])
The metrics must contain either of the allowed ones i.e visitors,visits,pageviews,views_per_visit,bounce_rate,visit_duration, or events. If not provided, all metrics will be included.
Filters - array, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::timeSeries(filters: ['event:page==/blog**', 'visit:browser==Firefox'])
Your filters must be properly formed as per plausible instructions. Filters defaults to null.
Interval - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::timeSeries(interval: 'month')
Interval can only be either month or date. When not provided, it defaults to date.
Date - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::timeSeries(period: 'custom', date: '2023-01-01,2023-01-31')
Date in Y-m-d format. Individual date e.g 2023-01-04 or a range 2023-01-01,2023-01-31. When not provided, date defaults to current date.
Info You must include
period: 'custom'when you provide a date range.
4. Getting Breakdowns
To get a breakdown of your stats by some property, run a request as follows.
use \NjoguAmos\Plausible\Facades\Plausible; // Simple with defaults $visitors = Plausible::breakdown(); // With optional parameters $aggregates = Plausible::breakdown( property: 'event:page', period: '12mo', metrics: ['visitors', 'visits', 'pageviews'], filters: 'event:page==/blog**', limit: 500 );
The response in a single digit number
[
{
"bounce_rate": 71,
"page": "/",
"pageviews": 146,
"visit_duration": 126,
"visitors": 87,
"visits": 77
},
{
"bounce_rate": 54,
"page": "/articles",
"pageviews": 179,
"visit_duration": 206,
"visitors": 71,
"visits": 50
},
{
"bounce_rate": 81,
"page": "/blog/about-laravel-plausible",
"pageviews": 42,
"visit_duration": 27,
"visitors": 35,
"visits": 37
},
{
"bounce_rate": 52,
"page": "/pricing",
"pageviews": 72,
"visit_duration": 147,
"visitors": 31,
"visits": 27
},
{
"bounce_rate": 76,
"page": "/aquatadas",
"pageviews": 22,
"visit_duration": 82,
"visitors": 21,
"visits": 21
}
]
Breakdown parameters explained expand to view more details.
Property - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::breakdown(property: '6mo')
The property MUST be either of the allowed ones i.e. visitors, visits, pageviews, bounce_rate, or visit_duration. If not provided, period will default to all allowed;
Period - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::breakdown(period: '6mo')
The period MUST be either of the allowed ones i.e 12mo,6mo,month,0d,7d,day, or custom. If not provided, period will default to 30d;
Date - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::breakdown(date: '2023-01-01')
Date in Y-m-d format.
Info
period: 'custom'is not supported. `date: '2023-01-01,2023-02-02' is not supported.
Metrics - array, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::breakdown(metrics: ['visits', 'pageviews', 'views_per_visit'])
The metrics must contain either of the allowed ones i.e visitors,visits,pageviews,views_per_visit,bounce_rate,visit_duration, or events. If not provided, all metrics will be included.
Limit - int, optional
use NjoguAmos\Plausible\Facades\Plausible;; $aggregates = Plausible::breakdown(limit: 200)
The results limit. It must be between 1 and 1000. When not provided, limit defaults to 100.
Page - int, optional
use NjoguAmos\Plausible\Facades\Plausible;; $aggregates = Plausible::breakdown(page: 2)
Page for the results. When not provided, page defaults to 1.
Filters - string, optional
use NjoguAmos\Plausible\Facades\Plausible; $aggregates = Plausible::breakdown(filters: 'event:page==/blog**')
Your filters must be properly formed as per plausible instructions. Filters defaults to null.
Info Multiple filters are not supported.
5. Caching Response
To increased performance of your application and reduce reliance to plausible api, all requests are cached for 3 minutes. You can specify cache duration (in seconds) and driver using the following env variables.
PLAUSIBLE_CACHE_DURATION=300 PLAUSIBLE_CACHE_DRIVER=redis
If for some reason you don't want to cache response, you can turn off caching entirely by adding the following env variable.
PLAUSIBLE_CACHE=false
Testing
Info To test this package, run the following command.
composer test
Changelog
Please see releases for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
njoguamos/laravel-plausible 适用场景与选型建议
njoguamos/laravel-plausible 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.52k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2023 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「analytics」 「laravel」 「plausible」 「njoguamos」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 njoguamos/laravel-plausible 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 njoguamos/laravel-plausible 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 njoguamos/laravel-plausible 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The Plausible Analytics Bundle lets you tracking your app with a plausible instance.
Start tracking your visitors with Plausible.io
Track visits of your Neos site with plausible.io
Plausible is a Craft CMS plugin that gives you handy dashboard widgets to display analytics data from Plausible.
Plausible integration for Kirby CMS
Analytics chooser extensions for site settings.
统计信息
- 总下载量: 9.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-04