View product
function push_product_view_event() {
global $product;
$product_data = array(
‘event’ => ‘product_view’,
‘ecommerce’ => array(
‘detail’ => array(
‘products’ => array(
array(
‘id’ => $product->get_id(),
‘name’ => $product->get_name(),
‘price’ => $product->get_price(),
)
)
)
)
);
dataLayer.push($product_data);
}
add_action(‘woocommerce_before_single_product’, ‘push_product_view_event’);
Checkout
add_action( ‘woocommerce_before_checkout_form’, ‘push_transaction_data_to_gtm_on_checkout’ );
function push_transaction_data_to_gtm_on_checkout() {
global $woocommerce;
// Get the cart object
$cart = $woocommerce->cart;
// Get the products in the cart
$products = array();
foreach ( $cart->get_cart() as $item ) {
$product = wc_get_product( $item[‘product_id’] );
$products[] = array(
‘name’ => $product->get_name(),
‘id’ => $product->get_id(),
‘price’ => $product->get_price(),
‘quantity’ => $item[‘quantity’],
);
}
// Get the cart total
$total = $cart->total;
// Get the tax total
$tax = $cart->tax_total;
// Get the shipping total
$shipping = $cart->shipping_total;
// Get the currency
$currency = get_woocommerce_currency();
?>
<script>
dataLayer.push({
‘event’: ‘transactionData’,
‘ecommerce’: {
‘checkout’: {
‘actionField’: {
‘revenue’: ‘<?php echo $total; ?>’,
‘tax’: ‘<?php echo $tax; ?>’,
‘shipping’: ‘<?php echo $shipping; ?>’,
‘currency’: ‘<?php echo $currency; ?>’
},
‘products’: <?php echo json_encode( $products ); ?>
}
}
});
</script>
<?php
}
Purchase
add_action( ‘woocommerce_thankyou’, ‘push_transaction_data_to_gtm_on_thankyou’, 10, 1 );
function push_transaction_data_to_gtm_on_thankyou( $order_id ) {
// Get the order object
$order = wc_get_order( $order_id );
// Get the products in the order
$products = array();
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
$products[] = array(
‘name’ => $product->get_name(),
‘id’ => $product->get_id(),
‘price’ => $product->get_price(),
‘quantity’ => $item->get_quantity(),
);
}
// Get the order total
$total = $order->get_total();
// Get the tax total
$tax = $order->get_total_tax();
// Get the shipping total
$shipping = $order->get_shipping_total();
// Get the order number
$order_number = $order->get_order_number();
// Get the currency
$currency = $order->get_currency();
// Push the transaction data to the data layer
?>
<script>
dataLayer.push({
‘event’: ‘transactionData’,
‘ecommerce’: {
‘purchase’: {
‘actionField’: {
‘id’: ‘<?php echo $order_number; ?>’,
‘revenue’: ‘<?php echo $total; ?>’,
‘tax’: ‘<?php echo $tax; ?>’,
‘shipping’: ‘<?php echo $shipping; ?>’,
‘currency’: ‘<?php echo $currency; ?>’
},
‘products’: <?php echo json_encode( $products ); ?>
}
}
});
</script>
<?php
}
View Category
function push_categoryview_event() {
if (is_product_category()) {
$term = get_queried_object();
$term_name = $term->name;
?>
<script>
dataLayer.push({
‘event’: ‘categoryview’,
‘category’: ‘<?php echo $term_name; ?>’
});
</script>
<?php
}
}
add_action(‘wp_footer’, ‘push_categoryview_event’);
Add to cart
function add_to_cart_data_layer_push($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$product = wc_get_product( $product_id );
?>
<script>
dataLayer.push({
‘event’: ‘addToCart’,
‘ecommerce’: {
‘add’: {
‘products’: [{
‘name’: ‘<?php echo $product->get_name(); ?>’,
‘id’: ‘<?php echo $product->get_id(); ?>’,
‘price’: ‘<?php echo $product->get_price(); ?>’,
‘quantity’: ‘<?php echo $quantity; ?>’
}]
}
}
});
</script>
<?php
}
add_action( ‘woocommerce_add_to_cart’, ‘add_to_cart_data_layer_push’, 10, 6 );