2026-03-18 10:56:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
class Website extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'user_id',
|
|
|
|
|
'name',
|
|
|
|
|
'domain',
|
2026-03-25 20:39:10 +02:00
|
|
|
'slug',
|
2026-03-18 10:56:35 +02:00
|
|
|
'tracking_id',
|
|
|
|
|
'is_active',
|
|
|
|
|
'settings',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'is_active' => 'boolean',
|
|
|
|
|
'settings' => 'array',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function booted(): void
|
|
|
|
|
{
|
|
|
|
|
static::creating(function (Website $website) {
|
|
|
|
|
if (empty($website->tracking_id)) {
|
|
|
|
|
$website->tracking_id = 'TA-' . strtoupper(Str::random(10));
|
|
|
|
|
}
|
2026-03-25 20:39:10 +02:00
|
|
|
|
|
|
|
|
// Auto-generate slug from name if not provided
|
|
|
|
|
if (empty($website->slug)) {
|
|
|
|
|
$baseSlug = Str::slug($website->name);
|
|
|
|
|
$slug = $baseSlug;
|
|
|
|
|
$counter = 1;
|
|
|
|
|
|
|
|
|
|
// Ensure slug uniqueness
|
|
|
|
|
while (static::where('slug', $slug)->exists()) {
|
|
|
|
|
$slug = $baseSlug . '-' . $counter;
|
|
|
|
|
$counter++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$website->slug = $slug;
|
|
|
|
|
}
|
2026-03-18 10:56:35 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 20:39:10 +02:00
|
|
|
/**
|
|
|
|
|
* Get the route key for the model.
|
|
|
|
|
*/
|
|
|
|
|
public function getRouteKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'slug';
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 10:56:35 +02:00
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function visitors(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Visitor::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sessions(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(AnalyticsSession::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pageViews(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(PageView::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function events(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Event::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function goals(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Goal::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function goalConversions(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(GoalConversion::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 18:42:19 +02:00
|
|
|
public function funnels(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Funnel::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function segments(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Segment::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dashboardWidgets(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DashboardWidget::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function alerts(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Alert::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function annotations(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Annotation::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 14:53:54 +02:00
|
|
|
public function dailyAggregates(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(DailyAggregate::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function pageDailyAggregates(): HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(PageDailyAggregate::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDataRetentionDaysAttribute(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->settings['data_retention_days'] ?? 365;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 10:56:35 +02:00
|
|
|
public function embedSnippet(): string
|
|
|
|
|
{
|
2026-03-24 22:42:31 +02:00
|
|
|
$scriptUrl = route('tracking.script.alt', $this->tracking_id);
|
2026-03-18 10:56:35 +02:00
|
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
|
<!-- Analytics Tracker -->
|
|
|
|
|
<script defer src="{$scriptUrl}"></script>
|
|
|
|
|
HTML;
|
|
|
|
|
}
|
2026-03-24 22:42:31 +02:00
|
|
|
|
|
|
|
|
public function embedSnippetLegacy(): string
|
|
|
|
|
{
|
|
|
|
|
$scriptUrl = route('tracking.script', $this->tracking_id);
|
|
|
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
|
<!-- Analytics Tracker (Legacy) -->
|
|
|
|
|
<script defer src="{$scriptUrl}"></script>
|
|
|
|
|
HTML;
|
|
|
|
|
}
|
2026-03-18 10:56:35 +02:00
|
|
|
}
|