In large-scale Web site do a lot of user behavior analysis, product planning is basically through the analysis of user access and other information made, the statistical accuracy of log information will directly affect the product design and development (such as the results of the ranking of rank value generation, etc.). One of the most commonly used ways to write log is to use JavaScript scripts in the Web page to New Image (). src = "http://xxx.com/log?msg=" + msg; This statistical method basically does not interfere with the normal operation of the user, although there is the possibility of log loss, but as long as the use of good, but also a very good log statistics recovery scheme.
But this log recycling method has a very hidden hidden danger , the thing is found. This March Baidu search results page on-line suggestion feature upgrade, in the log statistics suddenly found on the line after the log total is much less than before the online (more than 10%), the phenomenon is that the log is missing, not recycled to the server.
Engineers immediately do a detailed elimination, from a variety of factors to determine the large number of log loss and Web page suggestion related, we immediately to the suggestion script carpet, analysis of the code, the conclusion is that the script does not block new Image (). src Package request (at least on the surface), this script is placed in a script closure that does not affect the global variables/methods, nor does it mask the factors that interfere with HTTP requests.
We did another offline test, the same code environment, in the test environment under the line with this new Image (). SRC, a total of 10,000 log data sent to the server side, there is no log loss, the results of the test did not find the cause of log loss.
But the online environment is that there is log data loss, unexplained , so only emergency offline this suggestion upgrade version. After the script is offline, the log statistics are immediately returned to the "original normal" state. From this point of view, also from another perspective of the new script on the line does have an impact on the log statistics, headache ah ...
A fool is a man, and one must be! The Last known reason for this problem is the garbage collection mechanism of the browser !
functionC (q) {varP=window.document.location.href,sq= ', sv= '; for(Vinchq) { Switch(v) { Case"title": Sv=encodeuricomponent (Q[v].replace (/<[^<>]+>/g, "")); Break; Case"url": Sv=escape (Q[v]); Break; default: sv=Q[v]} SQ+=v+ "=" +sv+ "&"; } NewImage (). src = "http://s.baidu.com/w.gif?q=meizz&" +sq+ "path=" +p+ "&cid=9&t=" +NewDate (). GetTime (); return true;}
This new Image () object is not assigned to any variables, and at the end of the function execution, the browser's garbage collection mechanism is not polite to reclaim this "no-Master" object, and it is this kind of recycling that causes the HTTP request (async) to not be emitted. This results in the loss of log data . So why does a script on-line cause a lot of log loss? Because a large script runs back to generate a lot of "garbage", browser garbage collection will also be started more frequently, resulting in log data loss. After finding the right remedy, assign the new Image () object to a global variable constant, and the corresponding code is as follows:
var n = "Log_" + (Newdate ()). GetTime (); var c = window[n] =newimage (); // assigns new Image () to a global variable for long-term holding c.onload = (c.onerror=functionnull= "http// S.baidu.com/w.gif?q=meizz "+null; // release local variable C
After the statistics code on the line, Baidu's search results page of the log immediately more than 10%, and then go online other scripts also no more log statistics fluctuations.
Local variables, test 1000 consecutive log,ie6-7 found log lost, some IE kernel browser also found that the log is missing, Chrome did not find lost.
So the insurance practice is to hold with a non-local variable:
varUnique = (function () { varTime= (NewDate ()). GetTime () + '-', i=0; return function () { returnTime + (i++); } })(); varImglog =function(URL) {vardata = window[' Imglogdata ' | | (window[' imglogdata ') = {}); varIMG =NewImage (); varUID =Unique (); Img.onload= Img.onerror =function() {//destroy some objectsImg.onload = Img.onerror =NULL; IMG=NULL; DeleteData[uid]; } img.src= URL + ' &_uid= ' +uid; };
This article originates from FuDesign2008
"Go" with new Image (). A note for log statistics of SRC