Javascript uses the Image object to achieve tracking (clicks in a certain place) Statistics

Source: Internet
Author: User

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 ('&'));
}
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.