我们目前使用GTM作为我们所有的跟踪代码。
设置Facebook动态广告和Facebook像素,我需要收集事件,如AddToCart,购买等。
<script>
fbq('track', 'Purchase', {
content_ids: ['1234', '4642', '35838'],
content_type: 'product'
value: 247.35,
currency: 'USD'
});
</script>
如何获取Magento的SKU和购物车值,并使用Google标记管理器传递给Facebook跟踪代码?
发布于 2017-05-30 20:41:05
最好的方法是将数据层变量和一个自定义html标记。组合起来,当用户单击“购买”按钮时,就会将一个事件推送给拥有传递给facebook所需信息的数据层。类似于datalayer.push({ event:'PURCHASE_BUTTON_CLICKED', PURCHASE_BUTTON_CLICKED: { ids:['1234', '4642', '35838'], type:'product, value: getTotalValue(), currency: 'USD' })
,GTM内部使用一个或几个(我推荐几个)不同的数据层变量,引用这个数据层对象的不同属性。您可以在自定义html标记中使用这些变量,方法是将它们包装成双大括号,如图像所示。剩下的就是在自定义html标记中添加一个触发器。
尽可能多地输入数据层和gtm,这样就可以重用所有这些变量来发送任意数量的标记。
发布于 2018-04-03 13:09:29
对于cart事件,可以使用以下代码片段获取信息
<?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 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
https://stackoverflow.com/questions/44151482
复制