我的DOM中的Google Analytics像素在哪里?

d-_*_*_-b 6 javascript google-analytics

如何使用JavaScript识别Google Analytics像素(或任何像素)已发送,并包含我正在寻找的网址参数?

我想,因为它是一个跟踪像素,我可以在DOM中查找它,但它看起来不像它曾经插入过.

有人能想出一种方法来分析谷歌使用javascript(不是Chrome扩展程序)所做的网络请求吗?

就像是

document.whenGooglePixelIsSentDoReallyCoolStuff(function(requestUrl){

});
Run Code Online (Sandbox Code Playgroud)

Phi*_*ton 6

一些东西:

1)跟踪信标并不总是像素.有时他们是XHR,有时他们使用navigator.sendBeacon取决于情况和/或你的跟踪器的传输设置,所以如果你只是寻找像素,你可能会在错误的地方看.

2)您不需要向DOM添加图像以使其发送请求.简单地做document.createElement('img').src = "path/to/image.gif"就足够了.

3)您无需使用Chrome扩展程序来调试Google Analytics,您只需加载脚本调试版本而不是常规版本.

4)如果您确实不想使用Google Analytics的调试版本并希望以编程方式跟踪发送的内容,则可以sendHitTask在发送之前覆盖和拦截匹配.

更新(2015年7月21日)

你已经改变了你的问题的措辞,所以我会回答新的措辞,说你应该遵循我在上面#4中给出的建议.以下是一些适用于您的假设whenGooglePixelIsSentDoReallyCoolStuff函数的代码:

document.whenGooglePixelIsSentDoReallyCoolStuff = function(callback) {

  // Pass the `qa` queue method a function to get acess to the default
  // tracker object created via `ga('create', 'UA-XXXX-Y', ...)`. 
  ga(function(tracker) {

    // Grab a reference to the default `sendHitTask` function.
    var originalSendHitTask = tracker.get('sendHitTask');

    // Override the `sendHitTask` to call the passed callback.
    tracker.set('sendHitTask', function(model) {

      // When the `sendHitTask` runs, get the hit payload,
      // which is formatted as a URL query string.
      var requestUrl = model.get('hitPayload')

      // Invoke the callback passed to `whenGooglePixelIsSentDoReallyCoolStuff`
      // If the callback returns `false`, don't send the hit. This allows you
      // to programmatically do something else based on the contents of the
      // request URL.
      if (callback(requestUrl)) {
        originalSendHitTask(model);
      }
    });
  });
};
Run Code Online (Sandbox Code Playgroud)

请注意,您必须在创建跟踪器之后,但在发送第一个匹配之前运行此功能.换句话说,您必须在以下两行代码之间运行它:

ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');
Run Code Online (Sandbox Code Playgroud)