After I push the data to the dataLayer of GoogleTagManager, with for example:
dataLayer.push({user: 'me', site: 'bacon.com'})
how can I get the whole object pushed to the data layer from the GTM panel, without knowing what is pushed?
Maybe there is js variable that store this information?
Thanks
The dataLayer is a global JavaScript variable, an array of objects. GTM can access global variables via the JavaScript variable type, which reads the whole array.
You can then use a custom JavaScript variable, which is an anonymous function that returns a value, to extract the last element in the that array.
So you'd first create a GTM variable of the javascript type and set the variable name to "dataLayer":
And then use that to access the last element of the returned array:
in a custom JS variable to get the most recent entry, in your case "{user: 'me', site: 'bacon.com'}" without having to know the exact keys of the object. You could then iterate through the object to do whatever you need to do with the individual values.
Keep in mind that variables are only evaluated when an event occurs, so you either need a "native" GTM event (click, submit etc) or you need to push a custom event. Also it might happen that the last element is actually an event itself (e.g. the gtm.click if the click event happened after you pushed your data).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With