Statistics module: visitor statistics without cookies
This commit is contained in:
1 parent
acb8620621
commit
7482ee489b
24 files changed
+1872
-4
No files matched your search
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Modules\Statistics;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use IriksIT\Manager\Modules\BaseModule;
|
||||
use IriksIT\Manager\Support\Permission;
|
||||
|
||||
/**
|
||||
* The site's own visitor statistics: page views, time on a page, where visitors came from, and the
|
||||
* programs that fetch pages. No cookies and no stored IP addresses. Off by default: the site
|
||||
* decides which of its addresses count (`manager.statistics.pages`, or a resolver of its own) and
|
||||
* puts the script that reports a page view on its pages.
|
||||
*/
|
||||
class StatisticsModule extends BaseModule
|
||||
{
|
||||
public function key(): string
|
||||
{
|
||||
return 'statistics';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Statistics';
|
||||
}
|
||||
|
||||
public function enabledByDefault(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [new Permission('statistics.view', 'See the visitor statistics and download them', 'Statistics')];
|
||||
}
|
||||
|
||||
public function navigation(): array
|
||||
{
|
||||
return [['label' => 'Statistics', 'route' => 'manager.statistics.index', 'permission' => 'statistics.view']];
|
||||
}
|
||||
|
||||
public function commands(): array
|
||||
{
|
||||
return [ForgetVisitorKeys::class];
|
||||
}
|
||||
|
||||
public function actions(): array
|
||||
{
|
||||
return ['statistics.export' => ['Exported the statistics', ['sheet']]];
|
||||
}
|
||||
|
||||
public function views(): array
|
||||
{
|
||||
return ['statistics.index' => 'Opened the statistics'];
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
app()->singleton(CountedPages::class);
|
||||
|
||||
// A person opens a few pages a minute; this only stops someone from flooding the statistics.
|
||||
RateLimiter::for('manager-pulse', fn (Request $request) => Limit::perMinute(max(1, (int) config('manager.statistics.per_minute', 120)))->by((string) $request->ip()));
|
||||
|
||||
// Programs never run the script that reports a view, so the server counts the pages it serves them.
|
||||
// Through the kernel: it copies its own groups to the router when a request starts.
|
||||
if (config('manager.statistics.count_bots') && method_exists($kernel = app(Kernel::class), 'appendMiddlewareToGroup')) {
|
||||
$kernel->appendMiddlewareToGroup('web', CountBots::class);
|
||||
}
|
||||
|
||||
// Without yesterday's key, yesterday's visitor codes can no longer be traced back.
|
||||
if (app()->runningInConsole()) {
|
||||
app()->afterResolving(Schedule::class, fn (Schedule $schedule) => $schedule->command(ForgetVisitorKeys::class)->dailyAt('00:05'));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user