我导航到的大多数页面上的这个脚本是什么?

What is this script on most of the pages I navigate to?

这是我在随机页面上经常看到的代码。

我认为这是一个 google 跟踪代码,但有人可以确认一下吗?

当我 运行 它在 JSFiddle 中似乎没有任何反应。

var tvt = tvt || {};
tvt.captureVariables = function(a) {
  for (var b = new Date, c = {}, d = Object.keys(a || {}), e = 0, f; f = d[e]; e++)
    if (a.hasOwnProperty(f) &&
      "undefined" != typeof a[f]) try {
      var g = [];
      c[f] = JSON.stringify(a[f], function(a, b) {
        try {
          if ("function" !== typeof b) {
            if ("object" === typeof b && null !== b) {
              if (b instanceof HTMLElement || b instanceof Node || -1 != g.indexOf(b)) return;
              g.push(b)
            }
            return b
          }
        } catch (c) {}
      })
    } catch (l) {}
    a = document.createEvent("CustomEvent");
  a.initCustomEvent("TvtRetrievedVariablesEvent", !0, !0, {
    variables: c,
    date: b
  });
  window.dispatchEvent(a)
};
window.setTimeout(function() {
  tvt.captureVariables({
    'dataLayer': window['dataLayer']
  })
}, 2000);

它只是一个用于检查变量是否被定义的短代码,如果没有,则将其定义为空object。它类似于:

if(tvt === undefined){
   tvt = {};
}

基本上,它确保了:

  • 变量已定义(和一个object)
  • 它保留任何以前的属性(如果有的话)

var obj1 = {prop: 123};
var obj2 = obj1 || {}; // obj2 will have the value of obj1 because obj1 is defined
var obj3 = obj3 || {}; // obj will be an empty object

document.write(JSON.stringify({obj2: obj2, obj3: obj3}));


免责声明:我只回答了代码的第一行,因为我知道这就是您从标题中提出的要求。但现在我看到你可能指的是整个代码块。如果是这样,请忽略此答案。

这是由 Google 的 Tag Assistant 扩展程序注入的脚本。来源要大得多。

我已经对源代码进行了压缩,可以在 here Gist 上找到它。请注意,它大约有 6300 行长 - 大约两万个字符对于这个答案来说太长了。

注入发生在 line 3030 左右,使用动态创建的脚本。注意 kg,以及他们如何依赖 Function.prototype.toString 来获取函数的源代码。

lg = function(a) {
  if (!a.Ab && (a.Ab = !0, a = a.X(), a = Object.keys(a).map(function(a) {
    return "'" + a + "': window['" + a + "']"
  }), 0 < a.length && (!aa.tvt || !aa.tvt.captureVariables))) {
    var b = document.createElement("script");
    b.textContent = "var tvt = tvt || {}; tvt.captureVariables = " + kg + ";window.setTimeout(function() {tvt.captureVariables({" + a.join(",") + "})}, 2000);";
    (document.head || document.documentElement).appendChild(b)
  }
}

kg可以找到immediately afterwards,如下,与你发的内容相对应

kg = function(a) {
  for (var b = new Date, c = {}, d = Object.keys(a || {}), e = 0, f; f = d[e]; e++)
    if (a.hasOwnProperty(f) &&
      "undefined" != typeof a[f]) try {
      var g = [];
      c[f] = JSON.stringify(a[f], function(a, b) {
        try {
          if ("function" !== typeof b) {
            if ("object" === typeof b && null !== b) {
              if (b instanceof HTMLElement || b instanceof Node || -1 != g.indexOf(b)) return;
              g.push(b)
            }
            return b
          }
        } catch (c) {}
      })
    } catch (l) {}
  a = document.createEvent("CustomEvent");
  a.initCustomEvent("TvtRetrievedVariablesEvent", !0, !0, {
    variables: c,
    date: b
  });
  window.dispatchEvent(a)
};

关于扩展的简介,来自 About Tag Assistant

About Tag Assistant

Tag Assistant is a Chrome Extension that automatically validates the implementation of Google tracking scripts on any given page. It currently works for:

  • Google Analytics (ga.js)

  • Google Analytics (dc.js)

  • AdWords Conversion Tracking

  • AdWords Remarketing (legacy)

  • AdWords Remarketing (new remarketing tag)

  • Doubleclick Floodlight

  • Google Tag Manager

How Tag Assistant Works

After installing the extension, you will see an icon indicating the status of the tag on the page you're viewing.

  • A grey icon with an 'x' indicates that no code was found on the page

  • A green indicator will show if a valid tag was found. The number in the icon indicates the number of tags that were found

  • A blue indicator will show there are suggestions on improving overall tagging health

  • A yellow indicator will show that a tag was found with minor implementation issues

  • A red indicator will show that a tag was found with critical implementation issues

Click the icon to open a window containing tag details. It shows how many tags were found, how many errors were found, any errors, and a link to the most relevant help document available to resolve the problem. In addition, Tag Assistant makes suggestions when improvements can be made. For example, if an outdated Analytics tag has been installed, Tag Assistant may suggest that you update to a newer tag.