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.
It is bad practice to edit existing contents of the dataLayer, but you should just need to send the overriding property value(s) like this:
dataLayer.push({'giftBatch':{'giftAmount' : 50}});
The tag manager starts at the latest Object
and will keep looking backwards through previous Objects to determine the current setting of each DataLayer variable, so only giftBatch.giftAmount
is overriden with this new push.
Here is an example of preview debugger showing the dataLayer's merged view of a new test
Object with properties from previous messages:
In this instance, previous messages (#4 and/or #5) pushed at a minimum:
{test:{test:8}} // #6 does not contain test.test so it is from earlier
or at a maximum they could have pushed:
{test:{test:..,foo:..,test3:..}} // #4 if it's been completely shadowed
{test:{test:8,foo:..,test3:..}} // #5 if it has test.test, must have 8
No tags can fire between #3 and #7 as Messages
are objects that lack event
properties, so any shadowed values from #4 should be considered inaccessible when tracking could next occur on tags that fire on event #7.