GA Ecommerce datalayer

Product Page

var product = {
  'name': '<?=$product->name?>',         // Name or ID is required.
  'id': '<?=$product->id?>',
  'price': '<?=$product->price?>',
  'brand': '<?=$store->name?>',
  'category': '',
  'qty': 1
}

window.dataLayer = window.dataLayer || [];
dataLayer.push({
  'event': 'productDetail', 
  'ecommerce': {
    'detail': {
      'actionField': {'list': 'Product Detail'},    // 'detail' actions have an optional list property.
      'products': [product]
     }
   }
});

function add_cart_event(qty){
  product.qty = qty;

  dataLayer.push({
    'event': 'addToCart',
    'ecommerce': {
      'currencyCode': 'TWD',
      'add': {                                // 'add' actionFieldObject measures.
        'products': [product]
      }
    }
  });
}

Function to get object for Pixel AddToCart in Tag manager

function(){
	try{
		var af = google_tag_manager[{{Container ID}}].dataLayer.get("ecommerce");
        var product = af.add.products[0];
        var data = {
          content_ids: [product.id],
          content_type: 'product',
          content_name: product.name,
          value: product.price,
          currency: 'TWD'
        }
        console.log(data);
		return data;
	}catch(e){
		return 'undefined';
	}
}

Trigger

Pixel AddToCart