71 lines
1.7 KiB
PHP
71 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class AnalyticsEvent extends Model
|
|
{
|
|
const UPDATED_AT = null; // Only created_at timestamp
|
|
|
|
protected $fillable = [
|
|
'event_type',
|
|
'job_id',
|
|
'session_id',
|
|
'ip_address',
|
|
'user_agent',
|
|
'referer',
|
|
'event_data',
|
|
];
|
|
|
|
protected $casts = [
|
|
'event_data' => 'array',
|
|
'created_at' => 'datetime',
|
|
];
|
|
|
|
// Event types
|
|
const EVENT_JOB_VIEW = 'job_view';
|
|
const EVENT_JOB_APPLY_CLICK = 'job_apply_click';
|
|
const EVENT_SEARCH = 'search';
|
|
const EVENT_FILTER = 'filter';
|
|
const EVENT_CATEGORY_VIEW = 'category_view';
|
|
|
|
public function job(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Job::class);
|
|
}
|
|
|
|
/**
|
|
* Scope to get events by type
|
|
*/
|
|
public function scopeOfType($query, string $type)
|
|
{
|
|
return $query->where('event_type', $type);
|
|
}
|
|
|
|
/**
|
|
* Scope to get events within a date range
|
|
*/
|
|
public function scopeWithinDays($query, int $days)
|
|
{
|
|
return $query->where('created_at', '>=', now()->subDays($days));
|
|
}
|
|
|
|
/**
|
|
* Log an event
|
|
*/
|
|
public static function logEvent(string $eventType, ?int $jobId = null, array $eventData = []): void
|
|
{
|
|
self::create([
|
|
'event_type' => $eventType,
|
|
'job_id' => $jobId,
|
|
'session_id' => session()->getId(),
|
|
'ip_address' => request()->ip(),
|
|
'user_agent' => request()->userAgent(),
|
|
'referer' => request()->header('referer'),
|
|
'event_data' => $eventData,
|
|
]);
|
|
}
|
|
}
|