From ff8570b526a0f6c2a47e661e9826e47d367ba485 Mon Sep 17 00:00:00 2001 From: theRAD Date: Fri, 6 Mar 2026 05:24:15 +0200 Subject: [PATCH] feat: add advanced device and referral analytics charts --- .../Http/Controllers/EcommerceController.php | 82 ++++++++++++++ src/resources/views/ecommerce.blade.php | 101 ++++++++++++++++++ src/routes/web.php | 1 + 3 files changed, 184 insertions(+) diff --git a/src/app/Http/Controllers/EcommerceController.php b/src/app/Http/Controllers/EcommerceController.php index 7d06d63..dbaa5bd 100644 --- a/src/app/Http/Controllers/EcommerceController.php +++ b/src/app/Http/Controllers/EcommerceController.php @@ -176,4 +176,86 @@ public function topProducts(Request $request) return response()->json(['error' => 'Failed to fetch WooCommerce data.'], 500); } } + + /** + * Extensive Analytics (Device Types, Referrals). + */ + public function analytics(Request $request) + { + $client = $this->wooClient(); + if (!$client) { + return response()->json(['error' => 'API credentials missing.'], 500); + } + + [$from, $to] = $this->dateRange($request); + + // Fetch up to 100 recent orders for analytics sampling + $params = [ + 'per_page' => 100, + 'orderby' => 'date', + 'order' => 'desc', + ]; + + if ($from) $params['after'] = $from . 'T00:00:00'; + if ($to) $params['before'] = $to . 'T23:59:59'; + + try { + $resp = $client->get('orders', $params); + $orders = $resp->successful() ? $resp->json() : []; + + $devices = []; + $sources = []; + + foreach ($orders as $order) { + if (!isset($order['meta_data'])) continue; + + $device = 'Unknown'; + $source = 'Direct / Organic'; + + foreach ($order['meta_data'] as $meta) { + if ($meta['key'] === '_wc_order_attribution_device_type') { + $device = ucfirst(strtolower($meta['value'])); + } + if ($meta['key'] === '_wc_order_attribution_utm_source') { + $source = ucfirst(strtolower($meta['value'])); + } elseif ($meta['key'] === '_wc_order_attribution_source_type' && strtolower($meta['value']) === 'organic') { + if ($source === 'Direct / Organic') { + $source = 'Organic Search'; + } + } + } + + // Aggregate Device + if (!isset($devices[$device])) { + $devices[$device] = 0; + } + $devices[$device]++; + + // Aggregate Source + if (!isset($sources[$source])) { + $sources[$source] = 0; + } + $sources[$source]++; + } + + // Sort sources by count DESC + arsort($sources); + $topSources = array_slice($sources, 0, 5, true); + + return response()->json([ + 'devices' => [ + 'labels' => array_keys($devices), + 'values' => array_values($devices) + ], + 'sources' => [ + 'labels' => array_keys($topSources), + 'values' => array_values($topSources) + ] + ]); + + } catch (\Exception $e) { + Log::error('WooCommerce API Error (analytics): ' . $e->getMessage()); + return response()->json(['error' => 'Failed to fetch WooCommerce data.'], 500); + } + } } diff --git a/src/resources/views/ecommerce.blade.php b/src/resources/views/ecommerce.blade.php index 9342fea..414d127 100644 --- a/src/resources/views/ecommerce.blade.php +++ b/src/resources/views/ecommerce.blade.php @@ -6,6 +6,7 @@ E-Commerce Dashboard - Stargas @vite(['resources/css/app.css', 'resources/js/app.js']) +