I have piece of GTM code in which event is sometimes placed at the top of the object and sometimes at the bottom. Now the question is, is there any difference (like one sending data from previous event) and which is the right way to handle it?
dataLayer.push({
'event': 'addToCart',
'ecommerce': {
'add': {
'actionField': { 'list': 'Bestsellers' },
'products': [{
'name': 'Product 1',
'id': 1,
'position': 1,
'quantity': 1
}]
}
}
});
Or
dataLayer.push({
'ecommerce': {
'add': {
'actionField': { 'list': 'Bestsellers' },
'products': [{
'name': 'Product 1',
'id': 1,
'position': 1,
'quantity': 1
}]
}
},
'event': 'addToCart'
});
Note this is just an example. In case of adding products to the cart the event is always at the top of dataLayer, but in case of impression the event is always at the bottom (this part of code was based on a tutorial found on the internet in which events were placed there). On the first glance, the GA stats seem to register just fine. However in case of the GTM+GA combo it's sometimes difficult to find out if something works incorrectly (as opposed to finding out if it works at all), hence the question.
You are pushing javascript object into dataLayer. Inside of an object all the properties (ecommerce, event are in fact properties of an object) are indexed based on their name, not some number like in arrays, and they are further called precisely using their names. Objects in Javascript does not guarantee any order in their properties. Concluding, sequence in which you define properties in an object does not matter as they will be further "unordered", even in the moment of pushing it to the dataLayer.