// {% elsif template contains 'collection' %} // // {% elsif template contains 'product' %} // // {% elsif template contains 'cart' %} // // {% elsif template contains 'search' %} // // {% else %} // // {% endif %} analytics.subscribe('page_viewed', (event) => { // Example for accessing event data const timeStamp = event.timestamp; const pageEventId = event.id; const content = event.context; const window = content.window; const url = window.location.href; const pathname = window.location.pathname; // const payload = { // event_name: event.name, // event_data: { // pageEventId: pageEventId, // timeStamp: timeStamp, // }, // }; // Example for sending event data to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); const possibleHomePaths = ['/', '/index.html', '/index']; let homePage = possibleHomePaths.includes(pathname)?true:false; gtag('event', 'page_view', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': homePage?'home':'other' }); }); analytics.subscribe('collection_viewed', (event) => { // Example for accessing event data // const collection = event.data.collection; // const collectionTitle = collection.title; // const priceOfFirstProductInCollection = // collection.productVariants[0]?.price.amount; // const payload = { // event_name: event.name, // event_data: { // collectionTitle: collectionTitle, // priceFirstItem: priceOfFirstProductInCollection, // }, // }; // Example for sending event to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); gtag('event', 'view_item_list', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': 'category' }); }); analytics.subscribe('product_viewed', (event) => { // Example for accessing event data const productPrice = event.data.productVariant.price.amount; const productId = event.data.productVariant.id; // const productTitle = event.data.productVariant.title; // const payload = { // event_name: event.name, // event_data: { // productPrice: productPrice, // productTitle: productTitle, // }, // }; // // Example for sending event to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); gtag('event', 'view_item', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': 'product', 'ecomm_prodid': productId, 'ecomm_totalvalue': productPrice }) }); analytics.subscribe('cart_viewed', (event) => { // Example for accessing event data const totalCartCost = event.data.cart.cost.totalAmount.amount; // const firstCartLineItemName = // event.data.cart.lines[0]?.merchandise.product.title; // const payload = { // event_name: event.name, // event_data: { // cartCost: totalCartCost, // firstCartItemName: firstCartLineItemName, // }, // }; // Example for sending event to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); let productIds = []; if(event.data.cart && event.data.cart.lines.length >0){ event.data.cart.lines.forEach((item)=>{ productIds.push(item.merchandise.id); }) } gtag('event', 'view_cart', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': 'cart', 'ecomm_prodid':productIds, 'ecomm_totalvalue': totalCartCost }); }); analytics.subscribe('search_submitted', (event) => { // Example for accessing event data // const searchResult = event.data.searchResult; // const searchQuery = searchResult.query; // const firstProductReturnedFromSearch = // searchResult.productVariants[0]?.product.title; // const payload = { // event_name: event.name, // event_data: { // searchQuery: searchQuery, // firstProductTitle: firstProductReturnedFromSearch, // }, // }; // // Example for sending event to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); gtag('event', 'view_search_results', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': 'searchresults' }); }); var enhanced_conversion_data = {}; window.enhanced_conversion_data = enhanced_conversion_data; // 在这里回传用户详细信息给 gtag analytics.subscribe('checkout_completed', (event) => { // Example for accessing event data const checkout = event.data.checkout; const checkoutTotalPrice = checkout.totalPrice.amount; const currencyCode = checkout.currencyCode; const order = checkout.order; const lineItems = checkout.lineItems; const billingAddress = checkout.billingAddress; // const allDiscountCodes = checkout.discountApplications.map((discount) => { // if (discount.type === 'DISCOUNT_CODE') { // return discount.title; // } // }); // const firstItem = checkout.lineItems[0]; // const firstItemDiscountedValue = firstItem.discountAllocations[0]?.amount; // const customItemPayload = { // quantity: firstItem.quantity, // title: firstItem.title, // discount: firstItemDiscountedValue, // }; // const paymentTransactions = event.data.checkout.transactions.map((transaction) => { // return { // paymentGateway: transaction.gateway, // amount: transaction.amount, // }; // }); // const payload = { // event_name: event.name, // event_data: { // totalPrice: checkoutTotalPrice, // discountCodesUsed: allDiscountCodes, // firstItem: customItemPayload, // paymentTransactions: paymentTransactions, // }, // }; // Example for sending event data to third party servers // fetch('https://example.com/pixel', { // method: 'POST', // body: JSON.stringify(payload), // keepalive: true, // }); let productIds= []; lineItems.forEach((item)=>{ productIds.push(item.variant.id); }) gtag('event', 'conversion', { 'send_to': 'AW-11409537406/wulPCJmGwbIZEP7yvsAq', 'value': checkoutTotalPrice, 'currency': currencyCode, 'transaction_id': order.id }); gtag('event', 'purchase', { 'send_to': 'AW-11421913104', 'ecomm_pagetype': 'purchase', 'ecomm_prodid': productIds, 'ecomm_totalvalue': checkoutTotalPrice }); let street = ""; if(billingAddress.address1){ street += billingAddress.address1; } if(billingAddress.address2){ if(street){ street += " "; } street += billingAddress.address2; } enhanced_conversion_data = { 'email': checkout.email, 'phone_number': checkout.phone, "first_name": billingAddress.firstName, "last_name": billingAddress.lastName, "address": { "street": street, "city":billingAddress.city, "region": billingAddress.province, "postal_code": billingAddress.zip, "country": billingAddress.country } }; }); });})(self.webPixelsManager.createShopifyExtend('26083430', 'custom'));