You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"use client";import{trackEcomEvent}from"@/lib/datalayer";exportdefaultfunctionAddToCartButton({ product }){constonAdd=()=>{// your existing cart logic here...trackEcomEvent("add_to_cart",{currency: "BDT",value: product.price,items: [{item_id: product._id,item_name: product.name,item_category: product.category||"Uncategorized",price: product.price,quantity: 1,},],});};return<buttononClick={onAdd}>Add to cart</button>;}
C) begin_checkout when user proceeds to checkout
Put this on the Checkout button (from cart page/drawer):
"use client";import{trackEcomEvent}from"@/lib/datalayer";exportdefaultfunctionCheckoutButton({ cartItems }){constonCheckout=()=>{consttotalValue=cartItems.reduce((sum,i)=>sum+i.price*i.qty,0);consttotalQty=cartItems.reduce((sum,i)=>sum+i.qty,0);trackEcomEvent("begin_checkout",{currency: "BDT",value: totalValue,total_quantity: totalQty,items: cartItems.map((i)=>({item_id: i._id,item_name: i.name,item_category: i.category||"Uncategorized",price: i.price,quantity: i.qty,})),});// then route to checkout// router.push("/checkout");};return<buttononClick={onCheckout}>Proceed to Checkout</button>;}
D) purchase on success / thank you page
Important: fire purchase only after the order is confirmed (success page or after API confirms payment)
Example: app/checkout/success/page.js (client)
"use client";import{useEffect}from"react";import{trackEcomEvent}from"@/lib/datalayer";exportdefaultfunctionSuccessPage({ searchParams }){// Example: you can pass orderId in query params or fetch by sessionId// In App Router, you may fetch order details on server and pass as prop to a client component too.useEffect(()=>{// Replace this with actual order data from your backendconstorder={transaction_id: "ORD1767530045177533",value: 769,shipping: 59,discount: 0,currency: "BDT",total_quantity: 1,items: [{item_id: "6873f804b38361db35b4bb97",item_name: "KAO 8x4 Sarasara Switch Deodorant Spray (Rose & Verbena – Pink)",item_category: "Uncategorized",price: 710,quantity: 1,},],customer: {name: "hashed_name_here",email: "hashed_email_here",phone: "hashed_phone_here",address: "hashed_address_here",},};trackEcomEvent("purchase",{transaction_id: order.transaction_id,value: order.value,shipping: order.shipping,discount: order.discount,currency: order.currency,total_quantity: order.total_quantity,items: order.items,},{customer: order.customer});},[]);return<div>Payment Success ✅</div>;}