简体   繁体   English

Google跟踪代码管理器是否“订阅” dataLayer.push? 如果是,怎么办?

[英]Does Google Tag Manager “subscribe” to dataLayer.push? If yes, how?

When I push anything to datalayer (which is an array), does GTM notice that and push it directly to the Google Platform? 当我将任何内容推送到数据层(即数组)时,GTM是否会注意到并将其直接推送到Google平台?

Or is it only happening once, when the GTM container is loaded? 还是仅在加载GTM容器时发生一次?

If not, does GTM somehow subscribe to datalayer.push() ? 如果不是,那么GTM是否以某种方式订阅了datalayer.push()? Or is it just polling? 还是只是轮询?

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. 初始化GTM时,您传递的是dataLayer对象的引用,从那时起,该引用将由GTM代码管理的特殊对象的“实例”替换。 Compare what the chrome console tells you about the push method before and after you initialize GTM: 比较chrome控制台在初始化GTM之前和之后告诉您的push方法:

Before: 之前:

GTM之前

After: 后:

GTM之后

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. 当您将某些内容推送到GTM初始化的dataLayer时,实际上是在调用自定义推送方法,该方法将其通知客户端上运行的GTM代码,如果该推送触发代码触发,则会导致代码代码运行,但是否则不会发出任何请求,也不会发送到GTM服务器或类似的东西。

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. 观察到,除了推送之外,还有GTM监视的许多其他事件,例如单击,表单提交,DOM准备就绪,甚至容器加载,如您所述。

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) 您不限于推送事件,您可以(在许多情况下应该)推送常规数据(消息),并且这些数据仍将在Google跟踪代码管理器中可用并可见,只需记住,它需要进行交互才能实际更新其内部状态(例如点击)

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 并且dataLayer将是

数据层

Here's my take on the DL. 这是我对DL的看法。 When you push something to it, GTM does detect it but it doesn't necessarily track that new data into GA. 当您向其中推送内容时,GTM会检测到它,但不一定会将新数据跟踪到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. 推入的数据向DL注册,但是如果您未在网页浏览量或事件或其他标签中明确使用该数据,则该数据仅保留在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. 但是,要显式地使用刚输入的数据,还应始终推送一个event ,该event可用于“访问”新的DL数据。

So GTM does constantly 'listen' for new things being pushed in, but tags need to be set up to use the new data. 因此,GTM确实会不断“监听”要推送的新内容,但是需要设置标签以使用新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM