Facebook 动态广告像素(AddToCart、购买)- Magento 和 Google 标签管理器

Facebook Dynamic ads pixel (AddToCart, Purchase) - Magento & Google Tag Manager

我们目前对所有跟踪代码都使用 GTM。

要设置 Facebook 动态广告和 Facebook 像素,我需要收集 AddToCart、Purchase 等事件。

<script>
fbq('track', 'Purchase', {
content_ids: ['1234', '4642', '35838'],
content_type: 'product'
value: 247.35,
currency: 'USD'
});
</script>

如何使用 Google 标签管理器获取 Magento 的 SKU 和购物车价值并传递给 Facebook 跟踪代码?

解决此问题的最佳方法是将数据层变量与一个 custom html tag. 当用户点击购买按钮时,您将一个事件推送到数据层,该数据层包含您需要传递给 facebook 的信息。就像是 datalayer.push({ event:'PURCHASE_BUTTON_CLICKED', PURCHASE_BUTTON_CLICKED: { ids:['1234', '4642', '35838'], type:'product, value: getTotalValue(), currency: 'USD' }) 然后在 GTM 内部使用一个或几个(我推荐几个)不同的数据层变量引用这个数据层对象的不同属性。您可以在自定义 html 标记中使用这些变量,方法是将它们用双花括号括起来,如图所示。剩下的就是向自定义 html 标记添加触发器。

尝试尽可能多地向数据层和 gtm 提供数据,以便您可以重复使用所有这些变量来发送任意数量的标签。

对于购物车事件,您可以使用以下代码片段获取信息

<?php $quote = Mage::getSingleton('checkout/cart')->getQuote();
$productIds = "";
foreach($quote->getAllItems() as $item):
    if($item->getParentItemId()) continue;
    if (strlen($productIds)==0){
        $productIds = "'".$item->getSku()."'";
    }
    else{
        $productIds = $productIds.",'".$item->getSku()."'";
    }
endforeach;
$pixelTotal = $quote->getBaseGrandTotal();
$pixelCurrency = $quote->getQuoteCurrencyCode();?>

<script>
fbq('track', 'Cart', {
   content_ids: <?php echo $productIds;?>,
   content_type: 'product'
   value: <?php echo $pixelTotal;?>,
   currency: <?php echo $pixelCurrency;?>
});
</script>

对于购买事件,您可以使用以下代码段获取信息

<?php $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
$productIds = "";
foreach($order->getAllItems() as $item):
    if($item->getParentItemId()) continue;
    if (strlen($productIds)==0){
        $productIds = "'".$item->getSku()."'";
    }
    else{
        $productIds = $productIds.",'".$item->getSku()."'";
    }
endforeach;
$pixelTotal = $order->getBaseGrandTotal();
$pixelCurrency = $order->getOrderCurrencyCode();?>

<script>
fbq('track', 'Purchase', {
   content_ids: <?php echo $productIds;?>,
   content_type: 'product'
   value: <?php echo $pixelTotal;?>,
   currency: <?php echo $pixelCurrency;?>
});
</script>

对于联系我们事件,使用下面的观察者事件controller_action_postdispatch_contacts_index_post

Config.xml

<controller_action_postdispatch_contacts_index_post>
                <observers>
                    <custom_module_contact_submit_after>
                        <type>singleton</type>
                        <class>custom_module/observer</class>
                        <method>ContactPost</method>
                    </custom_module_contact_submit_after>
                </observers>
</controller_action_postdispatch_contacts_index_post>

Observer.php

/**
  * Triggers on contact us form    
  * @return void|Varien_Event_Observer   
  */
  public function ContactPost() {
     Mage::getSingleton('core/session')->setContactPost('1');
  }

header.phtml

if (Mage::getSingleton('core/session')->getContactPost()==1){
    fbq('track', 'Lead');
}

对于完成注册事件,使用下面的观察者事件customer_register_success

Config.xml

<customer_register_success>
                <observers>
                    <custom_module_customer_register_success>
                        <type>singleton</type>
                        <class>custom_module/observer</class>
                        <method>CustomerRegister</method>
                    </custom_module_customer_register_success>
                </observers>
</customer_register_success>

Observer.php

/**
  * Triggers on contact us form    
  * @return void|Varien_Event_Observer   
  */
  public function CustomerRegister() {
     Mage::getSingleton('core/session')->setRegistered('1');
  }

header.phtml

if (Mage::getSingleton('core/session')->getRegistered()=="1"){
    fbq('track', 'CompleteRegistration');
}

对于 结帐事件(CheckoutInitiate 和 PaymentInfo),您可以在 onepage.pthml

上添加它们

CheckoutInitiate 可以在页面加载时完成,PaymentInfo 可以在 Payment.prototype.save[ 上触发=64=]函数

如果您是 Magento 开发人员,那么可以轻松实现上述事件,但如果不是,那么我建议使用以下第三方模块 -:

对于 Magento 1

https://www.scommerce-mage.com/magento-google-tag-manager-enhanced-ecommerce-tracking.html

对于 Magento 2

https://www.scommerce-mage.com/magento-2-google-tag-manager-enhanced-ecommerce-tracking.html