Requirement: Count the number of clicks at a certain part of the user page or the number of clicks executed to a certain point in the program
Features: Create multiple Image objects based on the actual situation. The principle is "Who is idle and who is doing things. To solve the problem of missing tracking due to excessive transmission of tracking data.
Implementation(Note the following code depends on jQuery)
Copy codeThe Code is as follows:
Var Statistic = {
ArrImg: [],
Log: function (from ){
// If the parameter is null, It is not processed.
If (typeof (from) = "undefined" | from = "") return;
Var me = this, img, imgHandler, arrImg, len = 0, index =-1;
ArrImg = me. arrImg;
Len = arrImg. length;
// Query available Image objects
For (var I = 0; I <len; I ++ ){
If (arrImg [I]. f = 0 ){
Index = I;
Break;
}
}
// Retrieve or generate an Image object
If (index =-1 ){
Img = $ (new Image ());
ArrImg. push ({f: 1, img: img });
Index = (len = 0? 0: len );
} Else {
Img = arrImg [index]. img;
}
// Mark the Image object as in use
ArrImg [index]. f = 1;
// Record the location of the Image object used
Img. data ("vid", index );
ImgHandler = function (){
Var vid = $ (this). data ("vid ");
If (vid> = 0 ){
ArrImg [vid]. f = 0;
}
};
Img. unbind (). load (imgHandler). error (imgHandler );
Var arr = [], ref, url = "http://z.ccccccc.com/beacon.gif? ";
Ref = document. referrer;
If (ref ){
Ref = encodeURIComponent (ref );
} Else {
Ref = '';
}
// Strictly follow the sequence below: t, r, from, version
Arr. push ('t = '+ (new Date (). getTime ());
Arr. push ('R = '+ ref );
$ (Img). attr ("src", url + arr. join ('&'));
}
};