Google Tag Manager code for WooCommerce order completion page

If you are looking for Google Tag Manager code for WooCommerce order complete, without any heavy plugin structure.

The code snippet is a self-explained. Code using standard WooCommerce thank you page filter. The script will process on order complete and user redirect on WooCommerce thank you page.

Here a code snippet for you. Place this code in your active theme functions.php file.


/**
* Adding custom javascripts or php action on WooCommerce Thank you page
* Works with WooCommerce 3.0 or above
*/
add_action( "woocommerce_thankyou", "replaceme_thank_you_script", 20 );
if ( ! function_exists( 'replaceme_thank_you_script' ) ) {
function replaceme_thank_you_script( $order_id ) {
if ( $order_id > 0 ) {
$order = wc_get_order( $order_id );
if ( $order instanceof WC_Order ) {
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
$coupon_codes = '';
foreach( $order->get_used_coupons() as $coupon) {
$coupon_codes .= $coupon;
if( $i < $coupons_count )
$coupon_codes .= ' ';
$i++;
}
}
$order_id = $order->get_id(); // order id
$order_key = $order->get_order_key(); // order key
$order_total = $order->get_total(); // order total
$order_sub_total = $order->get_subtotal();
$order_currency = $order->get_currency(); // order currency
$order_payment_method = $order->get_payment_method(); // order payment method
$order_shipping_country = $order->get_shipping_country(); // order shipping country
$order_billing_country = $order->get_billing_country(); // order billing country
$order_status = $order->get_status(); // order status
$order_discount_total = $order->get_discount_total();
/**
* full list methods and property that can be accessed from $order object
* https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
*/
?>

<script type="text/javascript">
window.dataLayer = window.dataLayer || []
dataLayer.push({
'transactionId': '<?php echo $order_id ?>',
'transactionCurrency': '<?php echo $order_currency ?>',
'transactionAffiliation': 'Order Confirm',
'transactionTotal': <?php echo $order_total ?>,
'transactionSubTotal': <?php echo number_format($order_sub_total, 2, ".", "") ?>,
'transactionTax': <?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>,
'transactionShipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>,
'transactionDiscount': <?php echo number_format($order_discount_total, 2, ".", "") ?>,
'transactionPromoCode': '<?php echo rtrim($coupon_codes) ?>',
'transactionProducts': [
<?php
foreach($order->get_items() as $key => $item):
$product = $order->get_product_from_item( $item );
$variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : '';
?>
{
'name': '<?php echo $item['name']; ?>',
'sku': '<?php echo $product->get_sku(); ?>',
'id': '<?php echo $item['product_id']; ?>',
'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',

'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
'variant': '<?php echo ($variant_name) ? implode("-", $variant_name->get_variation_attributes()) : ''; ?>',
'quantity': <?php echo $item['qty']; ?>
},
<?php endforeach; ?>
]
});
</script>
<?php
}
}
}
}