Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 884
rorschaff Avatar asked Dec 10 '15 22:12

rorschaff


People also ask

How do I use dataLayer push?

After the dataLayer has been declared, use dataLayer.push({'item': 'value'}) to add additional values to it, and if those values need to be available to Tag Manager when the page loads, then that dataLayer.push() call needs to be above the Tag Manager container code as well.

What does the dataLayer push () command return?

The return value, assuming you are referring to when you pasted the code into the console, indicates whether a GTM tag fired in response to the push. "true" means that no tags fired, and "false" means that a tag fired.

How do I access dataLayer variables?

You can find a full list of them by going to Variables > Configure. After you enable these variables, they'll appear in GTM Preview and Debug mode's Variables Tab.

How do you define dataLayer variable using JavaScript?

Click Variable Configuration and select Data Layer Variable as the variable type. In the Data Layer Variable Name field, enter the key exactly as it was written in the code (e.g. bookTitle, not book title.) In most cases you should leave the Data Layer Version set to the default value of Version 2. Learn more.


1 Answers

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.

like image 114
lossleader Avatar answered Oct 05 '22 12:10

lossleader