什么是为了观察去`dataLayer`阵列使用谷歌代码管理模式? [英] What is the pattern used by Google Tag Manager in order to observe de `dataLayer` Array?

查看:391
本文介绍了什么是为了观察去`dataLayer`阵列使用谷歌代码管理模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到的数据层数组,我看不到的任何更改。根本没有自定义方法,其实。 GTM如何观察更改阵列?据我所知,变为一个阵列不要扔任何事件,不是吗?

I observed the dataLayer array and I don't see any changes to push. No custom methods at all, actually. How is GTM observing the changes to the array? As far as I know, changes to an Array don't throw any events, do they?

我做了一些调查研究,发现谷歌的库与数据层交互:的 https://github.com/google/data-layer-helper#listening-for-messages 结果
我会看看在code和甚至回答我的问题,如果我理解ineer运作。

I did some more research and found Google's library for interacting with the dataLayer: https://github.com/google/data-layer-helper#listening-for-messages
I'll take a look at the code and maybe even answer my own question if I understand the ineer workings.

推荐答案

由GTM使用的模式是发布 / 用户

Pattern used by GTM is publish / subscriber

在code,有助于识别它的一些细节:线76和的 https://github.com/google/data-layer-helper/blob/master/src/helper/helper.js

Some details in code that helps to recognize it: line 76 and 181 of the https://github.com/google/data-layer-helper/blob/master/src/helper/helper.js

最后行114和119

And finally line 114 and 119

// Add listener for future state changes.
  var oldPush = dataLayer.push;
  var that = this;
  dataLayer.push = function() {
    var states = [].slice.call(arguments, 0);
    var result = oldPush.apply(dataLayer, states);
    that.processStates_(states);
    return result;
  };

看一看到状态变量,它是如何传递给 this.processStates _()

Take a look into states variable and how it is passed to this.processStates_()

这篇关于什么是为了观察去`dataLayer`阵列使用谷歌代码管理模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆