From a7160110ab082109e96696c3bc2321f28610958c Mon Sep 17 00:00:00 2001
From: it-fixcomart <it@fixcomart.co.id>
Date: Thu, 20 Feb 2025 11:25:55 +0700
Subject: <iman> add google tag button add to cart

---
 .../product-detail/components/AddToCart.tsx        | 37 +++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

(limited to 'src-migrate/modules/product-detail/components/AddToCart.tsx')

diff --git a/src-migrate/modules/product-detail/components/AddToCart.tsx b/src-migrate/modules/product-detail/components/AddToCart.tsx
index 280e4a7a..95bc1d88 100644
--- a/src-migrate/modules/product-detail/components/AddToCart.tsx
+++ b/src-migrate/modules/product-detail/components/AddToCart.tsx
@@ -15,7 +15,8 @@ import { useProductCartContext } from '@/contexts/ProductCartContext';
 import { createSlug } from '~/libs/slug';
 import formatCurrency from '~/libs/formatCurrency';
 import { useProductDetail } from '../stores/useProductDetail';
-
+import { gtagAddToCart } from '@/core/utils/googleTag';
+import axios from 'axios';
 type Props = {
   variantId: number | null;
   quantity?: number;
@@ -50,6 +51,38 @@ const AddToCart = ({
     isLoading,
     setIsloading,
   } = useProductCartContext();
+  const [activeVariant, setActiveVariant] = useState({
+    id: 0,
+    code: '',
+    name: '',
+    price: '',
+    stock: '',
+    weight: '',
+    isFlashSale: false,
+  });
+
+  useEffect(() => {
+    const fetchData = async () => {
+      if (variantId) {
+        let response = await axios(
+          `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/variant-detail?id=${variantId}`
+        );
+        let productVariant = response.data;
+        if (productVariant) {
+          setActiveVariant({
+            id: productVariant[0].id,
+            code: productVariant[0].code,
+            name: productVariant[0].name,
+            price: productVariant[0].price.price,
+            stock: productVariant[0].stockTotal,
+            weight: productVariant[0].weight,
+            isFlashSale: productVariant[0].isFlashsale,
+          });
+        }
+      }
+    };
+    fetchData();
+  }, [variantId]);
 
   const productSimilarQuery = [
     product?.name,
@@ -101,6 +134,8 @@ const AddToCart = ({
     setRefreshCart(true);
     setAddCartAlert(true);
 
+    gtagAddToCart(activeVariant, quantity);
+
     toast({
       title: 'Tambah ke keranjang',
       description: 'Berhasil menambahkan barang ke keranjang belanja',
-- 
cgit v1.2.3

