@@ -4312,8 +4312,11 @@ def sales_analytics(request):
43124312 total_revenue = orders .aggregate (total = Sum ("total_price" ))["total" ] or 0
43134313 total_orders = orders .count ()
43144314
4315- # Placeholder conversion rate (to be implemented properly later)
4316- conversion_rate = 0.00 # Temporary placeholder
4315+ # Calculate conversion rate: completed orders / storefront page visits * 100
4316+ total_visits = WebRequest .objects .filter (
4317+ path__icontains = f"/storefront/{ storefront .store_slug } /"
4318+ ).aggregate (total = Sum ("count" ))["total" ] or 0
4319+ conversion_rate = round ((total_orders / total_visits * 100 ), 2 ) if total_visits > 0 else 0.00
43174320
43184321 # Best selling products
43194322 best_selling_products = (
@@ -4347,9 +4350,11 @@ def sales_data(request):
43474350 # Calculate total orders
43484351 total_orders = orders .count ()
43494352
4350- # Calculate conversion rate (orders / visits * 100)
4351- total_visits = WebRequest .objects .filter (path__contains = "ref=" ).count () # Adjust based on visit tracking
4352- conversion_rate = (total_orders / total_visits * 100 ) if total_visits > 0 else 0.00
4353+ # Calculate conversion rate: completed/shipped orders / storefront page visits * 100
4354+ total_visits = WebRequest .objects .filter (
4355+ path__icontains = f"/storefront/{ storefront .store_slug } /"
4356+ ).aggregate (total = Sum ("count" ))["total" ] or 0
4357+ conversion_rate = round ((total_orders / total_visits * 100 ), 2 ) if total_visits > 0 else 0.00
43534358
43544359 # Get best-selling products
43554360 best_selling_products = (
0 commit comments