A tutorial on how to implement the user clicks count by javascript

Source: Internet
Author: User
Tags repetition

In the micro-letter to do a promotional campaign, the page amounted to three buttons, the need to count the number of clicks, the relevant statistics on the PC is "Baidu statistics", because the H5 activity page timeliness and other reasons, and did not use Baidu statistics, but their own to achieve a simple statistical small scheme: Front Click to request a blank gif, with parameters, back-end colleagues according to the Nginx request log to do statistics, by depositing in a cookie does not overlap the timestamp as a key value to distinguish whether the same user (UV).

The requested picture exists in seven cows, is fixed, the main change is the following two parameters: User ID UID and button identification, where the generation does not duplicate (the repetition rate to the lowest) user identification is interesting.

The timestamp uses the new Date (). GetTime () to get a 13-bit "random number" that is accurate to milliseconds, but what if more than two users click in the same millisecond? So more rigorous, with a for loop in a random 5-bit string concatenation, such a repetition rate is absolutely sufficient:

UID =newdate (). GetTime ();
Varrandomnumber = ';
for (vari = 0; i < 5; i + +) {
Randomnumber +=newstring (Math.floor (Math.random () * 10));
}
UID = uid + randomnumber;

The following is the specific logic code, when the page has requested a picture, change the URL of the parameters can also be a new GET request, to avoid every click Append a picture. This implementation feels more convenient than clicking on the send Ajax.

Statistics:function (position) {
Varpic = "Yun_qi_img/p.gif";
Varuid = Util.readcookie ("UID");
Varimglength = $ ("#statistics-img"). Length;
if (UID) {
if (Imglength = = 0) {
$ (' body '). Append (' ');
}else{
$ ("#statistics-img"). attr ("src", pic+ "uid=" +uid+ "&position=" +position);
}
}else{
UID =newdate (). GetTime ();
Varrandomnumber = ';
for (vari = 0; i < 5; i + +) {
Randomnumber +=newstring (Math.floor (Math.random () * 10));
}
UID = uid + randomnumber;
Util.createcookie ("UID", UID);
$ (' body '). Append (' ');
}
}
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.