1 2 3 4 5 6 7 8 9 10 11 12 |
<?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') ); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?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; } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!-- 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
Reference:
- Tag Manager for enhanced-ecommerce
- https://developers.google.com/tag-manager/enhanced-ecommerce#overview
- e-commerce tips for tag manager
https://www.simoahava.com/analytics/ecommerce-tips-google-tag-manager/ - Google Analytics for ecommerce:
https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce - 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/ - Implementing E-commerce tracking via tag manager:
https://www.optimizesmart.com/implementing-e-commerce-tracking-google-tag-manager/