AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
4.9K members online now
4.9K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

JavaScript Array for Multiple Items in Purchase

Follower ✭ ✭ ☆
# 1
Follower ✭ ✭ ☆

How do you set up the JS array on the ecommerce tracking code to properly track the values (SKU, quantity, revenue, tax, shipping, etc.) for multiple items. The code is currently only returning values for TWO items,, even when more are present  in the order.

 

<script type="text/javascript">
		var sXXcar = sXXecar || {};
		sidecar.transactions = {
    		add: true,
    		data: {
        		order_id: '{{dataLayerTransactionID}}',
        		subtotal: '{{dataLayerTransactionTotal}}',
        		tax: '{{dataLayerTransactionTax}}',
        		shipping: '{{dataLayerTransactionShipping}}',
        		discounts: '0.00',
        		total: '{{dataLayerTransactionTotal}}',
        		zipcode: ''
    		},
    		items: [
        		{
            		product_id: '{{sku}}',
            		unit_price: '{{price}}',
            		quantity: '{{quantity}}'
        		},{
            		product_id: '{{sku}}',
            		unit_price: '{{price}}',
            		quantity: '{{quantity}}'
        		}    		],
    		discounts: [
        		{
            		name: 'DISCOUNT 1',
            		amount: '0.00'
        		},{
            		name: 'DISCOUNT 2',
            		amount: '0.00'
        		}
    		]
		};
	</script>

Re: JavaScript Array for Multiple Items in Purchase

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

Hello! A little disclaimer: I haven't implemented an ecommerce Analytics setup in two years and I currently don't have access to a domain to test out this solution. So take it with a grain of salt as this is all theoretical. Smiley Indifferent

 

Based on the code you've pasted here, it seems like you only have two slots for products in the items array here (product 1 in orange, product 2 in green):

items: [
        		{
            		product_id: '{{sku}}',
            		unit_price: '{{price}}',
            		quantity: '{{quantity}}'
        		},{
            		product_id: '{{sku}}',
            		unit_price: '{{price}}',
            		quantity: '{{quantity}}'
        		}    		]

 

If you want to add items to that array, you can try using the .push() method via JS to add those.

 

Looking at the documentation for Enhanced Ecommerce implementation via GTM, it makes use of .push() to add a product to the shopping cart. Here's the example from that page--the push method itself is in red, the ones in orange are the contents inside the parenthesis:

 

dataLayer.push({
 
'event': 'addToCart',
 
'ecommerce': {
   
'currencyCode': 'EUR',
   
'add': {                                // 'add' actionFieldObject measures.
     
'products': [{                        //  adding a product to a shopping cart.
       
'name': 'Triblend Android T-Shirt',
       
'id': '12345',
       
'price': '15.25',
       
'brand': 'Google',
       
'category': 'Apparel',
       
'variant': 'Gray',
       
'quantity': 1
       
}]
   
}
 
}
});

Hope this helps!

JavaScript Array for Multiple Items in Purchase

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

<script type="text/javascript">
var sxxxcar = sxxxcar || {};
var orderdata = {{transProducts}};
var itemCount = orderdata.length;
var itemList = [];
for (var i = 0; i < itemCount; i++) {
itemList.push({
product_idSmiley Surprisedrderdata[i].sku,
unit_priceSmiley Surprisedrderdata[i].price,
quantitySmiley Surprisedrderdata[i].quantity
});
}
sxxxcar.transactions = {
add: true,
data: {
order_id:'{{transactionId}}',
subtotal:'{{transactionTotal}}',
tax:'{{transactionTax}}',
shipping:'{{transactionShipping}}',
discounts:'',
total:'{{transactionTotal}}',
zipcode:''
},
items: itemList,
discounts: {}
};
</script>