1

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.

jahu
  • 5,427
  • 3
  • 37
  • 64
  • After reading the answer by Tomek, I can't help but notice that in some ways it's the same question as http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order. Well, it's not exactly the same, but the answers explain why the position of event shouldn't matter in the object pushed to dataLayer. – jahu Feb 05 '15 at 14:30

1 Answers1

1

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.

  • I was thinking along the lines too, but I wasn't sure (especially if the order of properties is kept or not). Then again, it would make little sense if GTM relied on a method that can hardly be predicted (properties will likely be ordered in some way in most browsers, but it's not something to be relied upon). Either way, thanks for the answer. – jahu Feb 05 '15 at 14:21