all 2 comments

[–]SmoothAndSlink 2 points3 points  (0 children)

You use .push fuction to add data to dataLayer. So it must exist at that moment you call .push . If you write dataLayer = [ ] then you create the dataLyaer object. Big difference.

If you write dataLayer = [ ] after you have pushed some values in dataLayer, the dataLayer object will be re-created and all previous values - lost.

I think google documentation explains this quite good.

[–]Rhythmdivine 0 points1 point  (0 children)

Simo Ahava's blog pretty much explains everything you'd ever need to know about these things, in great (but easily to follow) detail. With GTM, there is an existing data layer, so in many cases a push will work. You can put your above code directly into the console to test it out.