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
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
Share Improve this question asked Apr 22, 2010 at 20:51 PranzPranz 1751 silver badge5 bronze badges 3- 2 I'm guessing anybody who knows the answer to this isn't legally allowed to share it. – brettkelly Commented Apr 22, 2010 at 20:52
- 1 Well, the Google Analytics JS is here, so dig in if you feel like it: google-analytics./urchin.js Of course, that doesn't answer the server-side questions. – icktoofay Commented Apr 22, 2010 at 20:57
- 1 Nothing stopping you read the JS yourself and working it out. There is nothing private about any scripts you send to the user to execute (though it is generally not worth reverse-engineering obfuscated code, though an auto-formatter does let you follow and debug it). – Nicholas Wilson Commented Apr 22, 2010 at 20:58
3 Answers
Reset to default 6The 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 e 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
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'].
(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;