我的DOM中Google Analytics(分析)像素在哪里? [英] Where is the Google Analytics pixel in my DOM?

查看:52
本文介绍了我的DOM中Google Analytics(分析)像素在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用JavaScript识别已发送Google Analytics(分析)像素(或与此相关的任何像素)并包含我正在寻找的URL参数?

How can I identify, using JavaScript that a Google Analytics pixel (or any pixel for that matter) has been sent, and contains URL parameters i'm looking for?

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

I thought, since it's a tracking pixel, i could look for it in the DOM, but it doesn't look like it's ever inserted.

有人能想到一种方法来分析使用javascript(不是chrome扩展名)的google的网络请求吗?

Can someone think of a way to analyze the network request made by google using javascript (not a chrome extension)?

类似

document.whenGooglePixelIsSentDoReallyCoolStuff(function(requestUrl){

});

推荐答案

几件事:

1)跟踪信标并不总是像素.有时它们是XHR,有时它们使用 navigator.sendBeacon 根据情况和/或跟踪器的传输设置,所以如果您只是在寻找像素,那可能是在错误的位置.

1) The tracking beacons aren't always pixels. Sometimes they're XHR and sometimes they use navigator.sendBeacon depending on the situation and/or your tracker's transport setting, so if you're just looking for pixels you could be looking in the wrong place.

2)您无需将图像添加到DOM就可以使它发送请求.只需做 document.createElement('img').src ="path/to/image.gif" 就足够了.

2) You don't need to add an image to the DOM to get it to send the request. Simply doing document.createElement('img').src = "path/to/image.gif" is sufficient.

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

3) You don't need to use a Chrome extension to debug Google Analytics, you can simply load the debug version of the script instead of the regular version.

4)如果您真的不想使用Google Analytics(分析)的调试版本,并且想跟踪以编程方式发送的内容,则可以覆盖

4) If you really don't want to use the debug version of Google Analytics and want to track what is sent programmatically, you can override the sendHitTask and intercept hits before they're sent.

您已经更改了问题的措词,因此我将回答新措词,说您应遵循我在上面#4中给出的建议.以下代码可与您假设的 GooglePixelIsSentDoReallyCoolStuff 函数一起使用:

You've changed how your question is worded, so I'll answer the new wording by saying you should follow the suggestion I give in #4 above. Here's some code that would work with your hypothetical whenGooglePixelIsSentDoReallyCoolStuff function:

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

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

Note that you'd have to run this function after creating your tracker, but prior to sending your first hit. In other words, you'd have to run it between the following two lines of code:

ga('create', 'UA-XXXX-Y', 'auto');
ga('send', 'pageview');

这篇关于我的DOM中Google Analytics(分析)像素在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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