当前位置:迷你笔记 » 技术 » Facebook Pixel代码集成到Shopify Customer Events里

Facebook Pixel代码集成到Shopify Customer Events里

// Step 1: Initialize the Facebook Pixel SDK (exclude HTML)
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', 'YOUR_PIXEL_ID_HERE');
fbq('track', 'PageView');

// Step 2: Subscribe to customer events and track them with Facebook Pixel
analytics.subscribe("product_viewed", event => {
fbq('track', 'ViewContent', {
content_ids: [event.data.productVariant.product.id],
content_name: event.data.productVariant.product.title,
content_type: 'product_group',
content_category: event.data.productVariant.product.type,
value: event.data.productVariant.price,
currency: event.data.productVariant.currency
});
});

analytics.subscribe("product_added_to_cart", event => {
fbq('track', 'AddToCart', {
content_ids: [event.data.cartLine.merchandise.id],
content_name: event.data.cartLine.merchandise.title,
content_type: 'product',
value: event.data.cartLine.cost.totalAmount,
currency: event.data.cartLine.cost.currencyCode,
quantity: event.data.cartLine.quantity
});
});

analytics.subscribe("checkout_started", event => {
fbq('track', 'InitiateCheckout', {
value: event.data.checkout.totalPrice,
currency: event.data.checkout.totalPrice.currencyCode
});
});

analytics.subscribe("checkout_completed", event => {
const lineItems = event.data.checkout.lineItems.map(item => ({
id: item.variant.id,
name: item.title,
price: item.variant.price,
quantity: item.quantity
}));

fbq('track', 'Purchase', {
content_ids: lineItems.map(item => item.id),
content_type: 'product',
value: event.data.checkout.totalPrice,
currency: event.data.checkout.totalPrice.currencyCode,
num_items: lineItems.length,
contents: lineItems
});
});
未经允许不得转载:迷你笔记 » Facebook Pixel代码集成到Shopify Customer Events里

相关文章

评论 (0)

5 + 4 =