Javascript-when a website obtains a user's browsing track, it clicks a button to enter the relevant behavior data in a form. what is a better way?

Source: Internet
Author: User
This is a face-to-face question for today's interview. what I think is simple is to record user data through cookies, and then send it to the server when the webpage is closed. I searched Baidu Statistics online and found GA-related information. it seems that the cookie is used to record the relevant information and save the data... this is a face-to-face question for today's interview. what I think is simple is to record user data through cookies, and then send it to the server when the webpage is closed.

I searched Baidu statistics on the internet. GA's related information seems to be recorded through cookies. The data is saved by requesting an image and then carrying different parameters.

I wonder if you have any other good methods.

Reply content:

This is a face-to-face question for today's interview. what I think is simple is to record user data through cookies, and then send it to the server when the webpage is closed.

I searched Baidu statistics on the internet. GA's related information seems to be recorded through cookies. The data is saved by requesting an image and then carrying different parameters.

I wonder if you have any other good methods.

I will not talk about it anymore.

Var _ hmt = _ hmt | [];/* specifies the site id of the account to respond to the JS-API call * // _ hmt. push (['_ setaccount', 'xxxxxxxx']); // used to send PV statistics requests for a specified URL, usually used for PV statistics on AJAX pages. // _ Hmt. push (['_ trackPageview', pageURL]); // used to trigger an event, such as clicking a button, playing or stopping a player, and starting or pausing a game. // _ Hmt. push (['_ trackevent', category, action, opt_label, opt_value]); // when you access a page with Baidu Statistics code installed, the code will automatically send PV statistics requests for this page. if you do not want to automatically count the page PV, you can use this interface. It is mainly used for nesting pages of iframe. // _ 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. the parameter is the string var action = ''. // required, the parameter is the string var label = ''; // Optional. the parameter is a string/** when only one character is separated, * for example, data-track =" index.10001 ", run the function _ hmt. push (['_ trackevent', 'click', 'index', '000000']) **/if (_ trackArr. length = 2) {category = 'click'; action = _ TrackArr [0]; // required. the parameter is string label = _ trackArr [1]; // optional, the parameter is a string}/** when there are more than two or more characters, * such as data-track = "songli. BTN. secured transaction ", executed _ hmt. push (['_ trackevent', 'songli', 'btn', 'secured bucket']); * data-track = "songli. BTN. secured Transactions. 1002 ", run _ hmt. push (['_ trackevent', 'songli', 'btn', 'secured transactions. 1002 ']); **/else {category = _ trackArr. shift (); // required. the parameter is string action = _ trackArr. shift (); // required. the parameter is string label = _ trackArr. join ('. '); // optional, The parameter is a 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.doc umentElement; addEvent (_ body, "click", function (event) {var event = event? Event: window. event; var target = event. srcElement? Event. srcElement: event.tar get; if (target. getAttribute ('data-track') {doMonitorForDataTrack (target);} else if (target. nodeName = "IMG" & target. parentNode. getAttribute ('data-track') {doMonitorForDataTrack (target. parentNode );}});})();

Browsing track. you can click the button to write it to mongo on the server side. form js listens to dom.

These statistics are usually used to identify user ID values in cookies. The user's operation record data should be immediately sent to the statistics server, and there is no need to save it locally before sending it. This type of data volume is small and will not cause congestion. As for how to send the data, most of them are simply loading an image as you understand, and passing data through parameters.

"When the web page is closed, the server will be sent again." This is a problem. when the browser is closed, your JS will basically be unable to do anything.

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.