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' );
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.