javascript - 如何删除数据层事件(GTM)?
问题描述
所以如果 GTM 嵌入到网络中,我们可以使用dataLayer.push({'event':'event_name'})
我可以弹出它来删除它吗?或者我如何删除该 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.
推荐阅读
- php - 在 PHP 的 SoapServer 中的 SetClass 之后添加 SoapHeader
- seo - 在标题中重新 nosnippet 标记
- php - 是否可以将 php 与烧瓶结合使用?
- jmeter - 如何在采样器错误时停止 Jmeter 中的当前事务?
- gitlab - git标记后防止触发Gitlab-ci作业
- project-reactor - 在reactor-kafka中立即生效?
- go - 指针检查中的 CGO 已知实现错误
- macos - 如何从 GitHub 安装此小部件?
- javascript - 特定页面中的自定义 Javascript 代码
- kubernetes - Kubernetes Pods 数小时前失败,如何调试已终止的 Pod