1

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.

  • This will help you stackoverflow.com/questions/23244945/… – Prashant Valanda Mar 14 '16 at 9:26
  • Thank you @PrashantValanda.I want to understand that called the checkout url in the javascript file is correct or not? – Mujahidh Mar 14 '16 at 10:18
  • You can add this code to head.phtml file. – Prashant Valanda Mar 14 '16 at 10:32
  • I want to pass the value to fbq('track', 'InitiateCheckout');.How can i add the value then? – Mujahidh Mar 14 '16 at 10:42
1

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.

  • how to get the value of the order here? – Mujahidh Mar 14 '16 at 11:01
  • Do you mean cart grand total? – Prashant Valanda Mar 14 '16 at 11:08
  • yes the grand total of the order or else simply the order details?In the link.phtml i can get the value using $checkoutDataArr = Mage::getSingleton('checkout/cart')->getQuote()->getData(); – Mujahidh Mar 14 '16 at 11:09
  • Yes using above code you get all detail of cart. Here is for grandtotal $total = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); – Prashant Valanda Mar 14 '16 at 11:11
  • Can use the above code in head.phtml rather than link.phtml.I only want to execute the facebook tracking code each and every time customer click the proceed to checkout button. – Mujahidh Mar 14 '16 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.