1

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
  });
Agava
  • 11
  • 2
  • you probably meant `price` instead of `'price'` (since it is a variable)? – phil294 Feb 18 '18 at 00:02
  • Yes, so is that possible and correct? Or is it wrong? cause on some sites it shows somethinking like obj.price or something else but not directly a variable.. :( – Agava Feb 18 '18 at 00:11

1 Answers1

0

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.

phil294
  • 10,038
  • 8
  • 65
  • 98