Last active
July 5, 2020 14:49
-
-
Save ihorduchenko/5c1e7c6174ac0dc77765f7485bbf39a3 to your computer and use it in GitHub Desktop.
Woocommerce: Tag Manager - Thankyou page tracking code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (is_order_received_page()) { | |
$order_id = wc_get_order_id_by_order_key( $_GET['key'] ); | |
$order = wc_get_order( $order_id ); | |
$cookie_name = "order_data_sent"; | |
$cookie_value = $order_id; | |
$transaction_id = ($order->transaction_id ? $order->transaction_id : $order_id); | |
$total = $order->total; | |
$total_tax = $order->total_tax; | |
$shipping_method = @array_shift($order->get_shipping_methods()); | |
$shipping_method_id = $shipping_method['method_id']; ?> | |
<script> | |
var arr = [ | |
<?php foreach( $order->get_items() as $item_id => $item ){ ?> | |
<?php $product_id = $item->get_product_id(); | |
$product = $item->get_product(); | |
$product_qty = $item->get_quantity(); | |
$product_name = $item->get_name(); | |
$sku = $product->get_sku(); | |
$price = $product->get_price(); | |
$terms = get_the_terms( $product_id, 'product_cat' ); ?> | |
{ | |
'sku': '<?php echo $sku; ?>', | |
'name': '<?php echo $product_name; ?>', | |
'category': '<?php foreach ( $terms as $term ) { | |
$product_cat_title = $term->name; | |
echo $product_cat_title . ', '; | |
} ?>', | |
'price': '<?php echo $price; ?>', | |
'quantity': '<?php echo $product_qty; ?>' | |
}, | |
<?php } ?> | |
]; | |
// console.log(arr); | |
// console.log('<?php echo $shipping_method_id; ?>'); | |
</script> | |
<?php if( isset($_COOKIE[$cookie_name] ) && $_COOKIE[$cookie_name] == $order_id ) { ?> | |
<script>// console.log('cookie: <?php echo $_COOKIE[$cookie_name]; ?>');</script> | |
<?php } else { ?> | |
<script> | |
console.log('cookies already set'); | |
window.dataLayer = window.dataLayer || [] | |
dataLayer.push({ | |
'event': 'purchase', | |
'transactionId': '<?php echo $order_id; ?>', | |
'transactionAffiliation': 'inweb.ua', | |
'transactionTotal': '<?php echo $total; ?>', | |
'transactionTax': '<?php echo $total_tax; ?>', | |
'transactionShipping': '<?php echo $shipping_method_id; ?>', | |
'transactionProducts': [ | |
<?php foreach( $order->get_items() as $item_id => $item ){ ?> | |
<?php $product_id = $item->get_product_id(); | |
$product = $item->get_product(); | |
$product_qty = $item->get_quantity(); | |
$product_name = $item->get_name(); | |
$sku = $product->get_sku(); | |
$price = $product->get_price(); | |
$terms = get_the_terms( $product_id, 'product_cat' ); ?> | |
{ | |
'sku': '<?php echo $sku; ?>', | |
'name': '<?php echo $product_name; ?>', | |
'category': '<?php foreach ( $terms as $term ) { | |
$product_cat_title = $term->name; | |
echo $product_cat_title . ' '; | |
} ?>', | |
'price': '<?php echo $price; ?>', | |
'quantity': '<?php echo $product_qty; ?>' | |
}, | |
<?php } ?> | |
] | |
}); | |
</script> | |
<?php | |
setcookie($cookie_name, $cookie_value, time() + (86400 * 3000), "/"); | |
} ?> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment