How to push a dynamic price to the datalayer?

722 Views Asked by At

How can i send a dynamic price that should change to the datalayer? can't i simply send a variable like below example?

Var price; //this price will dynamically change based on the product value


  dataLayer.push({
  'event': 'gtm-event',
  'value': price
  });
1

There are 1 best solutions below

0
phil294 On

Not sure about google-datalayer, but primitives in Javascript are passed by value, and objects by reference. So, consider

var price = 3;
var object_with_price = { price: 3 };

var data_with_price = { value: price };
var data_with_object = { value: object_with_price };

If you now change price to 4, data_with_price.price will still be 3 because the value had been passed.

But if you do object_with_price.price = 4, then data_with_object.value.price will also be 4.

If you change object_with_price = { price: 5 }, data_with_object will remain unchanged.