admin管理员组文章数量:1435061
When I push anything to datalayer (which is an array), does GTM notice that and push it directly to the Google Platform?
Or is it only happening once, when the GTM container is loaded?
If not, does GTM somehow subscribe to datalayer.push() ? Or is it just polling?
When I push anything to datalayer (which is an array), does GTM notice that and push it directly to the Google Platform?
Or is it only happening once, when the GTM container is loaded?
If not, does GTM somehow subscribe to datalayer.push() ? Or is it just polling?
Share Improve this question asked Aug 25, 2016 at 14:03 AlexAlex 35.3k19 gold badges114 silver badges197 bronze badges2 Answers
Reset to default 6When you initialize GTM you pass the reference of you dataLayer object, which from that point on is replaced by an 'instance' of a special object managed by the GTM code. Compare what the chrome console tells you about the push method before and after you initialize GTM:
Before:
After:
When you push something to a GTM-initalized dataLayer, are actually calling a custom push method that informs it to the GTM code running on the client, if that push triggers a tag fire, it will result in a tag code being run, but it does not make any requests otherwise and is not sent to GTM servers or anything like that.
Observe that apart from pushes, there are many other events that GTM watches, such as clicks, form submits, DOM ready and even the container load, as you described.
You are not restricted to pushing events, you can (and in many cases should) push regular data (messages) and it will still be usable and visible on Google Tag Manager, just bear in mind that it needs an interaction to actually update its internal state (such as a click)
How they go about doing it, I do not know, but it could be done with something as simple as:
var dataLayer = [{name: 'Rui'}];
var DLWatch = (function(){
var init = function(){
window.dataLayer.push = push;
};
var push = function(data) {
window.dataLayer = window.dataLayer.concat([data]);
console.log('Data pushed!');
};
return {init: init};
})();
DLWatch.init();
dataLayer.push({foo: 'bar'});
To what you will get:
Data pushed!
And dataLayer will be
Here's my take on the DL. When you push something to it, GTM does detect it but it doesn't necessarily track that new data into GA. Data that is pushed in registers with the DL, but if you do not explicitly use that data in a pageview or an event or some other tag, then that data just remains in the DL. But to explicitly use the data you just pushed in, you should always also push an event
which you can use to 'access' the new DL data.
So GTM does constantly 'listen' for new things being pushed in, but tags need to be set up to use the new data.
本文标签: javascriptDoes Google Tag Manager quotsubscribequot to dataLayerpush If yeshowStack Overflow
版权声明:本文标题:javascript - Does Google Tag Manager "subscribe" to dataLayer.push? If yes, how? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744844028a2628093.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论