2

I want to create a tracking script. Something similar with Google Analytic but very basic.

The requirements are

I need simple js script like Google Analytic does make most of the logic inside the js file from the main site collect in PHP the information and store it What I can't figure yet is what are the ways to do this? Google from what I see is loading a gif file, stores the information and parses the logs. If I do something similar sending the data to a php file Ajax cross site policy will stop me, from what I remember.

  • if the javascript is on your server, and it is sending data to another server, nothing will stop you. – CodeBird Feb 28 '14 at 10:13

1 Answers1

3

Not sure on how Google Analytics does things, but the way to circumvent the x-site policy is, simply, don't do an Ajax call. Suppose you used javascript and now have an hash with your visitor's data:

var statsPage = 'http://mysite/gather_stats.php';
var stats = {
    page: location.href,
    browser: 'ie',
    ip: '127.0.0.1',
    referral: 'google.com?search=porn'
};
var params = $.param(stats); // serializes it https://api.jquery.com/jQuery.param/

now you only have to do a GET request to you php page with this string as a parameter, don't use Ajax tho, simply use the url as an img src

$('<img>', {
    src: statsPage + '?' + params
}).appendTo('body').remove()

you may as well use a script tag the same way, but you should pay attention because anything the php stats page returns will be executed as javascript (which is exactly how jsonp works).

Bear in mind that some limits apply to Get strings length.

Community
  • 1
  • 1
David Fregoli
  • 3,377
  • 1
  • 19
  • 40
  • Like this answer, both funny and simple to understand. Why did you add this `.remove()` after you appended the image to the ? Does the image have to be removed or is it just because the url is not an image? –  Mar 05 '17 at 20:47