views:

235

answers:

3

Hello everyone, I am making a Google Analytics like project for my school assignment. I have two questions primarily:

1) Exactly when does Google store the data to the database? When it does it use XHR with some server side scripting language to store it to the database or is there a way to do it using plain javascript?

2) How do I get the IP address of a user from Javascript? How does Google do for Analytics??

Thanks for all the help.

Pranz

+2  A: 

1) Yes. The JavaScript on the user-side sends a request to the server, which processes it using its own application (you can use PHP or any language your server is set up to call).

2) You might find it simplest to grab the IP address using $_SERVER['REMOTE_ADDR'].

Nicholas Wilson
A: 

(1) Google Analytics is implemented by including what is known as a "page tag". This is referred to as the Google Analytics Tracking Code (GATC) and is a hidden snippet of JavaScript code that the user adds onto every page of his or her website. This code acts as a beacon, collecting private visitor data and sending it back to Google data collection servers for processing. Data processing takes place hourly, though it can be 3–4 hours in arrears of real time. Not sure what they use at server side may be Java

(2) You need server side includes to get this working - as shown below. /

/ This part gets the IP
var ip = '<!--#echo var="REMOTE_ADDR"-->';

// This part is for an alert box
alert("Your IP address is "+ip);

// This part is for the status bar
window.defaultStatus = "Your IP address is "+ip;
+1  A: 

Hi Pranz

The Google Analytics JS code doesn't talk directly to the server - it adds an image to the page, and appends all the info to send back as URL parameters on the image - check out a page with Google Analytics running with the Firebug Net panel running and you'll see what's happening. So the JS code doesn't need to work out the IP - that's going to come through to the GA recording servers as part of the image request.

The best official description of what happens is in the Google BigTable paper - there's a few paragraphs which give a sort of hint as to what happens behind the scenes.

It might also be worth following this Stack Overflow question on structuring the database which records & reports on the activity.

regards

Jamie
Oh...so thats how they avoid cross site scripting issues??
Pranz
Yes, everything happens within the context of the hosted page, so all the cookies are first-party, and there's no cross-site scripting.
Jamie