Magento – How to add “Initiate checkout” Facebook pixel Standard events on Magento Proceed to checkout

ecommerce-trackingfacebookmagento-1.7onepage-checkout

Magento Version 1.7

I want to add the Facebook pixel InitiateCheckout standard events on my website Proceed To Checkout button click.

I tried to add a JavaScript function on link.phtml(\app\design\frontend\default\default\template\checkout\onepage) and remove the onclick="window.location='<?php echo $this->getCheckoutUrl() ?>';"> and call this window location in the function track_fbq()

<button type="button" title="<?php echo $this->__('Proceed to Checkout') ?>" class="button btn-proceed-checkout btn-checkout<?php if ($this->isDisabled()):?> no-checkout<?php endif; ?>"<?php if ($this->isDisabled()):?> disabled="disabled"<?php endif; ?> onclick="track_fbq()"><span><span><?php echo $this->__('Proceed to Checkout') ?></span></span></button>

My JavaScript function is below.

<script>
  function track_fbq(){

    window.location="<?php echo $this->getCheckoutUrl() ?>";
    fbq('track', 'InitiateCheckout');
  }
</script>
<noscript>
<img height="1" width="1" style="display:none"
           src="https://www.facebook.com/tr?id=1677576565828693&ev=InitiateCheckout&value=<?php echo Value ?>&currency=<?php echo $currency ?>&noscript=1"
    />
</noscript>

My checkout is redirect to the checkout page and i can complete the purchase.

My question is, Is my approach is correct? If my method is not correct can you please help me to do this task.This is not a question that ask about Facebook tracking.
Thank you.

Best Answer

You need to add code to app/design/frontend/Your-Package/Your-Theme/template/page/html/head.phtml

<?php if(Mage::getURL('checkout/onepage') == Mage::helper('core/url')->getCurrentUrl()){ ?>

<script>
    fbq('track', 'InitiateCheckout');
</script>
<noscript>
<img height="1" width="1" style="display:none"
           src="https://www.facebook.com/tr?id=1677576565828693&ev=InitiateCheckout&value=<?php echo Value ?>&currency=<?php echo $currency ?>&noscript=1"
    />
</noscript>

<?php } ?>

Hope it will helpful to you.

Related Topic