How to Add Conversion Tracking Code to Woocommerce Thank You Page

To add Conversion Tracking Code to Woocommerce Thank You Page (Google Ads in this example), and dynamically insert order values (eg amount, currency, order_id), use the following code in your functions.php (of your child theme):

function custom_conversion_tracking( $order_id ) {

	$order = wc_get_order( $order_id ); ?>

    <script>
      gtag("event", "conversion", {
          "send_to": "Tracking-code-here-XXXXXXXXXX",
          "value": <?php echo $order->get_total(); ?>,
          "currency": "<?php echo $order->get_currency(); ?>",
          "transaction_id": <?php echo $order_id; ?>
      });
    </script>
    <?php
 }

add_action( 'woocommerce_thankyou', 'custom_conversion_tracking' );

If you want to add the code inside HEAD, use:

function custom_conversion_tracking(){
    // On order-received endpoint
    if( is_wc_endpoint_url( 'order-received' ) ) :

    // Get order ID
    $order_id = absint( get_query_var('order-received') );

    if( get_post_type( $order_id ) !== 'shop_order' ) return;

    $order = wc_get_order( $order_id );
    ?>
    <script>
      gtag("event", "conversion", {
          "send_to": "Tracking-code-here-XXXXXXXXXX",
          "value": <?php echo $order->get_total(); ?>,
          "currency": "<?php echo $order->get_currency(); ?>",
          "transaction_id": <?php echo $order_id; ?>
      });
    </script>
    <?php
    endif;
}

add_action( 'wp_head', 'custom_conversion_tracking' );
Your comments are welcome!

Are you a website owner?

Enhance your website with custom features, optimizations, and seamless e-commerce integration. Let's discuss your project and achieve your online business goals.

Christos Pontikis