How to properly use dataLayer.push(), to update values, of nested objects, in an array?
Asked Answered
G

1

6

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.

Geomorphic answered 10/12, 2015 at 22:33 Comment(0)
P
9

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:

GTM preview debugger DataLayer view

Interpreting this Debugger Data Layer view

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&#5 should be considered inaccessible when tracking could next occur on tags that fire on event #7.

Paulinapauline answered 10/12, 2015 at 23:20 Comment(3)
Just so I can get this straight, If I push several objects with the same name, e.g. giftCount, the variable in GTM will only pick up the last/most recent iteration of giftCount, i.e.: 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
@Geomorphic exactly, but you don't have to take my word for it, the preview debugger will show you what you've added and what the affects are on the effective dataLayer and variables for each message.Paulinapauline
I took your word for it. I recreated my initial dataLayer.push() and setup all further .push()'s to follow the same order of operation. GTM is only picking up the most recent dataLayer variables. Coming from a database background, I was worried about abusing the dataLayer capacity, but since it is technically a per page temp batch, I think this is a non-issue. @Paulinapauline Thank you !Geomorphic

© 2022 - 2024 — McMap. All rights reserved.