This is an interview question today, I think the more simple is to record the user's data through a cookie, and then when the page is closed and then send the server.
Search on the Internet Baidu Statistics, GA related information, it seems to be through the cookie record related information, save data by requesting a picture and then bring different parameters to achieve.
I wonder if you have any other good ways.
Reply content:
This is an interview question today, I think the more simple is to record the user's data through a cookie, and then when the page is closed and then send the server.
Search on the Internet Baidu Statistics, GA related information, it seems to be through the cookie record related information, save data by requesting a picture and then bring different parameters to achieve.
I wonder if you have any other good ways.
I'm not saying much.
var _hmt = _HMT | | [];/* Specifies the site ID *///_hmt.push ([' _setaccount ', ' XXXXXXXX ']) of the account to be used in response to the JS-API call, or//for sending a PV statistic request for a specified URL, typically used for PV statistics on Ajax pages. _hmt.push ([' _trackpageview ', pageurl]);//used to trigger an event, such as the click of a button, or the player's play/stop, and the start/pause of the game. _hmt.push ([' _trackevent ', category, action, Opt_label, Opt_value]);//When a user accesses a page that has Baidu statistics code installed, the code automatically sends the PV statistic request for that page, If you do not want to automatically count the PV of this page, you can use this interface. Mainly used in the case of IFRAME nesting pages. _hmt.push ([' _setautopageview ', false]);(function () {//load Baidu's statistical code var HM = document.createelement ("script"); HM.SRC = "//hm.baidu.com/hm.js?xxxxxxx"; var s = document.getelementsbytagname ("script") [0]; S.parentnode.insertbefore (hm, s);}) ();(function () {function domonitorfordatatrack (target) {var target = target; var _track = Target.getattribute ("Data-track"); if (typeof (_HMT)! = "undefined" && _track! = ") {if (_track.indexof (" _hmt.push ") >-1) { eval (_track); } else {var _trackarr = _track.split ('. '); if (_trackarr.length < 2) return; var category = ' Click ';//required, parameter is string var action = ';//required, parameter is string var label = ';//optional, parameter is Word String/** when there is only one split character, * For example data-track= "index.10001", execute function _hmt.push ([' _trackevent ', ' Click ', ' index ', ' 10001 ']) * */if (_trackarr.length = = 2) {Categor y = ' Click '; Action = _trackarr[0];//Required, argument is string label = _trackarr[1];//Optional, argument is string}/ * * When there are more than two or two split characters, * such as data-track= "Songli. BTN. Secured transaction ", Execution _hmt.push ([' _trackevent ', ' Songli ', ' BTN ', ' secured transaction ']); * such as data-track= "Songli. BTN. Secured transactions. 1002 ", performing _hmt.push ([' _trackevent ', ' Songli ', ' BTN ', ' secured transaction. 1002 ']); * */else {category = _trackarr.shift ();//required, argument is string action = _tra Ckarr.shift ();//required fields, parameter is string LABel = _trackarr.join ('. '); /optional, parameter is string} _hmt.push ([' _trackevent ', category, action, label]); }}} function Addevent (obj, type, fn) {if (obj.attachevent) {obj[' E ' + type + fn] = fn; OBJ[TYPE+FN] = function () {obj[' E ' +type+fn] (window.event);} Obj.attachevent (' on ' + type, Obj[type + fn]); } else Obj.addeventlistener (type, FN, false); } var _elemarr = document.getElementsByTagName (' A '), _elemlen = _elemarr.length; var _body = Document.body | | Document.documentelement; Addevent (_body, "click", Function (event) {var event = event? event:window.event; var target = event.srcelement? Event.srcElement:event.target; if (Target.getattribute (' Data-track ')) {domonitorfordatatrack (target); } else if (Target.nodename = = "IMG" && target.parentNode.getAttribute (' Data-track ')) {Domonitorfordata Track (TARGET.PArentnode); } });}) ();
Browse the track, click on the button can again serve to write to MONGO inside, form JS listen dom
These statistics exist in the cookie is generally just some to identify the user ID value. The user's operation record data should be sent to the statistical server immediately, there is no need to save the local to send, this kind of data is very small, does not cause congestion. As for how to send the past, most of it is to use your understanding of the simple loading of a picture, through the parameters of the data passed up.
"When the page is closed to send the service side," This is a problem, the browser is closed when your JS basically can not anything.