Here is my dataLayer array:
dataLayer = [{
'giftBatch' : {
'giftID': '',
'giftAmount': 0,
'giftCount': 0,
'giftUpdate': {
'giftPhase': 'Gift Empty'
}
},
'txBatch': {
'txID': '',
'txTotal': 0,
'txURL': window.location.href,
'txUpdate': {
'txPhase': 'Transaction Opened',
'txT0': new Date(),
'txT1': ''
'txT2': ''
}
}
}];
The console results are: Array [Object1]
Object1 contains the 'giftBatch' and 'txBatch' Objects, as desired.
I have a trigger that fires later to update the object in the dataLayer.
For example, update the 'giftAmount' to 50 and 'giftCount' to 1.
I have tried the following (I am only showing my unsuccessful attempts at modifying 'one object at a time'),
Attempt 1:
dataLayer.push({giftAmount : 50});
Result:
Array [object1, object2]
Object1 is the same as above,
Object2 is a new object with the property of 'Gift Amount' : 50,
Attempt 2:
dataLayer.push({giftBatch.giftAmount: 222});
Result: SyntaxError: missing : after property id
Attempt 3:
dataLayer.push({'giftBatch.giftAmount' : 50});
Result:
Array [object1, object2]
Object1 is the same as above,
Object2 is a new object with the property 'giftBatch.giftAmount': 50
What am I doing wrong here?
According to the dataLayer section here: https://support.google.com/tagmanager/answer/6106899?hl=en
I should be able to edit nested objects values.
PS. This is what I'm using now, and it does work. But, why doesn't push work?
dataLayer[index].giftBatch.giftAmount = 50;
Where index is the index of Object2.
Any help would be great.
Thank you.
dataLayer.push({'giftCount' : 1});
dataLayer.push({'giftCount' : 2});
dataLayer.push({'giftCount' : 3});
dataLayer.push({'giftCount' : 4});
GTM Variable giftCount is equal to dataLayer Variable giftCount = 4 ? – Geomorphic