我可以重定向谷歌分析片段到我的服务器


Can i redirect the google analytics snippet to my Server?

我需要在我的网站http://www.example.com上实现web分析。我发现,谷歌的分析片段可以添加到我的网站的'footer.php',这将触发谷歌的ga功能,从而提供分析仪表板等。

我必须知道我是否可以改变JavaScript片段来重定向数据到我的服务器,以获得原始数据并处理它们。

编辑:我通过谷歌找到了下面的代码。但是我无法理解它的实际功能,因为我是JavaScript的初学者。

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-YY', 'auto');
// START remote backup of GA data request for Angelfish
ga(function(tracker) {
  var originalSendHitTask = tracker.get('sendHitTask');
  tracker.set('sendHitTask', function(model) {
   var payLoad = model.get('hitPayload');
   originalSendHitTask(model);
   var gifRequest = new XMLHttpRequest();
   // Send __ua.gif to a remote server
   var gifPath = "https://www.your-domain.com/__ua.gif";
   gifRequest.open('GET', gifPath + '?' + payLoad, false);
   gifRequest.send();
  });
});
// END remote backup of GA data request for Angelfish
ga('send', 'pageview');
</script>

您可以通过添加sendHitTask来重定向数据,这实际上在GA文档中通过示例进行了解释:

ga('create', 'UA-XXXXX-Y', 'auto');
ga(function(tracker) {
  // Grab a reference to the default sendHitTask function.
  var originalSendHitTask = tracker.get('sendHitTask');
  // Modifies sendHitTask to send a copy of the request to a local server after
  // sending the normal request to www.google-analytics.com/collect.
  tracker.set('sendHitTask', function(model) {
    originalSendHitTask(model);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/localhits', true);
    xhr.send(model.get('hitPayload'));
  });
});
ga('send', 'pageview');

问题的第二部分(如何处理)太宽泛了,不能在这里回答。

终于找到了答案,感谢@Eike Pierstorff。

首先,我在网站的</body>标签之前添加了以下脚本footer.php

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
 ga('create', 'UA-XXXXX-Y', 'auto');
 ga(function(tracker) {
  // Grab a reference to the default sendHitTask function.
  var originalSendHitTask = tracker.get('sendHitTask');
  // Modifies sendHitTask to send a copy of the request to a local server after
  // sending the normal request to www.google-analytics.com/collect.
  tracker.set('sendHitTask', function(model) {
  originalSendHitTask(model);
  var payLoad = model.get('hitPayload');
  var xhr = new XMLHttpRequest();
  var gifPath = 'http://YourPath/process_ga_data.php';
  xhr.open('GET', gifPath + '?' + payLoad, false);
  xhr.send();
  });
});
ga('send', 'pageview');
</script>

在我推送Google分析有效负载的服务器中,编写了以下PHP代码,以便将所有参数作为数组获取到Output.txt中。

 if (isset($_REQUEST))
 $req_dump = print_r($_REQUEST, TRUE);  
 $file = file_put_contents('output.txt', $req_dump.PHP_EOL, FILE_APPEND);  
 fclose($file);

但是,并不是所有的测量协议参数都列出了,我必须进一步配置。

注:我将编辑这个答案,一旦我找到一个方法来获取所有的参数。