Facebook动态广告像素(AddToCart,购买) - Magento& Google代码管理器

时间:2017-05-24 07:16:07

标签: facebook 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跟踪代码?

2 个答案:

答案 0 :(得分:1)

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

<?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 函数

上触发

如果您是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

答案 1 :(得分:0)

最好的解决方法是数据层变量和一个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中,以便您可以重复使用所有这些变量来发送任意数量的标记。

相关问题