Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create GTM variable from dataLayer array?

I have no problems creating GTM variables from 'simple' dataLayer, example:

"transactionEntity": "ORDER",
"transactionId": "193552702",

But can't understand how to take values from array. Basically i need to take ID value from these dataLayer table:

"transactionProducts":     [
      {
        "id": "5",
        "sku": "black-handbag",
        "price": 170,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      },
      {
        "id": "3",
        "sku": "red-handbag",
        "price": 120,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      }
    ],

And to pass them to FB pixel using this format:

content_ids: ['5', '3'],
like image 488
MployBy Avatar asked Dec 14 '22 21:12

MployBy


1 Answers

If you have datalayer like that:

<script>
dataLayer.push({
  'ecommerce': {
     "transactionProducts":     [
      {
        "id": "5",
        "sku": "black-handbag",
        "price": 170,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      },
      {
        "id": "3",
        "sku": "red-handbag",
        "price": 120,
        "priceexcludingtax": "0.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "bundle",
        "category": "",
        "quantity": 1
      }
    ]
  }
});
</script>

And you want to create variable which will return array of IDs ['5', '3']

Then you need to create two variables:

1) Name: transactionProducts

Type: Data Layer Variable

Data Layer Variable Name: ecommerce.transactionProducts

2) Name: transactionProductsIds

Type: Custom JavaScript

Custom JavaScript: function () { return {{transactionProducts}}.map(function(a) {return a.id;}); }

And then you can use your second variable transactionProductsIds to receive IDs

like image 54
Victor Leontyev Avatar answered Apr 28 '23 08:04

Victor Leontyev