Reputation: 13
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.
Upvotes: 2
Views: 2055
Reputation: 3407
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.
Upvotes: 3