Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created April 24, 2020 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/f9b668c62403c91da31b9f3f7f2e03ef to your computer and use it in GitHub Desktop.
Save devinsays/f9b668c62403c91da31b9f3f7f2e03ef to your computer and use it in GitHub Desktop.
Tracking Copy
<?php
/**
* Add GTM ecommerce tracking to the Order Complete (Thank You) page.
*
* @param int $order_id
*/
public function thank_you_tracking($order_id)
{
$user_id = get_current_user_id();
$is_existing_customer = $user_id ? get_user_meta($user_id, '_existing_customer', true) : false;
// Grab the order
$order = wc_get_order($order_id);
$order_data = $order->get_data();
// Subscription purchase
if (wcs_order_contains_subscription($order) || wcs_order_contains_renewal($order)) {
foreach ($order->get_items() as $item) {
$item_data = $item->get_data();
$product_id = $item_data['product_id'];
break;
}
if ('gift' === UniversalYums_Frontend::get_product_cat_from_id($product_id)) {
$product_id = $item_data['variation_id'];
$length = get_post_meta($product_id, '_subscription_length', true);
$variant = $length . '-Month';
} else {
$variant = 'Annual';
if (get_post_meta($product_id, '_subscription_sign_up_fee', true)) {
$variant = 'Monthly';
}
}
$product = wc_get_product($product_id);
$sku = $product->get_sku();
$lifetime_value = $this->get_lifetime_value($sku);
// If a user changes their subscription plan
if (function_exists('wcs_order_contains_switch') && wcs_order_contains_switch($order) && isset($item_data)) {
?>
<script>
dataLayer.push({
'eventCategory': 'Subscription Plan',
'eventAction': 'Switch Plan',
'eventLabel': "<?php echo $item_data['name']; ?>",
'eventInteration': true,
'event': 'gaEvent'
});
</script>
<?php
// If a user manually renews their payment, probably for a failed auto payment
} elseif (function_exists('wcs_order_contains_renewal') && wcs_order_contains_renewal($order) && isset($item_data)) {
?>
<script>
dataLayer.push({
'eventCategory': 'Subscription Plan',
'eventAction': 'Manual Renewal for Failed Payment',
'eventLabel': "<?php echo $item_data['name']; ?>",
'eventInteration': true,
'event': 'gaEvent'
});
<?php if ('yes' !== get_post_meta($order_id, '_uy_gift_renewal_order', true)) { ?>
alert('This was a manual renewal for a previously failed payment');
<?php } ?>
</script>
<?php
// Otherwise, it must be a new order
} else {
?>
<script>
try {
if (localStorage.getItem('lastOrderId') != <?php echo $order_data['id']; ?>) {
localStorage.setItem('lastOrderId', <?php echo $order_data['id']; ?>);
dataLayer.push({
'ecommerce': {
'purchase': {
'actionField': {
'id': '<?php echo $order_data['id']; ?>',
'affiliation': 'Online Store',
'revenue': '<?php echo $order_data['total']; ?>',
'tax': '<?php echo $order_data['total_tax']; ?>',
'shipping': '<?php echo $order_data['shipping_total']; ?>',
'coupon': '',
'customer': '<?php echo $is_existing_customer ? 'Existing Customer' : 'New Customer'; ?>' ,
'content_ids': '<?php echo $sku ?>',
<?php echo $lifetime_value ? "'pltv':\"$lifetime_value\"" : ''; ?>
},
'products': [{
'name': "<?php echo $item_data['name']; ?>",
'id': '<?php echo get_post_meta($product_id, '_sku', true); ?>',
'price': '<?php echo $item_data['subtotal']; ?>',
'brand': 'Universal Yums',
'category': 'Boxes',
'variant': '<?php echo $variant; ?>',
'quantity': '<?php echo $item_data['quantity']; ?>',
'coupon': ''
}]
}
},
'event': 'orderConfirmation',
});
dataLayer.push({
'eventCategory': 'Checkout',
'eventAction': 'Purchase',
'eventLabel': '<?php echo $is_existing_customer ? 'Existing Customer' : 'New Customer'; ?>',
'eventInteration': true,
'event': 'gaEvent'
});
}
} catch(err) { }
</script>
<?php
}
} else {
// Single sale purchase
$data = [];
foreach ($order->get_items() as $item) {
$product = $item->get_product();
// Validate if single sale product.
if (WC_Subscriptions_Product::is_subscription($product)) {
continue;
}
$data[] = [
'sku' => $product->get_sku(),
'name' => $product->get_title(),
'price' => $item->get_total(),
'qty' => $item->get_quantity(),
];
}
$skus = implode(',', array_map(function ($el) {
return $el['sku'];
}, $data)); ?>
<script>
try {
if ( localStorage.getItem('lastOrderId') != <?php echo $order_data['id']; ?> ) {
localStorage.setItem('lastOrderId', <?php echo $order_data['id']; ?>);
dataLayer.push({
'ecommerce': {
'purchase': {
'actionField': {
'id': '<?php echo $order_data['id']; ?>',
'affiliation': 'Online Store',
'revenue': '<?php echo $order_data['total']; ?>',
'tax': '<?php echo $order_data['total_tax']; ?>',
'shipping': '<?php echo $order_data['shipping_total']; ?>',
'coupon': '',
'customer': '<?php echo $is_existing_customer ? 'Existing Customer' : 'New Customer'; ?>' ,
'content_ids': '<?php echo $skus ?>',
'pltv': '<?php echo $order_data['total'] ?>',
},
'products': [
<?php foreach ($data as $order_product): ?>
{
'name': "<?php echo $order_product['name']; ?>",
'id': '<?php echo $order_product['sku']; ?>',
'price': '<?php echo $order_product['price']; ?>',
'brand': 'Universal Yums',
'category': 'Single Sale',
'quantity': '<?php echo $order_product['qty']; ?>'
},
<?php endforeach; ?>
]
}
},
'event': 'orderConfirmation'
});
dataLayer.push({
'eventCategory': 'Checkout',
'eventAction': 'Purchase',
'eventLabel': '<?php echo $is_existing_customer ? 'Existing Customer' : 'New Customer'; ?>',
'eventInteration': true,
'event': 'gaEvent'
});
}
} catch(err) {}
</script>
<?php
}
if ($user_id) {
update_user_meta($user_id, '_existing_customer', true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment