summaryrefslogtreecommitdiff
path: root/src/core/utils
diff options
context:
space:
mode:
authorHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-07-17 16:58:42 +0700
committerHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-07-17 16:58:42 +0700
commite76d537689d08fb4c15d482ffa996b8012dbc941 (patch)
tree17267b16ba50c89d55c29ea8c962312c05bdcb70 /src/core/utils
parent581334e8d077916c3a9ab87fd5b6e6b5126aba12 (diff)
parente39d3b0082e83ad08044918f0b6d8e977223100d (diff)
Merge branch 'Feature/voucher' into Feature/promotion_programvaoucher
# Conflicts: # src/lib/cart/components/Cart.jsx # src/lib/checkout/components/Checkout.jsx # src/lib/product/components/Product/Product.jsx # src/lib/product/components/Product/ProductDesktop.jsx # src/lib/product/components/Product/ProductMobile.jsx
Diffstat (limited to 'src/core/utils')
-rw-r--r--src/core/utils/googleTag.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/core/utils/googleTag.js b/src/core/utils/googleTag.js
new file mode 100644
index 00000000..6d7476bd
--- /dev/null
+++ b/src/core/utils/googleTag.js
@@ -0,0 +1,79 @@
+const mapVariants = (variants) => {
+ return variants.map((variant) => {
+ const res = {
+ item_id: variant.id,
+ item_name: variant.parent.name,
+ discount: variant.price.price - variant.price.priceDiscount,
+ item_brand: variant.manufacture?.name,
+ item_variant: variant.code || variant.id,
+ price: variant.price.price
+ }
+ if (variant?.quantity > 0) {
+ res.quantity = variant.quantity
+ }
+ return res
+ })
+}
+
+const sumTotal = (variants) => {
+ let totalPurchase = variants.reduce((total, x) => total + x.price.price, 0)
+ let totalDiscount = variants.reduce(
+ (total, x) => total + (x.price.price - x.price.priceDiscount),
+ 0
+ )
+ let subtotal = totalPurchase - totalDiscount
+ let tax = Math.round(subtotal * 0.11)
+ let grandTotal = subtotal + tax
+ return {
+ totalPurchase: totalPurchase,
+ totalDiscount: totalDiscount,
+ subtotal: subtotal,
+ tax: tax,
+ grandTotal: Math.round(grandTotal)
+ }
+}
+
+export const gtagAddToCart = (variant, quantity) => {
+ const param = {
+ currency: 'IDR',
+ value: variant.price.priceDiscount * quantity,
+ items: [
+ {
+ item_id: variant.id,
+ item_name: variant.name,
+ discount: variant.price.price - variant.price.priceDiscount,
+ item_brand: variant.manufacture?.name,
+ item_variant: variant.code || variant.id,
+ price: variant.price.price,
+ quantity
+ }
+ ]
+ }
+ gtag('event', 'add_to_cart', param)
+}
+
+export const gtagViewItem = (variants) => {
+ const items = mapVariants(variants)
+ const param = { currency: 'IDR', value: variants[0].price.priceDiscount, items }
+ gtag('event', 'view_item', param)
+}
+
+export const gtagBeginCheckout = (variants) => {
+ const items = mapVariants(variants)
+ const { subtotal } = sumTotal(variants)
+ const param = { currency: 'IDR', value: subtotal, items }
+ gtag('event', 'begin_checkout', param)
+}
+
+export const gtagPurchase = (variants, shipping, transactionId) => {
+ const items = mapVariants(variants)
+ const { grandTotal } = sumTotal(variants)
+ const param = {
+ currency: 'IDR',
+ value: grandTotal,
+ transactionId,
+ shipping,
+ items
+ }
+ gtag('event', 'purchase', param)
+}