我可以将 google 分析片段重定向到我的服务器吗?

Can i redirect the google analytics snippet to my Server?

我需要在我的网站上实施网络分析 http://www.example.com. I found that the Google's analytics snippet can be added to the 'footer.php' of my website, and this will trigger Google's ga function 从而提供分析仪表板等。

我必须知道是否可以更改 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来重定向数据,这个其实有解释by example in the GA documentation:

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。

首先我在网站 footer.php

</body> 标签前附加了以下脚本
<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);

但是,并不是所有的 Measurement Protocol 参数都列出来了,我必须进一步配置它。

P.S。一旦找到获取所有参数的方法,我将编辑此答案。