(function(shopify) {shopify.extend('WebPixel::Render', function(api) { var analytics=api.analytics,browser=api.browser,init=api.init; // Define dataLayer and the gtag function. window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } //Initialize GTM tag (function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' }); var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-KRMXFXD2'); //Google Consent Mode v2 gtag('consent', 'update', { 'ad_storage': 'granted', 'analytics_storage': 'granted', 'ad_user_data': 'granted', 'ad_personalization': 'granted', }); /* *************** CHECKOUT PRODUCTS ARRAY HELPER FUNCTION *************** */ const processCheckoutProducts = (items) => { let finalProductArray = []; let orderCoupon = []; // to hold the discount titles if (items) { items.forEach((item, index) => { let itemDiscountAmount = 0; // Process discounts for this item item.discountAllocations?.forEach((allocation) => { const discount = allocation.discountApplication; // Capture the discount title if not already added if (discount.title && !orderCoupon.includes(discount.title)) { orderCoupon.push(discount.title); } // Accumulate discount amount for the item const allocationAmount = allocation.amount.amount; itemDiscountAmount += allocationAmount; }); // GA4: Calculate price after discount const itemPrice = item.variant.price.amount; const priceAfterDiscount = itemPrice - (itemDiscountAmount / item.quantity); // Helper to get property value by key const getPropertyValue = (props, key) => props?.find(p => p.key === key)?.value; // Determine if pricebeat was applied const pricebeatValue = getPropertyValue(item.properties, "_added_with_pricebeat") === "true" ? "applied" : ""; // GA4 let lineItem = { item_id: item.variant?.product?.id, item_name: item.variant?.product?.untranslatedTitle, price: priceAfterDiscount, item_brand: item.variant?.product?.vendor, item_category: getPropertyValue(item.properties, "_gtm_item_category_l1"), item_category2: getPropertyValue(item.properties, "_gtm_item_category_l2"), item_category3: getPropertyValue(item.properties, "_gtm_item_category_l3"), quantity: item.quantity, product_pricebeat: pricebeatValue, coupon: orderCoupon.join(',') || undefined, discount: itemDiscountAmount / item.quantity }; finalProductArray.push(lineItem); }); } return { items: finalProductArray, orderCouponString: orderCoupon.join(',') }; }; /* *************** END OF CHECKOUT PRODUCTS ARRAY HELPER FUNCTION *************** */ /* *************** BEGIN CHECKOUT DATALAYER *************** */ analytics.subscribe('checkout_started', (event) => { const eventContextData = event.context?.document; const checkout = event.data?.checkout; let orderDiscountAmount = checkout.discountsAmount?.amount || 0; // Construct the data layer object: const dataLayerObj = { event: 'begin_checkout', category: "ecommerce", action: "step 1 - customer overview", ecommerce: { currency: checkout?.currencyCode, value: checkout.subtotalPrice?.amount || 0, checkout_step: 1, checkout_option: event.name, coupon: processCheckoutProducts(checkout?.lineItems).orderCouponString || undefined, discount: orderDiscountAmount, items: processCheckoutProducts(checkout?.lineItems).items } } // Push the content to the dataLayer: window.dataLayer.push({ 'ecommerce': null }); window.dataLayer.push(dataLayerObj); }); /* *************** END OF BEGIN CHECKOUT *************** */ /* *************** PURCHASE *************** */ analytics.subscribe('checkout_completed', (event) => { const eventContextData = event.context?.document; const checkout = event.data?.checkout; let orderDiscountAmount = checkout.discountsAmount?.amount || 0; // Determine the payment type const paymentType = checkout?.transactions?. [0]?.paymentMethod?.type || undefined; // Construct the data layer object const dataLayerObj = { event: 'purchase', category: "ecommerce", action: "step 4 - purchase", ecommerce: { value: checkout.subtotalPrice?.amount || 0, currency: checkout?.currencyCode, checkout_step: 1, checkout_option: event.name, coupon: processCheckoutProducts(checkout?.lineItems).orderCouponString || undefined, discount: orderDiscountAmount, payment_type: paymentType, transaction_id: checkout?.order?.id, tax: checkout?.totalTax?.amount || 0, shipping: checkout?.shippingLine?.price?.amount || 0, items: processCheckoutProducts(checkout?.lineItems).items } }; // Push the content to the dataLayer: window.dataLayer.push({ 'ecommerce': null }); window.dataLayer.push(dataLayerObj); }); /* *************** END OF PURCHASE *************** */ });})(self.webPixelsManager.createShopifyExtend('101056577', 'custom'));