6

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'],

1 Answer 1

15

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.