From d4039c6e3b1c0cc836738d5c469a5e711523415c Mon Sep 17 00:00:00 2001 From: Unchained Date: Tue, 31 Mar 2026 05:53:53 +0200 Subject: [PATCH] feat(analytics): complete Rybbit tracking integration - Add Rybbit server-side tracking to analytics-server.ts for order completion and revenue - Add trackNewsletterSignup to analytics.ts and wire up NewsletterSection - Add cart tracking to CartDrawer (cart view, remove from cart) - All ecommerce events now track to both OpenPanel and Rybbit --- src/components/cart/CartDrawer.tsx | 28 +++++++++++- src/components/home/NewsletterSection.tsx | 3 ++ src/lib/analytics-server.ts | 56 ++++++++++++++++++++++- src/lib/analytics.ts | 12 +++++ 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/src/components/cart/CartDrawer.tsx b/src/components/cart/CartDrawer.tsx index dedccfd..0a1a75a 100644 --- a/src/components/cart/CartDrawer.tsx +++ b/src/components/cart/CartDrawer.tsx @@ -8,6 +8,7 @@ import { X, Minus, Plus, Trash2, ShoppingBag } from "lucide-react"; import { useTranslations, useLocale } from "next-intl"; import { useSaleorCheckoutStore } from "@/stores/saleorCheckoutStore"; import { formatPrice } from "@/lib/saleor"; +import { useAnalytics } from "@/lib/analytics"; export default function CartDrawer() { const t = useTranslations("Cart"); @@ -26,11 +27,13 @@ export default function CartDrawer() { initCheckout, clearError, } = useSaleorCheckoutStore(); + const { trackCartView, trackRemoveFromCart } = useAnalytics(); const lines = getLines(); const total = getTotal(); const lineCount = getLineCount(); const initializedRef = useRef(false); + const lastCartStateRef = useRef<{ count: number; total: number } | null>(null); useEffect(() => { if (!initializedRef.current && locale) { @@ -52,6 +55,22 @@ export default function CartDrawer() { }; }, [isOpen]); + useEffect(() => { + if (isOpen && lines.length > 0) { + const currentState = { count: lineCount, total }; + if (!lastCartStateRef.current || + lastCartStateRef.current.count !== currentState.count || + lastCartStateRef.current.total !== currentState.total) { + trackCartView({ + total, + currency: checkout?.totalPrice?.gross?.currency || "RSD", + item_count: lineCount, + }); + lastCartStateRef.current = currentState; + } + } + }, [isOpen, lineCount, total]); + return ( {isOpen && ( @@ -181,7 +200,14 @@ export default function CartDrawer() {