How to read a dataLayer?

Asked

Viewed 130 times

2

Good afternoon, everyone,

I need to bring a Lieyer data in my system. The people who work with Google Tag Manager have created some dataLayer and I need to gather some information with javascript to use in other places.

How can I read a particular dataLayer and get your information? Example of a dataLayer:

dataLayer.push({
        'transactionId': '{{mov_vehicle_lead}}',
        'transactionTotal': '{{PProd_Price_AmericanStandard}}',
        'transactionProducts': [{
            'sku': '{{PProd - VehicleID}}',
            'name': '{{PProd_Veiculo}}',
            'category': '{{PProd_Marca}}',
            'price': '{{PProd_Price_AmericanStandard}}',
            'quantity': '{{Quantidade}}'
    }],
    'event': 'transactionComplete'
    });

Thanks for the help! Thank you!

  • 1

    If the dataLayer is an array, so much so that it adds new entries to this array with push, must use a for to go through and get what you want. Give a concrete example of something you wanted to get from dataLayer

  • 1

    For example, I would like to take the sku value of the dataLayer.

1 answer

2

Is that what you thought? Returns an array of transactionProducts skus (put the name to better view).


const skus = (transactionId) => window.dataLayer.forEach(obj => {
  if(obj.transactionId === transactionId) {
    return obj.transactionProducts.map({ sku, name }) => {
      sku,
      name
    });
  }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.