After I push the data to the dataLayer of GoogleTagManager, with for example:
dataLayer.push({user: 'me', site: 'bacon'})
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
After I push the data to the dataLayer of GoogleTagManager, with for example:
dataLayer.push({user: 'me', site: 'bacon.'})
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
Share Improve this question asked Jan 8, 2017 at 14:35 Oscar FanelliOscar Fanelli 3,6773 gold badges32 silver badges40 bronze badges1 Answer
Reset to default 8The 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.'}" 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).