fixed queries to use mysql functions for mysql and sqlite functions for sqlite, also commented some duplicate routes
This commit is contained in:
@@ -292,7 +292,7 @@ class AnalyticsQueryService
|
||||
$pvRaw = PageView::where('website_id', $this->website->id)
|
||||
->whereBetween('viewed_at', [$rawFrom, $rawTo])
|
||||
->select(
|
||||
DB::raw("strftime('{$groupFormat}', viewed_at) as period"),
|
||||
DB::raw($this->dateFormat($groupFormat, 'viewed_at').' as period'),
|
||||
DB::raw('COUNT(*) as page_views'),
|
||||
DB::raw('COUNT(DISTINCT visitor_id) as visitors'),
|
||||
)
|
||||
@@ -303,7 +303,7 @@ class AnalyticsQueryService
|
||||
$sessRaw = AnalyticsSession::where('website_id', $this->website->id)
|
||||
->whereBetween('started_at', [$rawFrom, $rawTo])
|
||||
->select(
|
||||
DB::raw("strftime('{$groupFormat}', started_at) as period"),
|
||||
DB::raw($this->dateFormat($groupFormat, 'started_at').' as period'),
|
||||
DB::raw('COUNT(*) as sessions'),
|
||||
)
|
||||
->groupBy('period')
|
||||
@@ -602,6 +602,9 @@ class AnalyticsQueryService
|
||||
|
||||
public function eventsOverview(int $limit = 50): Collection
|
||||
{
|
||||
$driver = DB::connection()->getDriverName();
|
||||
$castType = $driver === 'sqlite' ? 'REAL' : 'DECIMAL(10,2)';
|
||||
|
||||
return \App\Models\Event::where('website_id', $this->website->id)
|
||||
->whereBetween('occurred_at', [$this->from, $this->to])
|
||||
->select(
|
||||
@@ -609,7 +612,7 @@ class AnalyticsQueryService
|
||||
'category',
|
||||
DB::raw('COUNT(*) as total'),
|
||||
DB::raw('COUNT(DISTINCT visitor_id) as unique_visitors'),
|
||||
DB::raw('SUM(CAST(value AS REAL)) as total_value'),
|
||||
DB::raw("SUM(CAST(value AS {$castType})) as total_value"),
|
||||
)
|
||||
->groupBy('name', 'category')
|
||||
->orderByDesc('total')
|
||||
@@ -621,13 +624,16 @@ class AnalyticsQueryService
|
||||
|
||||
public function eventDetail(string $eventName): Collection
|
||||
{
|
||||
$driver = DB::connection()->getDriverName();
|
||||
$castType = $driver === 'sqlite' ? 'REAL' : 'DECIMAL(10,2)';
|
||||
|
||||
return \App\Models\Event::where('website_id', $this->website->id)
|
||||
->whereBetween('occurred_at', [$this->from, $this->to])
|
||||
->where('name', $eventName)
|
||||
->select(
|
||||
'action', 'label',
|
||||
DB::raw('COUNT(*) as total'),
|
||||
DB::raw('SUM(CAST(value AS REAL)) as total_value'),
|
||||
DB::raw("SUM(CAST(value AS {$castType})) as total_value"),
|
||||
)
|
||||
->groupBy('action', 'label')
|
||||
->orderByDesc('total')
|
||||
@@ -646,7 +652,7 @@ class AnalyticsQueryService
|
||||
->whereBetween('occurred_at', [$this->from, $this->to])
|
||||
->where('name', $eventName)
|
||||
->select(
|
||||
DB::raw("strftime('{$groupFormat}', occurred_at) as period"),
|
||||
DB::raw($this->dateFormat($groupFormat, 'occurred_at').' as period'),
|
||||
DB::raw('COUNT(*) as total'),
|
||||
)
|
||||
->groupBy('period')
|
||||
@@ -685,6 +691,28 @@ class AnalyticsQueryService
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate date formatting function for the current database driver.
|
||||
*/
|
||||
private function dateFormat(string $format, string $column): string
|
||||
{
|
||||
$driver = DB::connection()->getDriverName();
|
||||
|
||||
if ($driver === 'sqlite') {
|
||||
return "strftime('{$format}', {$column})";
|
||||
}
|
||||
|
||||
// MySQL/MariaDB
|
||||
// Convert SQLite strftime format to MySQL DATE_FORMAT
|
||||
$mysqlFormat = str_replace(
|
||||
['%Y', '%m', '%d', '%H', '%M', '%S'],
|
||||
['%Y', '%m', '%d', '%H', '%i', '%s'],
|
||||
$format
|
||||
);
|
||||
|
||||
return "DATE_FORMAT({$column}, '{$mysqlFormat}')";
|
||||
}
|
||||
|
||||
// ---- Conversions ----
|
||||
|
||||
public function conversionsReport(): Collection
|
||||
@@ -722,7 +750,7 @@ class AnalyticsQueryService
|
||||
$data = \App\Models\GoalConversion::where('website_id', $this->website->id)
|
||||
->whereBetween('converted_at', [$this->from, $this->to])
|
||||
->select(
|
||||
DB::raw("strftime('{$groupFormat}', converted_at) as period"),
|
||||
DB::raw($this->dateFormat($groupFormat, 'converted_at').' as period'),
|
||||
DB::raw('COUNT(*) as completions'),
|
||||
)
|
||||
->groupBy('period')
|
||||
|
||||
Reference in New Issue
Block a user