Do a promotion activity on WeChat. There are three buttons on the page, and you need to count the number of clicks separately. The relevant statistics on PC use "Baidu Statistics". Because of the timeliness of the H5 activity page and other reasons, Baidu Statistics are not used, but to implement a simple one.Statistical small plan:When the front-end clicks, a blank small gif graph is requested with parameters. The back-end colleagues do statistics based on the nginx request log and distinguish whether the same user (uv) is the same by storing a timestamp that does not overlap in the cookie as the key value.
The requested picture exists in Qiniu and is fixed. The main changes are the following two parameters: the user ID uid and the button ID. It is interesting to generate a user ID that does not repeat (reduce the repetition rate to the lowest).
Use new Date().getTime() to obtain a 13-bit "random number", which is accurate to milliseconds, but what if there are more than two users clicking in the same millisecond? So be more rigorous and use a for loop to splice a random 5-bit string. This repetition rate is absolutely enough:
uid = new Date().getTime(); var randomNumber = ''; for(var i = 0 ; i < 5 ; i ++){ randomNumber += new String ((() * 10)); } uid = uid + randomNumber;
The following is the specific logical code. When there is a requested image on the web page, changing the url parameters can also initiate a new get request to avoid append a picture every time you click. This implementation method feels more convenient than clicking to send ajax.
statistics: function(position){ var pic = "/notification/statistics/"; var uid = ("uid"); var imgLength = $("#statistics-img").length; if(uid){ if(imgLength == 0){ $('body').append('<img src="' + pic + '?uid='+ uid + '&position='+ position +'"/>'); }else{ $("#statistics-img").attr("src",pic+"?u&position="+position); } }else{ uid = new Date().getTime(); var randomNumber = ''; for(var i = 0 ; i < 5 ; i ++){ randomNumber += new String ((() * 10)); } uid = uid + randomNumber; ("uid",uid); $('body').append('<img src="' + pic + '?uid='+ uid + '&position='+ position +'"/>'); } }
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!