Search code examples
javascripthtmlnodestrackinghtmlelements

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


This the code I keep seeing a lot on random pages.

I think it's a google tracking code but could someone confirm?

When I run it in JSFiddle nothing appears to happen.

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);


Solution

  • This is a script injected by Google's Tag Assistant extension. The source is much larger.

    I've unminifed the source code, and it can be found here on Gist. Be warned, it is ~6300 lines long - about twenty thousand characters too long for this answer.

    The injection occurs around line 3030, with a dynamically created script. Note kg, and how they are relying on Function.prototype.toString to get the source code of the function.

    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 can be found immediately afterwards, as the following, which corresponds with what you've posted.

    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)
    };

    A blurb about the extenstion, from 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.