move into sonardesk
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Analytics\Services;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Analytics\Models\AnalyticsEvent;
|
||||
|
||||
final class AnalyticsTracker
|
||||
{
|
||||
public function track(Request $request, array $payload): ?AnalyticsEvent
|
||||
{
|
||||
if (! config('analytics.enabled')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (config('analytics.respect_dnt') && $request->header('DNT') === '1') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$path = ltrim((string) ($payload['path'] ?? ''), '/');
|
||||
|
||||
foreach (config('analytics.exclude_paths', []) as $excluded) {
|
||||
if ($excluded !== '' && str_starts_with($path, trim($excluded, '/'))) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$ip = (string) $request->ip();
|
||||
$visitorIdentifier = (string) ($payload['visitor_id'] ?? $request->cookie('analytics_visitor', 'guest'));
|
||||
$sessionIdentifier = (string) ($payload['session_id'] ?? $this->sessionIdentifier($request, $visitorIdentifier));
|
||||
|
||||
return AnalyticsEvent::create([
|
||||
'event_id' => (string) Str::uuid(),
|
||||
'site_id' => app()->bound('currentSite') ? app('currentSite')?->id : null,
|
||||
'event_type' => $payload['event_type'] ?? 'page_view',
|
||||
'path' => '/'.$path,
|
||||
'title' => $payload['title'] ?? null,
|
||||
'referrer' => $payload['referrer'] ?? $request->headers->get('referer'),
|
||||
'visitor_hash' => hash('sha256', $visitorIdentifier),
|
||||
'session_hash' => hash('sha256', $sessionIdentifier),
|
||||
'ip_hash' => config('analytics.anonymize_ip') ? hash('sha256', $ip.config('app.key')) : $ip,
|
||||
'device_type' => $payload['device_type'] ?? null,
|
||||
'browser' => $payload['browser'] ?? null,
|
||||
'operating_system' => $payload['operating_system'] ?? null,
|
||||
'subject_type' => $payload['subject_type'] ?? null,
|
||||
'subject_id' => $payload['subject_id'] ?? null,
|
||||
'properties' => $payload['properties'] ?? [],
|
||||
'occurred_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function sessionIdentifier(Request $request, string $visitorIdentifier): string
|
||||
{
|
||||
if ($request->hasSession()) {
|
||||
return $request->session()->getId();
|
||||
}
|
||||
|
||||
return (string) ($request->header('X-Analytics-Session') ?: $visitorIdentifier ?: 'stateless');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user