Reputation: 34958
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?
Upvotes: 4
Views: 2480
Reputation: 675
When 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
Upvotes: 6
Reputation: 8907
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.
Upvotes: 2