I believe that anyone who has used google's analytics knows about its powerful functions.
Due to work needs, we recently updated its urchin. the js Code is down and studied. since there are few annotations, it is difficult to understand the name. I studied it for one month. small success. rewrite the code and add comments. share with you. here, google's user identification code is deleted. /**//*
Copy content to clipboard
Code:
=====================================
|--------Author By BlackSoul---------|
|------------2006.04.27--------------|
|--------BlackSoulylk@gmail.com------|
|------------QQ:9136194--------------|
|------http://blacksoul.cnblogs.cn---|
======================================
*/
// Define the global volume
Var ur = ""; // Source Address
Var urp = new Array (); // Source Parameter Name
Var urpv = new Array (); // Source Parameter Value
Var arrayCount = 0; // number of parameters
PageOpen = new Date (); // time when the page is accessed
Var reqURL = "http: // 192.168.0.219/Analytics/"; // address of the page for receiving data
Var GUID = Math. round (Math. random () * 2147483647); // the unique random Number of the user.
Var title = document. title; // webpage title
Var uexp = pageOpen. getTime () + (1000*60*60*24*30); // set the cookie expiration time to meet the user's requirements
Var rtu = "false"; // indicates whether the user returns to the user
// Browser feature information
Var brower = new Array ();
/**//*
* Brower [0] browser type
* Brower [1] browser version
* Brower [2] indicates whether the browser java enables 1-1.
* Brower [3] browser flash version
* Brower [4] browser operating system
* Brower [5] browser resolution
* Brower [6] browser color depth
* Brower [7] browser Language
* Brower [8] browser plug-in
*/
Var sEn = new Array (); // name of the search engine
Var keyWord = new Array (); // keyWord transmission form
SEn [0] = "google"; keyWord [0] = "q ";
SEn [1] = "yahoo"; keyWord [1] = "p ";
SEn [2] = "msn"; keyWord [2] = "q ";
SEn [3] = "aol"; keyWord [3] = "query ";
SEn [4] = "lycos"; keyWord [4] = "query ";
SEn [5] = "ask"; keyWord [5] = "q ";
SEn [6] = "altavista"; keyWord [6] = "q ";
SEn [7] = "search"; keyWord [7] = "q ";
SEn [8] = "netscape"; keyWord [8] = "query ";
SEn [9] = "earthlink"; keyWord [9] = "q ";
SEn [10] = "cnn"; keyWord [10] = "query ";
SEn [11] = "looksmart"; keyWord [11] = "key ";
SEn [12] = "about"; keyWord [12] = "terms ";
SEn [13] = "excite"; keyWord [13] = "qkw ";
SEn [14] = "mamma"; keyWord [14] = "query ";
SEn [15] = "alltheweb"; keyWord [15] = "q ";
SEn [16] = "gigablast"; keyWord [16] = "q ";
SEn [17] = "voila"; keyWord [17] = "kw ";
SEn [18] = "virgilio"; keyWord [18] = "qs ";
SEn [19] = "teoma"; keyWord [19] = "q ";
SEn [20] = "baidu"; keyWord [20] = "wd ";
// Test data ----------------------------//////////////////////////////-----------/-/- /-/-/-/-/-/-/-/-/-
SEn [21] = "localhost"; keyWord [21] = "q ";
Here some global quantities are defined, where upr, urpv are the source parameters and indications, such as the source is http://www.google.com /? P = BlackSoul & q = javascript, then urp [0] = "p", urp [1] = "q" corresponds to urpv [0] = "BlackSoul ", urpv [1] = "javascript ".
But the final transfer is passed to the server as a separator.
The GUID is a random number, which google did. I didn't figure out why I wanted to save a random number, but I understood it only when I analyzed the data. In order to verify the uniqueness of the user.
Function processing:Copy content to clipboard
Code:
// ----------------------------- Compare the url. If it is a search engine, save the keyword -------------
Function getKeyword (url)
{
Var hostname;
If (url. indexOf (".") =-1)
{Hostname = url ;}
Else
{Hostname = url. substring (url. indexOf ("."), url. lastIndexOf ("."));}
For (var I = 0; I <sEn. length; I ++)
{
If (hostname = sEn [I])
{
For (var j = 0; j <urp. length; j ++)
{
If (urp [j] = keyWord [I])
{
Return urpv [j];
}
}
}
}
Return "";
}
// Convert a URL to an address, a page parameter, and a parameter value. The uri is a page address.
Function gethn (uri)
{
If (! Uri | uri = "") return "";
Ur = uri;
Var sub;
// With Parameters
If (ur. indexOf ("? ")! =-1)
{
Var url = ur. substring (0, ur. indexOf ("? "));
Var para = ur. substring (ur. indexOf ("? ") + 1, ur. length );
While (para. length> 0)
{
If (para. indexOf ("&") =-1)
{
Urp [arrayCount] = para. substring (0, para. indexOf ("= "));
Urpv [arrayCount] = para. substring (para. indexOf ("=") + 1, para. length );
Break;
}
Sub = para. substring (0, para. indexOf ("&"));
Urp [arrayCount] = sub. substring (0, sub. indexOf ("= "));
Urpv [arrayCount] = sub. substring (sub. indexOf ("=") + 1, sub. length );
Para = para. substring (para. indexOf ("&") + 1, para. length );
ArrayCount ++;
}
Return url;
}
Else
Return ur;
}
// ---------------------------- Obtain the domain name ---------------------------------------------
Function getHostName (url)
{
Url = url. substring (url. indexOf (': //') + 3, url. length );
Url = url. substring (0, url. indexOf ("/"));
Return url;
}
// --------------------------- Obtain the flash version ------------------------------------------
Function getFlash (){
Var f = "-1", n = navigator;
If (n. plugins & n. plugins. length ){
For (var ii = 0; ii <n. plugins. length; ii ++ ){
If (n. plugins [ii]. name. indexOf ('shockwave flash ')! =-1 ){
F = n. plugins [ii]. description. split ('shockwave flash') [1];
Break;
}
}
} Else if (window. ActiveXObject ){
For (var ii = 10; ii> = 2; ii --){
Try {
Var fl = eval ("new ActiveXObject ('shockwaveflash. ShockwaveFlash." + ii + "');");
If (fl) {f = ii + '. 0'; break ;}
}
Catch (e ){}
}
}
If (f = "-1 ")
Return f;
Else
Return f. substring (0, f. indexOf (".") + 2 );
}
// -------------------------- Set asynchronous transmission -----------------------------------
Function createXMLHttpRequest ()
{
If (window. XMLHttpRequest)
{
Return new XMLHttpRequest ();
}
Else if (window. ActiveXObject)
{
Return new ActiveXObject ("Microsoft. XMLHttp ");
}
}
The above method can obtain some basic information. however, the key issue is to set the user cookie, determine the return visit, determine the last access time, and set its GUID. I haven't seen how google works for a long time... so I thought of some stupid methods. see the code below:Copy content to clipboard
Code:
// Browser feature information
Function BrowserInfo ()
{
Brower [0] = navigator. appName;
Brower [7] = navigator. language;
If (brower [0] = "Netscape ")
{
Var browerInfo = navigator. userAgent;
Brower [1] = browerInfo. substring (browerInfo. lastIndexOf ("") + 1, browerInfo. length );
Brower [0] = brower [1]. substring (0, brower [1]. lastIndexOf ("/"));
Brower [1] = browerInfo. substring (browerInfo. lastIndexOf ("/") + 1, browerInfo. length );
Brower [7] = navigator. language;
}
Else if (brower [0] = "Microsoft Internet Explorer ")
{
Brower [1] = navigator. userAgent. split (";") [1];
Brower [7] = navigator. userLanguage;
}
Brower [2] = navigator. javaEnabled ()? 1:-1;
Brower [3] = getFlash ();
Brower [4] = getOS ();
If (self. screen ){
Sr = screen. width + "x" + screen. height;
SC = screen. colorDepth + "-bit ";
}
Else if (self. java)
{
Var j = java. awt. Toolkit. getdefatooltoolkit ();
Var s = j. getScreenSize ();
Sr = s. width + "x" + s. height;
}
// Resolution
Brower [5] = sr;
// Color depth
Brower [6] = SC;
// Plug-in list
Brower [8] = getPlugin ();
}
// ----------------------- Obtain the current address -----------------------------
Function getHref ()
{
Return document. location. href;
}
// ----------------------- The cookie operation starts to renew -----------------------------------------------------------------------------------------------------------------
Function setCookie (name, value)
// Set the Cookie value
{
Var expdate = new Date ();
Var argv = setCookie. arguments;
Var argc = setCookie. arguments. length;
Var expires = 15768000;
Var path = (argc> 3 )? Argv [3]: null;
Var domain = (argc> 4 )? Argv [4]: null;
Var secure = (argc> 5 )? Argv [5]: false;
If (expires! = Null)
{
// Set the expiration time to 24 hours
Expdate. setTime (uexp );
Document. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + expdate. toGMTString ()))
+ (Path = null )? "": ("; Path =" + path) + (domain = null )? "": ("; Domain =" + domain ))
+ (Secure = true )? "; Secure = ":"");
}
}
Function delCookie (name)
// Delete the Cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
Document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
// Obtain the Cookie value
Function getCookie (fname)
{
Var name, value;
Var cookies = new Object ();
Var beginning, middle, end;
beginning = 0;
while(beginning < document.cookie.length)
{
middle = document.cookie.indexOf("=",beginning);
end = document.cookie.indexOf(";",beginning);
if(end == -1)
{
end = document.cookie.length;
}
if((middle > end) || (middle == -1))
{
name = document.cookie.substring(beginning,end);
value = "";
}
else
{
name = document.cookie.substring(beginning,middle);
value = document.cookie.substring(middle+1,end);
}
if(name == fname)
{
return unescape(value);
}
beginning = end + 2;
}
}
// ----- Obtain the existence of the GUID cookie ---------------------------------------------------
Function getCookieValue ()
{
Var guid = getCookie ("GUID ");
If (guid! = Null)
{
Return guid;
}
Else
{
Return "noCookie ";
}
}
// --------------------- Obtain the cookie of the registered user ---------------------------------------------
Function getRegUserCookie ()
{
Return;
}
// ------------------------------- The cookie operation is complete ------------------------------------------------------------------------------------------------------------
// ----------------------------- Operating system ---------------------------
Function getOS ()
{
Var OSlist = new Array ();
Var OSName = new Array ();
OSlist [0] = "Windows4.0"; OSName [0] = "Windows 95 ";
OSlist [1] = "Windows 98"; OSName [1] = "Windows 98 ";
OSlist [2] = "Windows NT 5.0"; OSName [2] = "Windows 2000 ";
OSlist [3] = "Windows NT 5.1"; OSName [3] = "Windows XP ";
OSlist [4] = "Windows NT 5.2"; OSName [4] = "Windows Server 2003 ";
Var ua = navigator. userAgent. split (";");
For (var I = 0; I <OSlist. length; I ++)
{
If (ua [2] = OSlist [I])
Return OSName [I];
}
Return ua [2];
}
// Obtain the plug-in
Function getPlugin ()
{
Var plugin = "";
Var ua = navigator. userAgent. split (";");
If (ua. length <4)
Return "";
For (var I = 4; I <ua. length; I ++)
{
Plugin + = ua [I] + ",";
}
Return plugin. substring (0, plugin. length-2 );
}
One thing to note is that the GetResidentTime function uses img. the load method loads the page for receiving and processing information. Then, you need to configure the server to suffixed with .gif or other forms of files you have obtained to compile and run them using aspx. because I used to consider using xmlHttp for asynchronous calls. however, when the page exits, it is sometimes not executed. it is discarded. the Code is as follows:Copy content to clipboard
Code:
function GetResidentTime()
{
pageClose = new Date();
minutes = (pageClose.getMinutes() - pageOpen.getMinutes());
if(minutes < 0)
{
minutes = minutes + 60;
}
seconds = (pageClose.getSeconds() - pageOpen.getSeconds());
if(seconds < 0){ seconds += 60;}
time = (seconds + (minutes * 60));
// ------------------------ Modify the address here to receive the link url xml asynchronous transmission ------------------------------------
// Var xmlHttp = createXMLHttpRequest ();
// XmlHttp. open ("POSt", reqURL + firstvisit. aspx? "+ StrPara (), false );
// XmlHttp. send (null );
// ---------------------------- Transmit the image in the form -------------------------------------------------------
If (isReturn () = false)
{
Var I = new Image (1, 1 );
i.src = reqURL + "firstVisit.aspx?" + StrPara() + "&GUID=" + GUID;
i.onload = function() {LoadVoid();}
// Enter the page information
If (getCookieValue ("GUID") = "noCookie") {return ;}
I. src = reqURL + "pageView.gif? "+ PageView () +" & st = "+ time;
I. onload = function () {LoadVoid ();}
}
Else
{
var i=new Image(1,1);
i.src = reqURL + "pageView.gif?" + pageView() + "&st=" + time;
i.onload=function() {LoadVoid();}
}
}
Finally, it is called. I used window. onunload = GetResidentTime
You can print the passed parameters and see what they mean.
The rest is the server-side processing, which is actually quite simple. obtain the request value, verify and save it to the database. now that data is available, the analysis is handled by users according to their own requirements .. the IP address is obtained and saved by the server.
Remember to put it on the server, and then use <javascript language = "javascript" type = "text/javascript" src = server address + path + "statistics. js "> </script> with the complete code http://files.cnblogs.com/blacksoul/statistic.rardownload