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;
$ (' body '). Append (' ');
$ ("#statistics-img"). attr ("src", pic+ "uid=" +uid+ "&position=" +position);
UID =newdate (). GetTime ();
for (vari = 0; i < 5; i + +) {
Randomnumber +=newstring (Math.floor (Math.random () * 10));
UID = uid + randomnumber;
Util.createcookie ("UID", UID);
$ (' body '). Append (' ');
|