Setup Enhanced Ecommerce With Google Tag Manager

Setting up dataLayers and Tag settings for ecommerce tracking in Google Analytics is a complicated topic. In this article we go over the process of integrating transaction tracking.

1. Installing a Data Layer On Your Checkout Page

2. Setup the Data Layer That Stores the Purchasing Info

The Ecommerce dataLayer is a critical component of our GTM-enabled Enhanced Ecommerce Transaction Tracking. We’ll go over how to construct a dataLayer Specification for your developer in the second edition of our series, and I’ll show you how a developer would install it. You’ll be able to pull the data into GTM for further processing after the dataLayer is installed.

Data Layer Code For WordPress

<script>
var dataLayer  = window.dataLayer || [];
dataLayer.push({
  'event': 'transaction',
  'ecommerce': {
    'purchase': {
      'actionField': {
        'id': '<?php echo $order->get_order_number(); ?>',        // Transaction ID. Required           
        'affiliation': 'Online Store',
        'revenue': '<?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>',   // Total transaction value (incl. tax and shipping)
        'tax':'<?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>',
        'shipping': '<?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>',
		<?php if($order->get_used_coupons()): ?> 
        'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>'
		<?php endif; ?>
      },
	  'products': [
		  <?php 
		   foreach($order->get_items() as $key => $item):
		   	$product = $order->get_product_from_item( $item );
			$varient_name = ($item[ 'variation_id' ]) ? wc_get_product($item[ 'variation_id' ]) : '';
		  ?>  
		{          // List of productFieldObjects.
        'name': '<?php echo $item['name']; ?>',  // Name or ID is required.
        'id': '<?php echo $item['product_id']; ?>',
        'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
        'brand': 'N/A',
        'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
		'variant': '<?php echo ($variant_name) ? implode ("-", $varient_name->get_variation_attributes()) : ''; ?>',
        'quantity': <?php echo $item['qty']; ?>
        },
		<?php endforeach; ?>
	   ] //expand this array if more product exists
    }
  }
});

</script>

3. Take Data Layer info and send it to Google Analytics

We’ve acquired all of the necessary data and are ready to use it in tools like Google Analytics, Facebook Pixel, and Google Ads Conversion Tracking Code. How can we extract the data from the dataLayer into these tags, though? With the help of Google Tag Manager! We’ll go over how to set up all of the triggers, variables, and tags in GTM to properly transmit our data to these tools and track our conversions in this final video of our eCommerce tracking series.

Leave a Reply