javascriptjquerygoogle-datalayer

Get value from dataLayer.push


after finishing order on my eshop, there is this part of code:

dataLayer.push({
'event': 'trackTrans', 
'ecommerce': {
    'purchase': {
      'actionField': {
        'id': 'XXX',                         
        'affiliation': 'My eshop',
        'revenue': '158.99',                     
        'shipping': '00',
        'coupon': ''
      },
      'products': [

{'name': 'Microsoft Office',
 'id': 'XXX',
 'category': 'Microsoft', 
 'price': 158.99, 
 'quantity': 1}

]
    }
  }
});

I'm trying to save value of revenue into variable but I fail to reach revenue's value, how do I do it?

Thanks a lot.


Solution

  • dataLayer is an array of objects so you should rather use something like: dataLayer[x].ecommerce.purchase.actionField.revenue where x is position of your object (you can use [dataLayer.length -1] for latest object)