From e12765964e2e9c834205634533cb9fb3300fbb4c Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Mon, 29 May 2023 14:41:38 +0700
Subject: Add gtag event conversion on checkout

---
 src/lib/checkout/components/Checkout.jsx | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

(limited to 'src/lib/checkout/components/Checkout.jsx')

diff --git a/src/lib/checkout/components/Checkout.jsx b/src/lib/checkout/components/Checkout.jsx
index 088b641b..525a641b 100644
--- a/src/lib/checkout/components/Checkout.jsx
+++ b/src/lib/checkout/components/Checkout.jsx
@@ -233,12 +233,26 @@ const Checkout = () => {
       return
     }
 
-    for (const product of products) deleteItemCart({ productId: product.id })
-    const payment = await axios.post(
-      `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
-    )
-    setIsLoading(false)
-    window.location.href = payment.data.redirectUrl
+    const midtrans = async () => {
+      for (const product of products) deleteItemCart({ productId: product.id })
+      const payment = await axios.post(
+        `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/midtrans-payment?transactionId=${isCheckouted.id}`
+      )
+      setIsLoading(false)
+      window.location.href = payment.data.redirectUrl
+    }
+
+    gtag('event', 'conversion', {
+      send_to: 'AW-954540379/nDymCL3BhaQYENvClMcD',
+      value:
+        totalAmount -
+        totalDiscountAmount +
+        taxTotal +
+        Math.round(parseInt(biayaKirim * 1.1) / 1000) * 1000,
+      currency: 'IDR',
+      transaction_id: isCheckouted.id,
+      event_callback: midtrans
+    })
   }
   const taxTotal = (totalAmount - totalDiscountAmount) * 0.11
 
-- 
cgit v1.2.3

