首页 > 解决方案 > 如何删除数据层事件(GTM)?

问题描述

所以如果 GTM 嵌入到网络中,我们可以使用dataLayer.push({'event':'event_name'})

我可以弹出它来删除它吗?或者我如何删除该 dataLayer 事件?

标签: javascriptgoogle-tag-managergoogle-datalayer

解决方案


GTM will not notice if you pop the event from the dataLayer (since GTM needs an event to update its internal state, so the only way to make this known to GTM would be another event, at which point the remove operation becomes redundant).

Usually you can reset a datalayer value by pushing "undefined". For the reason given above I doubt that this will work with the event key (but I admit I haven't tried), but it is good practice with other variables.

You can reset the datalayer alltogether from inside GTM, via

google_tag_manager[“GTM-XXXX”].dataLayer.reset()

where GTM-XXXX is the id of your GTM container. However that will just empty out everything.

The thing is, there is no real usecase for removing the event, since GTM will never look at it twice. GTM amends the native push() function of the dataLayer array with some code that, among other things, scans for the event keyword, and then takes all new variables and merges them with the existent internal state of the GTM container, and then the event is done; GTM won't use it for anything else. So even if you did reset the event, GTM would not even be aware of it.


推荐阅读