最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the whole data of Google Tag Manager data layer - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.'}" 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).

发布评论

评论列表(0)

  1. 暂无评论