Facebook 更新了他们的像素,所以我创建了自己的自定义函数来调用,它将动态地将参数放在一起以提交给 Facebook。
第 1 步。在每个页面上,确保已在页面的head元素中初始化像素。
第 2 步。添加我创建的这个自定义函数(它有点冗长,因为它是初稿,所以我确信有一些方法可以优化它以使您受益)。
triggerFacebookPixel: function(type, price, product_id, product_name, product_category, num_items) {
//See https://developers.facebook.com/docs/ads-for-websites/pixel-events/v2.8#events for documentation
//type = "ViewContent"|"AddToCart"|"Search"|"AddToWishlist"|"InitiateCheckout"|"AddPaymentInfo"|"Purchase"|"Lead"|"CompleteRegistration"
//product_id = Numeric Product ID. String or Integer accepted for single product events, or an array of integers for multiple products.
//price = Decimal/Float number of individual product's price or total price paid in conversion, or the user's status for 'CompleteRegistration'
//product_name = Optional. String of individual product's name or string of search query
//product_category = Optional. String of product category, hierarchy's accepted. E.g. "Clothing > Shirts > Men's > T-Shirts"
//num_items = Optional. Total number of products.
var data = {
value: typeof(price) == 'string' ? parseFloat(price) : price,
currency: 'USD'
}
switch (type) {
case 'Search':
data.content_ids = product_id;
data.search_string = product_name;
if (product_category !== undefined && product_category != '') {
data.content_category = product_category;
}
break;
case 'Lead':
data.content_name = product_name;
data.content_category = product_category;
break;
case 'CompleteRegistration':
data.status = product_id;
data.content_name = product_name;
break;
default:
//Product Specific Calls
//ViewContent|AddToCart|AddToWishlist|InitiateCheckout|AddPaymentInfo|Purchase
if (num_items == 1) {
data.content_ids = [typeof(product_id) == 'string' ? parseInt(product_id) : product_id];
data.content_category = product_category;
data.content_name = product_name;
} else {
data.content_ids = product_id;
}
//"num_items" is only a parameter used in these two types
if (type == 'InitiateCheckout' || type == 'Purchase') {
data.num_items = num_items
}
//"content_type" is only a parameter used in these three types
if (type == 'Purchase' || type == 'AddToCart' || type == 'ViewContent') {
data.content_type = 'product';
}
break;
}
fbq('track', type, data);
}
步骤 3. 使用适当的参数调用该函数。
对于购买后的感谢弹出窗口,如果用户购买 1 件商品而不是多件商品,则触发像素的方式不同。基本上,如果它只是一个产品,Facebook 接受产品名称和类别的参数,但如果它是多个产品,则不接受这些参数。
对于以下示例,以下是用户购买 1 项的一些示例产品数据:
- 产品名称:“我的超棒T恤”
- 产品编号:182
- 产品分类:“服装 > 衬衫 > T恤”
- 用户支付的总金额(包括运费/手续费和税费):10.84 美元
这是您在确认时调用的函数:
triggerFacebookPixel('Purchase', 10.84, 182, 'My Super Awesome T-Shirt', 'Clothing > Shirts > T-Shirts', 1);
当用户购买多件商品时,像素会以不同方式处理它,不包括产品名称和类别,只发送他们的 ID。所以让我们假设我们的用户购买了这两个项目:
- 产品 ID:182 和 164(衬衫和其他东西)
- 用户支付的总金额(包括运费/手续费和税费):24.75 美元
这是您使用该功能的方式:
triggerFacebookPixel('Purchase', 24.75, [182, 164], '', '', 2);
对于Facebook 定义的关于产品的其他标准事件,您可以对“ViewContent”、“AddToCart”、“AddToWishlist”、“InitiateCheckout”和“AddPaymentInfo”使用相同的函数。只需将“购买”更改为您通话中的任何关键词即可。
其他事件不一定与产品相关:“Lead”、“Search”和“Complete Registration”。你仍然可以使用这个功能来处理这些关键词。
示例:用户搜索“蓝色衬衫”:
triggerFacebookPixel('Search', 0, [], 'blue shirts');
如果要在用户搜索功能中传递产品类别,可以在末尾将其作为字符串传递。我想不出你会知道用户正在搜索什么类别的用例场景。除非您在产品出现在搜索结果中并且用户从搜索页面单击它的情况下使用此功能。这可能是这个函数的实际用途,但文档对此并不十分清楚。如果您是这种情况,那么只需将 0 和空数组更改为从搜索结果页面单击的产品的实际值(分别为价格和产品 ID),并将其类别作为字符串添加为最后一个搜索字符串后的参数。
示例:用户提交了一份表格,将他们注册到您的时事通讯:
triggerFacebookPixel('CompleteRegistration', 0, 'Signed Up', 'Newsletter');
Facebook 的文档指出,填写注册表单时应使用“CompleteRegistration”,例如完成订阅/注册服务。“Signed Up”字符串是“status”参数,“Newsletter”字符串是“content_name”参数。
示例:用户提交了一份表格,为他们注册了您提供的某些服务的 30 天免费试用(因此他们现在是潜在客户),其中服务的名称是“免费 30 天试用服务”,它位于“我的服务”类别下的“免费试用”子类别:
triggerFacebookPixel('Lead', 0, 'Free 30-Day Trial Service', 'My Services > Free Trials');
Facebook 的文档指出,“Lead”是指完成注册,例如点击定价、注册试用。我假设服务的名称是参数“content_name”,服务的类别是“content_category”参数。