diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/components/layouts/BasicLayout.jsx | 15 | ||||
| -rw-r--r-- | src/core/utils/googleTag.js | 79 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx index 04b189f9..70482e12 100644 --- a/src/core/components/layouts/BasicLayout.jsx +++ b/src/core/components/layouts/BasicLayout.jsx @@ -2,11 +2,26 @@ import dynamic from 'next/dynamic' import BasicFooter from '../elements/Footer/BasicFooter' import Image from 'next/image' import whatsappUrl from '@/core/utils/whatsappUrl' +import { useEffect, useState } from 'react' +import axios from 'axios' +import odooApi from '@/core/api/odooApi' const Navbar = dynamic(() => import('../elements/Navbar/Navbar')) const AnimationLayout = dynamic(() => import('./AnimationLayout')) const BasicLayout = ({ children }) => { + useEffect(() => { + const getIP = async () => { + const ip = await odooApi('GET', '/api/ip-address') + const data = { + page_title: document.title, + url: window.location.href, + ip: ip + } + axios.get(`/api/user-activity?page_title=${data.page_title}&url=${data.url}&ip=${data.ip}`) + } + getIP() + }, []) return ( <> <Navbar /> 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) +} |
