Skip to content

Commit

Permalink
Merge pull request #647 from getformwork/feature/statistics-sources-d…
Browse files Browse the repository at this point in the history
…evices

Improve Statistics with sources, devices and consecutive visits limiter
  • Loading branch information
giuscris authored Feb 22, 2025
2 parents 4dce550 + 76465d1 commit d18068a
Show file tree
Hide file tree
Showing 36 changed files with 665 additions and 101 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"symfony/yaml": "^7.0.3",
"league/commonmark": "^2.4",
"jaybizzle/crawler-detect": "^1.2",
"masterminds/html5": "^2.9"
"masterminds/html5": "^2.9",
"mobiledetect/mobiledetectlib": "^4.8"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
Expand Down
173 changes: 169 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions formwork/config/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ metadata: []
path: '${%ROOT_PATH%}/site'

routeAliases: []

statistics:
enabled: true
trackLocalhost: false
visitsDelay: 15
path: '${site.path}/statistics'
registries:
sessions: 'sessions.json'
visits: 'visits.json'
uniqueVisits: 'uniqueVisits.json'
visitors: 'visitors.json'
pageViews: 'pageViews.json'
sources: 'sources.json'
devices: 'devices.json'
cleanup:
ttl: 86400
probability: 5
4 changes: 0 additions & 4 deletions formwork/config/system.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ schemes:
system: '${%SYSTEM_PATH%}/schemes'
site: '${%ROOT_PATH%}/site/schemes'

statistics:
enabled: true
path: '${%ROOT_PATH%}/site/statistics'

templates:
path: '${%ROOT_PATH%}/site/templates'
extension: .php
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Cms/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private function loadServices(Container $container): void
->alias('templates');

$container->define(Statistics::class)
->parameter('path', fn(Config $config) => $config->get('system.statistics.path'))
->parameter('options', fn(Config $config) => $config->get('site.statistics'))
->parameter('translation', fn(Translations $translations) => $translations->getCurrent())
->alias('statistics');

Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
*/
public function load(RouteParams $routeParams, Statistics $statistics): Response
{
$trackable = $this->config->get('system.statistics.enabled');
$trackable = $this->config->get('site.statistics.enabled');

if ($this->site->get('maintenance.enabled') && !$this->app->panel()->isLoggedIn()) {
$trackable = false;
Expand Down
10 changes: 10 additions & 0 deletions formwork/src/Http/Utils/DeviceType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Formwork\Http\Utils;

enum DeviceType: string
{
case Mobile = 'mobile';
case Tablet = 'tablet';
case Desktop = 'desktop';
}
29 changes: 24 additions & 5 deletions formwork/src/Http/Utils/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Formwork\Http\Utils;

use Detection\MobileDetect;
use Formwork\Http\Request;
use Formwork\Traits\StaticClass;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
Expand All @@ -27,11 +28,29 @@ public static function isBrowser(Request $request): bool
return !self::isBot($request);
}

/**
* Detect whether current visitor prefers not to be tracked
*/
public static function isTrackable(Request $request): bool
public static function getDeviceType(Request $request): DeviceType
{
static $mobileDetect = new MobileDetect(config: ['autoInitOfHttpHeaders' => false]);
$mobileDetect->setUserAgent($request->userAgent() ?? '');
return match (true) {
$mobileDetect->isMobile() => DeviceType::Mobile,
$mobileDetect->isTablet() => DeviceType::Tablet,
default => DeviceType::Desktop,
};
}

public static function isMobile(Request $request): bool
{
return self::getDeviceType($request) === DeviceType::Mobile;
}

public static function isTablet(Request $request): bool
{
return self::getDeviceType($request) === DeviceType::Tablet;
}

public static function isDesktop(Request $request): bool
{
return $request->headers()->get('Dnt') !== '1';
return self::getDeviceType($request) === DeviceType::Desktop;
}
}
Loading

0 comments on commit d18068a

Please sign in to comment.