简体   繁体   English

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

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

We currently using GTM for all our tracking codes. 我们目前将GTM用于所有跟踪代码。

To setup Facebook Dynamic ads and Facebook pixel, I need to collect events like AddToCart, Purchase etc. 要设置Facebook动态广告和Facebook像素,我需要收集AddToCart,Purchase等事件。

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

How to get Magento's SKUs and Cart value and pass to Facebook tracking code using Google Tag Manager? 如何使用Google跟踪代码管理器获取Magento的SKU和购物车价值并将其传递给Facebook跟踪代码?

For cart event , you can get the information using the following snippet code 对于购物车事件 ,您可以使用以下代码段获取信息

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

For purchase event , you can get the information using the following snippet code 对于购买活动 ,您可以使用以下代码段获取信息

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

For Contact us event, use the following observer event controller_action_postdispatch_contacts_index_post 对于“ 与我们联系”事件,请使用以下观察者事件controller_action_postdispatch_contacts_index_post

Config.xml 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 Observer.php

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

header.phtml header.phtml

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

For complete registration event, use the following observer event customer_register_success 对于完整的注册事件,请使用以下观察者事件customer_register_success

Config.xml 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 Observer.php

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

header.phtml header.phtml

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

For Checkout events (CheckoutInitiate and PaymentInfo) , you can add them on onepage.pthml 对于Checkout事件(CheckoutInitiate和PaymentInfo) ,可以将它们添加到onepage.pthml

CheckoutInitiate can be done on the page load and PaymentInfo can be triggered on Payment.prototype.save function CheckoutInitiate可以在页面加载上完成,而PaymentInfo可以在Payment.prototype.save函数上触发

If you are a Magento developer then above events can easily be implemented but if not then I would recommend to use the following third party modules -: 如果您是Magento开发人员,那么可以很容易地实现上述事件,但是如果没有,那么我建议使用以下第三方模块-:

For Magento 1 对于Magento 1

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

For Magento 2 对于Magento 2

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

The best way to go about this is a combination of datalayer variables and one custom html tag. 最好的方法是将数据层变量和一个自定义html标记结合在一起 when the user clicks on the purchase button you push an event to the datalayer that has the information you need to pass along to facebook. 当用户单击购买按钮时,您会将事件推送到数据层,该事件具有您需要传递给Facebook的信息。 something like datalayer.push({ event:'PURCHASE_BUTTON_CLICKED', PURCHASE_BUTTON_CLICKED: { ids:['1234', '4642', '35838'], type:'product, value: getTotalValue(), currency: 'USD' }) then inside GTM use one or several (i recommend several) different datalayer variables referencing different attributes of this datalayer object. 类似于datalayer.push({ event:'PURCHASE_BUTTON_CLICKED', PURCHASE_BUTTON_CLICKED: { ids:['1234', '4642', '35838'], type:'product, value: getTotalValue(), currency: 'USD' })然后在GTM内部使用一个或几个(我建议几个)不同的数据层变量来引用此数据层对象的不同属性。 you can use these variables in your custom html tag by wrapping them in double curly braces as shown in the image. 您可以通过将其包装在双大括号中来在自定义html标记中使用这些变量,如图所示。 all that is left is to add a trigger to the custom html tag. 剩下的就是向自定义html标签添加触发器。

try and feed as much as you can into the datalayer and gtm so that you can reuse all these variables to send off any number of tags. 尝试将尽可能多的数据馈入数据层和gtm,以便可以重用所有这些变量来发送任意数量的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM