phase 8 and 9
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?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;
|
||||
|
||||
class Alert extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'name',
|
||||
'metric',
|
||||
'condition',
|
||||
'threshold',
|
||||
'period',
|
||||
'channel',
|
||||
'is_active',
|
||||
'last_triggered_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'threshold' => 'decimal:2',
|
||||
'is_active' => 'boolean',
|
||||
'last_triggered_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
|
||||
public function notifications(): HasMany
|
||||
{
|
||||
return $this->hasMany(AlertNotification::class);
|
||||
}
|
||||
|
||||
public static array $metrics = [
|
||||
'page_views' => 'Page Views',
|
||||
'visitors' => 'Unique Visitors',
|
||||
'sessions' => 'Sessions',
|
||||
'bounce_rate' => 'Bounce Rate',
|
||||
];
|
||||
|
||||
public static array $conditions = [
|
||||
'above' => 'goes above',
|
||||
'below' => 'drops below',
|
||||
'change_up' => '% increase exceeds',
|
||||
'change_down' => '% decrease exceeds',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AlertNotification extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'alert_id',
|
||||
'value',
|
||||
'message',
|
||||
'is_read',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'value' => 'decimal:2',
|
||||
'is_read' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function alert(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Alert::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Annotation extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'user_id',
|
||||
'date',
|
||||
'text',
|
||||
'category',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'date' => 'date',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public static array $categories = [
|
||||
'general' => 'General',
|
||||
'deployment' => 'Deployment',
|
||||
'campaign' => 'Campaign',
|
||||
'incident' => 'Incident',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class DashboardWidget extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'title',
|
||||
'widget_type',
|
||||
'config',
|
||||
'sort_order',
|
||||
'size',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'config' => 'array',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
|
||||
public static array $types = [
|
||||
'metric' => 'Single Metric',
|
||||
'chart' => 'Visitors Chart',
|
||||
'top_pages' => 'Top Pages',
|
||||
'top_sources' => 'Top Sources',
|
||||
'events' => 'Events Overview',
|
||||
'conversions' => 'Goal Conversions',
|
||||
'new_vs_returning' => 'New vs Returning',
|
||||
'browsers' => 'Browsers',
|
||||
'countries' => 'Countries',
|
||||
];
|
||||
|
||||
public static array $metrics = [
|
||||
'page_views' => 'Page Views',
|
||||
'visitors' => 'Unique Visitors',
|
||||
'sessions' => 'Sessions',
|
||||
'bounce_rate' => 'Bounce Rate',
|
||||
'avg_duration' => 'Avg. Duration',
|
||||
'avg_pages' => 'Pages / Session',
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Funnel extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'name',
|
||||
'steps',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'steps' => 'array',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Segment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'name',
|
||||
'filters',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'filters' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the visitor IDs that match this segment's filters.
|
||||
*/
|
||||
public function matchingVisitorIds(): \Illuminate\Support\Collection
|
||||
{
|
||||
$query = Visitor::where('website_id', $this->website_id);
|
||||
|
||||
foreach ($this->filters as $filter) {
|
||||
$field = $filter['field'];
|
||||
$operator = $filter['operator'] ?? '=';
|
||||
$value = $filter['value'];
|
||||
|
||||
$allowedFields = ['country', 'region', 'city', 'browser', 'os', 'device_type', 'language', 'screen_resolution'];
|
||||
if (! in_array($field, $allowedFields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ($operator) {
|
||||
'=' => $query->where($field, $value),
|
||||
'!=' => $query->where($field, '!=', $value),
|
||||
'contains' => $query->where($field, 'like', "%{$value}%"),
|
||||
'starts_with' => $query->where($field, 'like', "{$value}%"),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
return $query->pluck('id');
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -8,11 +8,12 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
@@ -73,6 +73,31 @@ class Website extends Model
|
||||
return $this->hasMany(GoalConversion::class);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function dailyAggregates(): HasMany
|
||||
{
|
||||
return $this->hasMany(DailyAggregate::class);
|
||||
|
||||
Reference in New Issue
Block a user