Add conversion tracking code in thank you page Woocommerce
Hi Guys
Today we are going to learn how to add conversion tracking code in order thank you page.
We will do this using custom snippet in function.php
/**
* @snippet Conversion tracking code in thank you head Woocommerce
* @author Deep Prakash Goyal
* @website https://wpexpertdeep.com/
*/
add_action( 'wp_head', 'wpexpertdeep_google_conversion_thankyou' );
function wpexpertdeep_google_conversion_thankyou(){
// On Order received endpoint only
if( is_wc_endpoint_url( 'order-received' ) ) :
$order_id = absint( get_query_var('order-received') ); // Get order ID
if( get_post_type( $order_id ) !== 'shop_order' ) return; // Exit
$order = wc_get_order( $order_id ); // Get the WC_Order Object instance
?>
<!-- Event snippet for Purchase conversion page -->
<script>
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXXXXXXXXXXXXX', // Enter your tracking code here
'value': <?php echo $order->get_total(); ?>,
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
<?php
endif;
}