Google Analytics Ecommerce tracking via Tag Manager

<?php
// Transaction Data
$trans = array('id'=>'1234', 'affiliation'=>'Acme Clothing',
               'revenue'=>'11.99', 'shipping'=>'5', 'tax'=>'1.29');

// List of Items Purchased.
$items = array(
  array('sku'=>'SDFSDF', 'name'=>'Shoes', 'category'=>'Footwear', 'price'=>'100', 'quantity'=>'1'),
  array('sku'=>'123DSW', 'name'=>'Sandals', 'category'=>'Footwear', 'price'=>'87', 'quantity'=>'1'),
  array('sku'=>'UHDF93', 'name'=>'Socks', 'category'=>'Footwear', 'price'=>'5.99', 'quantity'=>'2')
);
?>
<?php
// Function to return the JavaScript representation of a TransactionData object.
function getTransactionJs(&$trans) {
  return <<<HTML
ga('ecommerce:addTransaction', {
  'id': '{$trans['id']}',
  'affiliation': '{$trans['affiliation']}',
  'revenue': '{$trans['revenue']}',
  'shipping': '{$trans['shipping']}',
  'tax': '{$trans['tax']}'
});
HTML;
}

// Function to return the JavaScript representation of an ItemData object.
function getItemJs(&$transId, &$item) {
  return <<<HTML
ga('ecommerce:addItem', {
  'id': '$transId',
  'name': '{$item['name']}',
  'sku': '{$item['sku']}',
  'category': '{$item['category']}',
  'price': '{$item['price']}',
  'quantity': '{$item['quantity']}'
});
HTML;
}
?>
<!-- Begin HTML -->
<script>
ga('require', 'ecommerce');

<?php
echo getTransactionJs($trans);

foreach ($items as &$item) {
  echo getItemJs($trans['id'], $item);
}
?>

ga('ecommerce:send');
</script>

 

Enhanced ecommerce tracking with tag manager

Image 20

Reference:

  1. Tag Manager for enhanced-ecommerce
  2. https://developers.google.com/tag-manager/enhanced-ecommerce#overview
  3. e-commerce tips for tag manager
    https://www.simoahava.com/analytics/ecommerce-tips-google-tag-manager/
  4. Google Analytics for ecommerce:
    https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce
  5. Unlock the Data Layer: A Non-Developer’s Guide to Google Tag Manager
    https://www.lunametrics.com/blog/2013/10/15/unlock-data-layer-google-tag-manager/
  6. Implementing E-commerce tracking via tag manager:
    https://www.optimizesmart.com/implementing-e-commerce-tracking-google-tag-manager/