Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the whole data of Google Tag Manager data layer

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

like image 869
Oscar Fanelli Avatar asked Sep 18 '25 03:09

Oscar Fanelli


1 Answers

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":

enter image description here

And then use that to access the last element of the returned array:

enter image description here

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).

like image 76
Eike Pierstorff Avatar answered Sep 19 '25 17:09

Eike Pierstorff