简体   繁体   English

Google Analytics会拦截所有请求

[英]Google analytics intercept all requests

I would like to get a callback each time Google Analytics sends data to the server. 我希望每次Google Analytics向服务器发送数据时都会收到回调。 I would like also to send the same data to my server. 我也想将相同的数据发送到我的服务器。 Is it possible and if so, how? 是否可能,如果可能,怎么样?

https://jsfiddle.net/bk1j8u7o/2/ https://jsfiddle.net/bk1j8u7o/2/

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-143361924-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-143361924-1');
</script>

Google is actually using gif to sync the data to its server, so intercepting the XHR requests wont work. Google实际上使用gif将数据同步到其服务器,因此拦截XHR请求无法正常工作。

In analytics.js there is an official way to do that. 在analytics.js中,有一种官方方法可以做到这一点。 via Tasks , here is some small untested example: 通过任务 ,这是一些未经测试的小例子:

ga(function(tracker) {
    var originalSendHitTask = tracker.get('sendHitTask');
    tracker.set('sendHitTask', function(model) {
        var payLoad = model.get('hitPayload');
        originalSendHitTask(model);
        var gifRequest = new XMLHttpRequest();
        var gifPath = "http://localhost/collect";
        gifRequest.open('get', gifPath + '?' + payLoad, true);
        gifRequest.send();
    });
});

make sure that the pageView is sent after this code is executed. 确保在执行此代码后发送pageView。

I would demonstrate how you can intercept any AJAX call. 我将演示如何拦截任何AJAX调用。 Taking from this generic solution, you can filter the GA requests and take the actions you want. 借助此通用解决方案,您可以过滤GA请求并执行所需的操作。

I modified this answer. 我修改了这个答案。

The idea behind this solution is modifying the open and send prototype methods of XMLHttpRequest object and do the interception there. 这个解决方案背后的想法是修改XMLHttpRequest对象的opensend原型方法并在那里进行拦截。 The IIFE gets the XMLHttpRequest object, saves the original prototype methods, install new methods and call the original methods from within the new methods. IIFE获取XMLHttpRequest对象,保存原始原型方法,安装新方法并从新方法中调用原始方法。 And, of course, do what you want with the data in the mean time. 当然,同时用数据做你想做的事。

(function(XHR) {

    //Save the original methods
    var open = XHR.prototype.open;
    var send = XHR.prototype.send;


    //Hook new open method in order to get the url    
    XHR.prototype.open = function(method, url, async, user, pass) {
        this._url = url;

        //Call the original
        open.call(this, method, url, async, user, pass);
    };


    //Hook here too. This will be executed just before the data is sent
    XHR.prototype.send = function(data) {

        if (this_url === GA_URL_CONST)     //Symbolic const  
             SendDataToMyServer(data);     //Symbolic Fn  


        //Call the original
        send.call(this, data);
    }
})(XMLHttpRequest);

Possible? 可能? Yes, practical? 是的,实用吗? No. Take a look of what the BigQuery schema for GA looks like and you'll get a sense of the complexity that goes behind the scenes. 不。看看GA的BigQuery架构是什么样子,你会了解幕后的复杂性。

That said, I think what you COULD do is: 那就是说,我认为你可以做的是:

  1. Use GTM to implement GA. 使用GTM实施GA。
  2. Set up a custom tag template to refer to your own server that will collect the information. 设置自定义标记模板以引用将收集信息的您自己的服务器。 Passing just the data that you need, instead of everything GA collects. 仅传递您需要的数据,而不是GA收集的所有内容。
  3. Trigger your new custom tags wherever you're triggering your GA tags. 无论您何时触发GA标记,都会触发新的自定义标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM