Nikita Chernykh Nikita Chernykh - 3 months ago 9
Javascript Question

GTM dataLayer .push creates a new object instead of adding it to existing dataLayer

I'm trying to create a dataLayer for my website and I have this problem.
As i understand: Google Tag Manager dataLayer works that way so you have one dataLayer object with all the data variables. And everytime you use dataLayer.push it will add more data to your dataLayer object.
In my case it creates the separate objects. Here is a screenshots how it looks:
enter image description here

So the first object is my dataLayer, here is code:

enter image description here

And the 4th object is one more dataLayer created with dataLayer.push which suppose to be added to existing one if I understand correctly.

enter image description here

So instead of having one object I have 2 and if I add more .push methods I will have even more objects.

So how do I have all my dataLayer.push pushing to my main dataLayer instead of creating an extra objects?

enter image description here

Please have a look and point if I'm missing something important, I'm new to this and can't understand why don't I have one complete dataLayer object in the end.

Answer

I'm new to this and can't understand why don't I have one complete dataLayer object in the end.

Cause that's how dataLayer and GTM works :) Everything looks fine on your screenshot. Each dataLayer.push should add additional object (datalayer is an array of objects).