简体   繁体   中英

Can i redirect the google analytics snippet to my Server?

I need to implement web analytics on my website 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 thereby provide analytics dashboards and so on.

I have to know whether I can alter the JavaScript snippet to redirect data to my server, as to get the raw data and process them.

Edit: I have found this code below, by googling. But I was not able to understand what it actually does, being too much beginner to 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>

You can redirect the data by adding to the sendHitTask, which is actually explained 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');

The second part of the question (how to process) is to broad to answer here.

Finally found the answer, Thanks to @Eike Pierstorff.

First I appended the following script before the </body> tag of websites 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>

And in the server to which I am pushing the Google analytics payload, the following PHP code was written, as to fetch all the parameters into Output.txt as an array.

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

But still, not all the Measurement Protocol parameters are listing, I have to further configure that.

PS I will edit this answer, once I find out a way to fetch all the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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