bnomei/kirby3-pageviewcounter
Composer 安装命令:
composer require bnomei/kirby3-pageviewcounter
包简介
Track Page view count and last visited timestamp
README 文档
README
Track Page view count and last visited timestamp
Installation
- unzip master.zip as folder
site/plugins/kirby3-pageviewcounteror git submodule add https://github.com/bnomei/kirby3-pageviewcounter.git site/plugins/kirby3-pageviewcounterorcomposer require bnomei/kirby3-pageviewcounter
Usage
Echo the tracking a 1px x 1px-image or adding css anywhere in your template. Both techniques will increment the counter but technically they track in a different way.
A) via image, tracking scroll below first fold
<?php echo $page->counterImage(); ?>
B) via css, tracking mouse hover on body
<?php echo $page->counterCss(); ?>
How it works
The tracking image will be moved below the fold and trigger the counter with native lazy loading on first user scroll. Why? To avoid most of the bots. It will also work for cached pages using the pages cache (even when cached static).
SQLite database (default)
To view the tracked count and timestamp this plugin provides two optional fields (viewcount and lastvisited).
in your page blueprint
fields: counter: label: Page view count type: viewcount lastvisited: label: Page last visited type: lastvisited # format: 'DD-MM-YYYY'
Tip
Kirby has day.js built in which you can use to format your date output.
You do not have to add anything to you config files. But you could make some changes to the defaults, like the path to the sqlite file if you wanted to.
site/config/config.php
<?php return [ /* default 'bnomei.pageviewcounter.counter' => function () { return new \Bnomei\PageViewCounterSQLite(); }, 'bnomei.pageviewcounter.sqlite.file' => function () { $dir = realpath(kirby()->roots()->accounts() . '/../'); Dir::make($dir); return $dir . '/pageviewcounter.sqlite'; }, */ // other options ... ];
Page Fields (alternative)
If you do not want to store your tracked counts in a sqlite file then add kirby text or hidden fields to your blueprint. I usually have blueprints for them and extend them in my target pages blueprints. like this...
site/blueprints/fields/viewcount.yml
type: number min: 0 default: 0 disabled: true label: Visit Count
site/blueprints/fields/lastvisited.yml
type: hidden
in your page blueprint
fields: viewcount: extends: fields/viewcount lastvisited: extends: fields/lastvisited
site/config/config.php
<?php return [ 'bnomei.pageviewcounter.field.count' => 'viewcount', 'bnomei.pageviewcounter.field.timestamp' => 'lastvisited', 'bnomei.pageviewcounter.counter' => function () { return new \Bnomei\PageViewCounterField(); }, // other options ... ];
Warning
Be warned that Page Fields might not work well for concurrent requests.
Usage in Stats section via page-methods
Thanks to an PR by @TinQ0 it's also possible to use the tracked count and timestamp in sections like Pages or Stats.
in your page blueprint
sections: stats: type: stats reports: - label: View count of page value: "{{ page.pageviewcount }}" - label: View count of children + count of page itself value: "{{ page.children.pageviewcount(page.pageviewcount) }}" - label: Last visited value: "{{ page.pagelastvisited }}" # page.pagelastvisited('Y-m-d H:i:s') pages: type: pages info: "{{ page.pageviewcount }}" # show view count of each page
Note
The pageviewcount/pagelastvisited methods on the page object are prefix with page* to avoid clashing with the plain fields viewcount/lastvisited from the page field-setup variation.
Settings
| bnomei.pageviewcounter. | Default | Description |
|---|---|---|
| field.counter | fn() |
callback. returns instance of sqlite or field counter class |
| field.ignore-panel-users | true |
boolean. if true will not increment count if session is by a panel user. |
| field.sqlite.file | fn() |
callback. returns filepath to sqlite file |
| field.field.count | viewcount |
string. name of field in page blueprint |
| field.field.timestamp | lastvisited |
string. name of field in page blueprint |
| botDetection.CrawlerDetect | true |
check for crawlers (~10ms) |
| botDetection.DeviceDetector | true |
check for bots (~40ms) |
Disclaimer
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.
License
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.
bnomei/kirby3-pageviewcounter 适用场景与选型建议
bnomei/kirby3-pageviewcounter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.08k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2020 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「sqlite」 「static」 「seo」 「tracking」 「kirby」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bnomei/kirby3-pageviewcounter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnomei/kirby3-pageviewcounter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bnomei/kirby3-pageviewcounter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
repository php library
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
The tenancy/tenancy database driver for sqlite
Slimmed down concise interfaces and query builder for database queries and transactions which can be layered / decorated.
统计信息
- 总下载量: 1.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 26
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-27