cancel
Showing results for 
Search instead for 
Did you mean: 

Adding tracking code to success page

SOLVED

Adding tracking code to success page

I'm trying to add tracking code to my onepage success page. I've created layout and created success.phtml to my custom theme. app/design/frontend/my_theme/Magento_Checkout/layout/checkout_onepage_success.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <script src="module-checkout::templates/success.phtml"/>
</head>

app/design/frontend/my_theme/Magento_Checkout/templates/success.phtml

$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$totall = $order->getGrandTotal();
?>
<!-- Event snippet for website conversion conversion page -->
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-595825986/_BYQCLj7tr0DEMKqjpwC',
      'value': <?php echo $totall;?>,
      'currency': 'SAR',
      'transaction_id': ''
  });
</script>
<script>
  dataLayer.push({
    'event':'purchase',
    'order_value': <?php echo json_encode($order->getGrandTotal()) ?>,
    'order_id':<?php echo json_encode($block->getOrderId()) ?>,
    'enhanced_conversion_data': {
      "email": <?php echo json_encode($order->getCustomerEmail()) ?>,   
      "phone_number": <?php echo ($billingAddress->getTelephone() ? json_encode($billingAddress->getTelephone()) : '""'); ?>,
      "first_name": <?php echo json_encode($billingAddress->getFirstName()) ?>,
      "last_name": <?php echo json_encode($billingAddress->getLastName()) ?>,
      "street": <?php echo json_encode($billingAddress->getStreet()[0]) ?>,
      "city": <?php echo json_encode($billingAddress->getCity()) ?>,
      "region": <?php echo json_encode($billingAddress->getRegion()) ?>,
      "postal_code": <?php echo json_encode($billingAddress->getPostcode()) ?>,
      "country": <?php echo json_encode($billingCountry) ?>
    }
  })
</script>

Everything fine but the tracking code didn't appear when I click view page source in my browser !

Could you please help me.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Adding tracking code to success page

Hi @Bhanu Periwal Thank you for your assistance.

But I found the solution.

There is another success,phtml in Magento_InventoryInStorePickupFrontend.

And this is the file which I'm looking for regarding magento 2.4

View solution in original post

2 REPLIES 2

Re: Adding tracking code to success page

Hello @infonectar9054 

 

Kindly take reference from below code:
1. Modify the “app/design/frontend/default/my_custom_theme/layout/checkout.xml” file. Look for section

 <checkout_onepage_success>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="content">
            <block name="checkout.success" template="checkout/success.phtml" type="checkout/onepage_success" />
        </reference>
    </checkout_onepage_success>

And turn it into

 

 <checkout_onepage_success>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="content">
            <block name="checkout.success" template="checkout/success.phtml" type="checkout/onepage_success" />
        </reference>
        <reference name="before_body_end">
            <block name="google_adwords_tracking" template="checkout/google_adwords_tracking.phtml" type="core/template" />
        </reference>        
    </checkout_onepage_success>

2. Create “app/design/frontend/default/my_custom_theme/template/checkout/google_adwords_tracking.phtml” file and copy-paste the the Adwords tracking JavaScript code into it.

< ?php echo $this?>getChildHtml('before_body_end') ?>

It may help you!
thank you

 

Problem solved? Click Accept as Solution!

Re: Adding tracking code to success page

Hi @Bhanu Periwal Thank you for your assistance.

But I found the solution.

There is another success,phtml in Magento_InventoryInStorePickupFrontend.

And this is the file which I'm looking for regarding magento 2.4