phase 5
This commit is contained in:
@@ -2,20 +2,22 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AnalyticsSession;
|
||||
use App\Models\Event;
|
||||
use App\Models\PageView;
|
||||
use App\Models\Visitor;
|
||||
use App\Jobs\ProcessTrackingPayload;
|
||||
use App\Models\Website;
|
||||
use App\Services\GoalEvaluationService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Jaybizzle\CrawlerDetect\CrawlerDetect;
|
||||
|
||||
class CollectController extends Controller
|
||||
{
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
if ($this->isBot($request)) {
|
||||
return response()->json(['ok' => false, 'reason' => 'bot'], 200);
|
||||
}
|
||||
|
||||
$payload = $request->validate([
|
||||
'tid' => 'required|string',
|
||||
'vid' => 'required|string|max:64',
|
||||
@@ -55,117 +57,81 @@ class CollectController extends Controller
|
||||
'is_new_session' => 'nullable|boolean',
|
||||
]);
|
||||
|
||||
$website = Website::where('tracking_id', $payload['tid'])
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
if (! $website) {
|
||||
return response()->json(['ok' => false], 404);
|
||||
if (! $this->isDomainValid($payload)) {
|
||||
return response()->json(['ok' => false, 'reason' => 'domain_mismatch'], 400);
|
||||
}
|
||||
|
||||
$now = Carbon::now();
|
||||
|
||||
$visitor = Visitor::updateOrCreate(
|
||||
[
|
||||
'website_id' => $website->id,
|
||||
'uid' => $payload['vid'],
|
||||
],
|
||||
[
|
||||
'last_seen_at' => $now,
|
||||
'first_seen_at' => $now,
|
||||
'device_type' => $payload['device'] ?? null,
|
||||
'browser' => $payload['browser'] ?? null,
|
||||
'browser_version' => $payload['browser_version'] ?? null,
|
||||
'os' => $payload['os'] ?? null,
|
||||
'os_version' => $payload['os_version'] ?? null,
|
||||
'screen_resolution' => $payload['sr'] ?? null,
|
||||
'language' => $payload['lang'] ?? null,
|
||||
],
|
||||
);
|
||||
|
||||
if ($visitor->wasRecentlyCreated) {
|
||||
$visitor->update(['first_seen_at' => $now]);
|
||||
}
|
||||
|
||||
$session = AnalyticsSession::firstOrCreate(
|
||||
['uid' => $payload['sid']],
|
||||
[
|
||||
'website_id' => $website->id,
|
||||
'visitor_id' => $visitor->id,
|
||||
'started_at' => $now,
|
||||
'entry_page' => $payload['path'] ?? $payload['url'] ?? null,
|
||||
'ip_address' => $request->ip(),
|
||||
'referrer_url' => $payload['referrer'] ?? null,
|
||||
'referrer_domain' => $payload['ref_domain'] ?? null,
|
||||
'utm_source' => $payload['utm_source'] ?? null,
|
||||
'utm_medium' => $payload['utm_medium'] ?? null,
|
||||
'utm_campaign' => $payload['utm_campaign'] ?? null,
|
||||
'utm_term' => $payload['utm_term'] ?? null,
|
||||
'utm_content' => $payload['utm_content'] ?? null,
|
||||
],
|
||||
);
|
||||
|
||||
if ($payload['type'] === 'pageview') {
|
||||
$this->recordPageView($website, $visitor, $session, $payload, $now);
|
||||
}
|
||||
|
||||
if ($payload['type'] === 'event') {
|
||||
$this->recordEvent($website, $visitor, $session, $payload, $now);
|
||||
}
|
||||
|
||||
$session->update([
|
||||
'ended_at' => $now,
|
||||
'duration' => $now->diffInSeconds($session->started_at),
|
||||
'exit_page' => $payload['path'] ?? $payload['url'] ?? $session->exit_page,
|
||||
'pages_count' => $session->pageViews()->count(),
|
||||
'is_bounce' => $session->pageViews()->count() <= 1,
|
||||
]);
|
||||
|
||||
$session->refresh();
|
||||
|
||||
app(GoalEvaluationService::class)->evaluate($website, $visitor, $session, $payload, $now);
|
||||
ProcessTrackingPayload::dispatch($payload, $request->ip(), Carbon::now());
|
||||
|
||||
return response()->json(['ok' => true]);
|
||||
}
|
||||
|
||||
private function recordPageView(
|
||||
Website $website,
|
||||
Visitor $visitor,
|
||||
AnalyticsSession $session,
|
||||
array $payload,
|
||||
Carbon $now,
|
||||
): void {
|
||||
PageView::create([
|
||||
'website_id' => $website->id,
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'url' => $payload['url'] ?? '',
|
||||
'path' => $payload['path'] ?? '/',
|
||||
'title' => $payload['title'] ?? null,
|
||||
'referrer' => $payload['referrer'] ?? null,
|
||||
'time_on_page' => $payload['time_on_page'] ?? null,
|
||||
'viewed_at' => $now,
|
||||
]);
|
||||
/**
|
||||
* 1x1 transparent GIF tracking pixel for no-JS fallback.
|
||||
* Usage: <img src="/api/pixel?tid=TA-XXX" width="1" height="1" alt="">
|
||||
*/
|
||||
public function pixel(Request $request): Response
|
||||
{
|
||||
if ($this->isBot($request)) {
|
||||
return $this->gifResponse();
|
||||
}
|
||||
|
||||
$tid = $request->get('tid');
|
||||
|
||||
if ($tid) {
|
||||
$payload = [
|
||||
'tid' => $tid,
|
||||
'vid' => $request->get('vid', md5($request->ip() . $request->userAgent())),
|
||||
'sid' => $request->get('sid', md5($request->ip() . $request->userAgent() . date('Y-m-d-H'))),
|
||||
'type' => 'pageview',
|
||||
'url' => $request->get('url', $request->header('Referer')),
|
||||
'path' => $request->get('path', parse_url($request->header('Referer', '/'), PHP_URL_PATH)),
|
||||
'referrer' => $request->header('Referer'),
|
||||
];
|
||||
|
||||
ProcessTrackingPayload::dispatch($payload, $request->ip(), Carbon::now());
|
||||
}
|
||||
|
||||
return $this->gifResponse();
|
||||
}
|
||||
|
||||
private function recordEvent(
|
||||
Website $website,
|
||||
Visitor $visitor,
|
||||
AnalyticsSession $session,
|
||||
array $payload,
|
||||
Carbon $now,
|
||||
): void {
|
||||
Event::create([
|
||||
'website_id' => $website->id,
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'name' => $payload['event_name'] ?? 'unknown',
|
||||
'category' => $payload['event_category'] ?? null,
|
||||
'action' => $payload['event_action'] ?? null,
|
||||
'label' => $payload['event_label'] ?? null,
|
||||
'value' => $payload['event_value'] ?? null,
|
||||
'properties' => $payload['event_properties'] ?? null,
|
||||
'occurred_at' => $now,
|
||||
private function isBot(Request $request): bool
|
||||
{
|
||||
return (new CrawlerDetect)->isCrawler($request->userAgent());
|
||||
}
|
||||
|
||||
private function isDomainValid(array $payload): bool
|
||||
{
|
||||
$url = $payload['url'] ?? null;
|
||||
if (! $url) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
if (! $host) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$website = Website::where('tracking_id', $payload['tid'])->first();
|
||||
if (! $website) {
|
||||
return true; // let the job handle the 404
|
||||
}
|
||||
|
||||
$domain = $website->domain;
|
||||
|
||||
// Allow exact match or subdomain match (e.g. www.example.com matches example.com)
|
||||
return $host === $domain || str_ends_with($host, '.' . $domain);
|
||||
}
|
||||
|
||||
private function gifResponse(): Response
|
||||
{
|
||||
// 1x1 transparent GIF
|
||||
$gif = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7');
|
||||
|
||||
return response($gif, 200, [
|
||||
'Content-Type' => 'image/gif',
|
||||
'Content-Length' => strlen($gif),
|
||||
'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user