Clear Arrays by pushing an empty array
Let's start with this simple DataLayer object:
{
page: {
campaigns: ['campaign1', 'campaign2']
}
}
Then I push this:
adobeDataLayer.push({
page: {
campaigns: []
}
});
Expected Behaviour
adobeDataLayer.getState() returning this:
{
page: {
campaigns: []
}
}
Actual Behaviour
adobeDataLayer.getState() returning this:
{
page: {
campaigns: ['campaign1', 'campaign2']
}
}
Workaround
You need to push null to delete it before setting it to an empty array as described above.
adobeDataLayer.push({
page: {
campaigns: null
}
});
The other way would be to first find out how many items there are in the array already and do a push like this:
adobeDataLayer.push({
page: {
campaigns: [null, null]
}
});
I'm also seeing this issue and with updating arrays in general. Attempting to update an existing array of:
campaigns: ['camp-one', 'camp-two']
with an update of:
campaigns: ['camp-one']
will still result in a datalayer with:
campaigns: ['camp-one', 'camp-two']
when calling adobeDataLayer.getState()
Is this by design or a bug? I was expecting any update in general to be valid.
Just curious about this issue, but would you mind describing the need to clear these arrays?
@jm-adobe in a case of wanting clear an array or pass new information on an event to the dataLayer. This also seems to be a possibility described in the documentation yet overwrite may not work as expected? .. One important detail to clarify is what happens when pushing data to an already existing array: it will overwrite the data of the inner array.
I can't believe this has not been fixed yet. The currently observed behavior is the complete opposite of what one would expect. If I assign a new value to an existing variable I expect that variable to reference the new value, not a combination of the new and previous value.