(function(shopify) {shopify.extend('WebPixel::Render', function(api) { var analytics=api.analytics,browser=api.browser,init=api.init; // Code Created By FeedArmy V1.0816 (please don't remove the creator) // Copyright FeedArmy Co., Ltd. const GOOGLE_TAG_MANAGER_ID = 'GTM-MHVPWBG'; const GOOGLE_MERCHANT_CENTER_ID = '10509684'; const PRODUCT_COUNTRY_CODE = 'US'; const ESTIMATED_SHIPPING_TIME = 3; const GRANT_CONSENT = true; // Use false or true // for the ID_TYPE, if your products look like shopify_US_123456789_123456798 add the word shopify // if your products are sku's add the value sku -- const ID_TYPE = 'sku'; // if your proudcts use the variant id use the value variant_id -- const ID_TYPE = 'variant_id'; // if your proudcts use the parent id use the value parent_id -- const ID_TYPE = 'parent_id'; const ID_TYPE = 'shopify'; // If you want to capitalize the SKU set CAPITALIZE_SKU to true, if you don't want to change anything set to false const CAPITALIZE_SKU = false; // SETTINGS END // SETTINGS END // 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', GOOGLE_TAG_MANAGER_ID); if (GRANT_CONSENT === true){ //Google Consent Mode v2 gtag('consent', 'update', { 'ad_storage': 'granted', 'analytics_storage': 'granted', 'ad_user_data': 'granted', 'ad_personalization': 'granted', }); } const createLineItemsData = (lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU) => { return lineItems.map((item) => { let itemId; if (ID_TYPE === "shopify") { itemId = "shopify_" + PRODUCT_COUNTRY_CODE + "_" + item.variant?.product?.id + "_" + item.variant?.id; } else if (ID_TYPE === "parent_id") { itemId = item.variant?.product?.id; } else if (ID_TYPE === "sku") { itemId = item.variant?.sku; if (CAPITALIZE_SKU) { itemId = itemId.toUpperCase(); } } else { itemId = item.variant?.id; } // check for discount let discount = item.discountAllocations?.reduce((total, discountAllocation) => { return total + parseFloat(discountAllocation.amount.amount); }, 0) || 0; // calculate price let price = parseFloat(item.variant?.price?.amount) - discount; return { item_id: itemId, item_name: item.variant?.product?.title, price: price, quantity: item.quantity }; }); }; const createLineItemsDataCart = (lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU) => { return lineItems.map((item) => { let itemId; if (ID_TYPE === "shopify") { itemId = "shopify_" + PRODUCT_COUNTRY_CODE + "_" + item.merchandise?.id + "_" + item.merchandise?.product?.id; } else if (ID_TYPE === "parent_id") { itemId = item.merchandise?.id; } else if (ID_TYPE === "sku") { itemId = item.merchandise?.sku; if (CAPITALIZE_SKU) { itemId = itemId.toUpperCase(); } } else { itemId = item.merchandise?.product?.id; } return { item_id: itemId, item_name: item.merchandise?.product?.title, price: item.cost?.totalAmount?.amount, quantity: item.quantity }; }); }; const createLineItemsDataAddToCart = (item, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU) => { let itemId; if (ID_TYPE === "shopify") { itemId = "shopify_" + PRODUCT_COUNTRY_CODE + "_" + item.merchandise?.id + "_" + item.merchandise?.product?.id; } else if (ID_TYPE === "parent_id") { itemId = item.merchandise?.id; } else if (ID_TYPE === "sku") { itemId = item.merchandise?.sku; if (CAPITALIZE_SKU) { itemId = itemId.toUpperCase(); } } else { itemId = item.merchandise?.product?.id; } return { item_id: itemId, item_name: item.merchandise?.product?.title, price: item.merchandise?.price?.amount, quantity: item.quantity }; }; const createLineItemsDataProductView = (item, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU) => { let itemId; if (ID_TYPE === "shopify") { itemId = "shopify_" + PRODUCT_COUNTRY_CODE + "_" + item.product?.id + "_" + item.id; } else if (ID_TYPE === "parent_id") { itemId = item.product?.id; } else if (ID_TYPE === "sku") { itemId = item.sku; if (CAPITALIZE_SKU) { itemId = itemId.toUpperCase(); } } else { itemId = item.id; } return { item_id: itemId, item_name: item.product?.title, price: item.price?.amount, quantity: 1 }; }; const calculateTotalDiscount = (lineItems) => { let totalDiscount = 0; lineItems.forEach(item => { item.discountAllocations.forEach(discount => { totalDiscount += parseFloat(discount.amount.amount); }); }); return totalDiscount.toFixed(2); // Convert to string with 2 decimal places }; analytics.subscribe("checkout_completed", (event) => { const lineItemsData = createLineItemsData(event.data?.checkout?.lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); const email = event.data?.checkout?.email; const phone = event.data?.checkout?.phone; const address = event.data?.checkout?.shippingAddress; const totalDiscountAmount = calculateTotalDiscount(event.data?.checkout?.lineItems); let shopifyPixelData = { currency: event.data?.checkout?.subtotalPrice?.currencyCode, value: event.data?.checkout?.subtotalPrice?.amount, transaction_id: event.data?.checkout?.order?.id, shipping: event.data?.checkout?.shippingLine?.price?.amount, tax: event.data?.checkout?.totalTax?.amount, }; if (email) { shopifyPixelData.email = email; } if (phone) { shopifyPixelData.phone = phone; } if (address) { let addressData = {}; if (address.firstName) addressData.first_name = address.firstName; if (address.lastName) addressData.last_name = address.lastName; if (address.address1) addressData.street = address.address1; if (address.city) addressData.city = address.city; if (address.province) addressData.region = address.province; if (address.zip) addressData.postal_code = address.zip; if (address.provinceCode) addressData.province_code = address.provinceCode; if (address.country) addressData.country = address.country; if (address.countryCode) addressData.country_code = address.countryCode; if (Object.keys(addressData).length > 0) { shopifyPixelData.address = addressData; } } shopifyPixelData.aw_merchant_id = GOOGLE_MERCHANT_CENTER_ID; shopifyPixelData.aw_feed_country = address.countryCode; shopifyPixelData.discount = totalDiscountAmount; shopifyPixelData.shipping = event.data?.checkout?.shippingLine?.price?.amount; shopifyPixelData.estimated_shipping_time = ESTIMATED_SHIPPING_TIME; shopifyPixelData.items = lineItemsData; dataLayer.push({ ecommerce: null }); dataLayer.push({ event: "purchase", url: event.context.document.location.href, ecommerce: shopifyPixelData }); }); analytics.subscribe("product_added_to_cart", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsDataAddToCart(event.data?.cartLine, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "add_to_cart", url: event.context.document.location.href, ecommerce: { currency: event.data?.cartLine?.cost?.totalAmount?.currencyCode, value: event.data?.cartLine?.cost?.totalAmount?.amount, items: [items] } }); }); analytics.subscribe("cart_viewed", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsDataCart(event.data?.cart?.lines, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "view_cart", url: event.context.document.location.href, ecommerce: { currency: event.data?.cart?.cost?.totalAmount?.currencyCode, value: event.data?.cart?.cost?.totalAmount?.amount, items: [items] } }); }); analytics.subscribe("checkout_started", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsData(event.data?.checkout?.lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "begin_checkout", url: event.context.document.location.href, ecommerce: { currency: event.data?.checkout?.currencyCode, value: event.data?.checkout?.subtotalPrice?.amount, items: [items] } }); }); analytics.subscribe("payment_info_submitted", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsData(event.data?.checkout?.lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "add_payment_info", url: event.context.document.location.href, ecommerce: { currency: event.data?.checkout?.currencyCode, value: event.data?.checkout?.subtotalPrice?.amount, items: [items] } }); }); analytics.subscribe("checkout_shipping_info_submitted", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsData(event.data?.checkout?.lineItems, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "add_shipping_info", url: event.context.document.location.href, ecommerce: { currency: event.data?.checkout?.currencyCode, value: event.data?.checkout?.subtotalPrice?.amount, items: [items] } }); }); analytics.subscribe("search_submitted", (event) => { dataLayer.push({ ecommerce: null }); dataLayer.push({ event: "search_submitted", url: event.context.document.location.href, ecommerce: { query: event.data?.searchResult?.query, } }); }); analytics.subscribe("collection_viewed", (event) => { dataLayer.push({ ecommerce: null }); dataLayer.push({ event: "view_collection", url: event.context.document.location.href, ecommerce: { collection_id: event.data?.collection?.id, collection_title: event.data?.collection?.title, } }); }); analytics.subscribe("product_viewed", (event) => { dataLayer.push({ ecommerce: null }); const items = createLineItemsDataProductView(event.data?.productVariant, ID_TYPE, PRODUCT_COUNTRY_CODE, CAPITALIZE_SKU); dataLayer.push({ event: "view_item", url: event.context.document.location.href, ecommerce: { currency: event.data?.productVariant?.price?.currencyCode, value: event.data?.productVariant?.price?.amount, items: [items] } }); }); analytics.subscribe("page_viewed", (event) => { window.dataLayer.push({ event: "shopify_page_view", url: event.context.document.location.href }); }); });})(self.webPixelsManager.createShopifyExtend('24969401', 'custom'));