The act of collecting user privacy is no longer a new topic (collecting is no longer a peek), and even G and MS show off repeatedly. Of course, due to reasons, enterprises can not understand users'behavior through various means, which can be used as a basis for decision support; usually manifested as cross-disciplinary collection(Browser, Intelligence System, OS Etc.)), business, product data collection, of course, collection methods are also different.The following shows how client-side scripting can be used to collect Web site user behavior data to judge page loading, execution time, user retention time, page submission rate, success rate, page errors, etc. (This example is simple to implement and can be reconstructed appropriately for specific complex business data collection)And transformation).
1. Collect basic data
Base Data Coverage
(1) Business Class: Business Flow Page Address, User Stay Time, Open Number,
Session ID, Client IP, Business Flow Steps, etc.
(2) Auxiliary classes: browser, OS, COOKIE, etc.
(3) Example
- /*
- Record Construction Base Data
- */
- var Logger = {
- Debug: false,
- UserControl: 'inputUser',
- HistoryControl: 'inputHistory',
- TimerControl: 'inputTimer',
- GetUser: function() {
- if (!$f(this.UserControl)) return ",,";
- return $f(this.UserControl).value;
- },
- /*-- attribute --*/
- Guid: function() {
- return this.GetUser().split(',')[0];
- },
- SessionID: function() {
- return this.GetUser().split(',')[1];
- },
- GetStep: function() {
- return this.GetUser().split(',')[2];
- },
- ProcessTime: function() {
- if (!$f(this.TimerControl)) return "0";
- return $f(this.TimerControl).value;
- },
- AppSys: 1, //Different System Numbers
- Environment: '', //Environment.Dev: Develop Dev, Test, Official
- IsNewSite: 1,
- //Whether history returns
- IsHistory: function() {
- if (!$f(this.HistoryControl)) return false;
- if ($f(this.HistoryControl).value.length > 0)
- return true;
- //if (this.IsSuccReturn()) return true; //Successful return non-historic
- return false;
- },
- IsStep2History: function() { //Whether to return to history for step 2
- if (!$f(this.HistoryControl)) return false;
- var history = $f(this.HistoryControl).value;
- if (history.length == 0)
- return false;
- if (history.split(',').length > 1)
- return true;
- return false;
- },
- //Whether to return for page reload
- IsReturn: function() {
- if (typeof getUrlParam != "function") return false;
- var para = getUrlParam("return");
- if (para == "1") return true;
- return false;
- },
- //Successfully returned
- IsSuccReturn: function() {
- var para = getUrlParam("succret");
- if (para == "1") return true;
- return false;
- },
- //tracetype,guid,sessionid,processtime,description
- WriteStepLog: function() {
- var argc = arguments.length;
- var traceType = (argc > 0) ? arguments[0] : "";
- var guid = (argc > 1) ? arguments[1] : "";
- var sessionID = (argc > 2) ? arguments[2] : "";
- var processTime = (argc > 3) ? (arguments[3] == "" ? "0" : arguments[3]) : "0";
- var description = (argc > 4) ? arguments[4] : "";
- var url = (argc > 5) ? arguments[5] : "";
- /*with (Trace.Parameter)
- {
- TraceType = traceType;
- Guid = guid;
- SessionID = sessionID;
- PageUrl = window.location.href;
- ProcessTime = processTime;
- //set const value
- AppSys = 1;
- Environment = Environment.Dev; //Offical
- IsNewSite = 1;
- Description = description;
- }*/
- Trace.Parameter.TraceType = traceType;
- Trace.Parameter.Guid = guid;
- Trace.Parameter.SessionID = sessionID;
- if (url.length == 0) {
- url = window.location.href;
- //alert("self:" + window.location.href + ",refer:" + self.document.referrer);
- if (url.toLowerCase().indexOf('errorpage.aspx') > -1 && traceType.indexOf('ret') == -1) {
- if (document.referrer != null && document.referrer != "") url = document.referrer;
- }
- }
- Trace.Parameter.PageUrl = url;
- Trace.Parameter.ProcessTime = processTime;
- Trace.Parameter.AppSys = this.AppSys;
- if (this.Environment.length == 0) this.Environment = Environment.Official;
- var curUrl = window.location.href.toLowerCase();
- if (curUrl.indexOf('https://') > -1) {
- this.Environment = this.Environment.replace('http://', 'https://');
- }
- Trace.Parameter.Environment = this.Environment;
- Trace.Parameter.IsNewSite = this.IsNewSite;
- Trace.Parameter.Description = escape(description);
- if (this.Debug) {
- alert(Trace.Parameter.TraceType + "," + Trace.Parameter.Guid + "," + Trace.Parameter.SessionID + ","
- + Trace.Parameter.ProcessTime + "," + Trace.Parameter.Description);
- }
- Trace.Submit(Trace.Parameter, null, 'img');
- },
- WriteOpenLog: function() {
- try {
- var argc = arguments.length;
- var step = (argc > 0) ? arguments[0] : "";
- var desc = (argc > 1) ? arguments[1] : "";
- if (typeof PTID != "undefined" && PTID.length > 0) {
- desc += ",PTID:" + PTID;
- }
- var loginstep = this.GetStep();
- /*if (this.IsSuccReturn()) { //Successful Return
- Logger.WriteStepLog(Step.succret, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- this.SetTimer();
- }*/
- if (step == "step1" && !this.IsHistory() && typeof loginstep != "undefined" && loginstep.length > 0) { //Logon Return (the first step occurs)
- Logger.WriteStepLog(loginstep, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- }
- else if (step == "step1" && !this.IsHistory() && !this.IsReturn()) //not history back,not page reload
- {
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- }
- else if ((step == "step2" && !this.IsStep2History()) || step == "step3") { //Step 2, Step 3
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- this.SetTimer();
- }
- else if (step == "password" || step == "mobile" || step == "cancelbind") { //No attributes such as historical returns at all
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- this.SetTimer();
- }
- else if (this.IsHistory() || this.IsStep2History() || this.IsReturn()) { //History Return
- Logger.WriteStepLog(step + "ret", this.Guid(), this.SessionID(), "0", desc);
- this.SetTimer();
- }
- else {
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), "0", desc);
- }
- }
- catch (e) {
- }
- },
- WriteSubmitLog: function() {
- try {
- var argc = arguments.length;
- var step = (argc > 0) ? arguments[0] : "";
- var desc = (argc > 1) ? arguments[1] : "";
- var url = (argc > 2) ? arguments[2] : "";
- if (typeof PTID != "undefined" && PTID.length > 0) {
- desc += ",PTID:" + PTID;
- }
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), this.ProcessTime(), desc, url);
- $f(this.HistoryControl).value = "1";
- //set step2
- if (step == "step2submit") {
- $f(this.HistoryControl).value = "1,1";
- }
- this.SetTimer();
- }
- catch (e) {
- }
- },
- SetTimer: function() { //reset timer
- if (Timer && typeof Timer != "undefined") {
- Timer.Reset();
- }
- },
- DirectOpenLog: function() {
- try {
- var argc = arguments.length;
- var step = (argc > 0) ? arguments[0] : "";
- var desc = (argc > 1) ? arguments[1] : "";
- if (typeof PTID != "undefined" && PTID.length > 0) {
- desc += ",PTID:" + PTID;
- }
- this.AppSys = 2;
- Logger.WriteStepLog(step, this.Guid(), this.SessionID(), this.ProcessTime(), desc);
- if (step != Step.step1) {
- this.SetTimer();
- }
- }
- catch (e) {
- }
- }
- };
- var $f = function(name) {
- return document.getElementById(name);
- }
- //Log client script errors
- window.onerror = function GetErrors(error) {
- try {
- var msg;
- for (var i = 0; i < arguments.length; i++) {
- if (i == 0 || i == 2) {
- msg += " | " + arguments[i];
- }
- }
- if (msg.length > 0 && typeof Logger != 'undefined') {
- Logger.WriteStepLog('syserror', '-', '-', '', msg);
- }
- window.onerror = null;
- return true;
- } catch (e) { };
- }
2. Time Statistics
Calculate the user's stay time in the current view by placing a timer on the page.
Example:
- /*--------Timer Script-------------
- *
- * Page Timer Control
- * 1,Timer.BindControl = 'inputTimer';
- * 2,<input id="inputTimer" type="hidden" class="timer" />
- * Use EndTime - StratTime when cookies are not written and timers are not displayed
- */
- var up, down;
- var cmin1, csec1, clock;
- var Timer = {
- Debug: false,
- BindControl: 'inputTimer',
- StartTime: '',
- EndTime: '',
- StartTimer: function() {
- if (!$f(this.BindControl)) return;
- if ($f(this.BindControl).value != "") return;
- //$("#" + this.BindControl).val("");
- cmin1 = 0;
- csec1 = 0;
- //Record each page separately, do not use cookies, block
- // var cookie = GetCookie("Timer");
- // if (cookie) {
- // cmin1 = parseInt(this.Minutes(cookie));
- // csec1 = parseInt(this.Seconds(cookie));
- // DeleteCookie("Timer");
- // }
- // else {
- // cmin1 = csec1 = 0;
- // }
- this.Repeat();
- },
- SetValue: function() {
- var html = $f(this.BindControl).value;
- if (html != null && html.length > 0) SetCookie("Timer", html);
- },
- Minutes: function(data) {
- for (var i = 0; i < data.length; i++) if (data.substring(i, i + 1) == ":") break;
- return (data.substring(0, i));
- },
- Seconds: function(data) {
- for (var i = 0; i < data.length; i++) if (data.substring(i, i + 1) == ":") break;
- return (data.substring(i + 1, data.length));
- },
- Display: function(min, sec) {
- var disp = "";
- if (min <= 9) disp += "0" + min + ":";
- else disp += min + ":";
- if (sec <= 9) disp += "0" + sec;
- else disp += sec;
- return (disp);
- },
- Repeat: function() {
- csec1++;
- if (csec1 == 60) { csec1 = 0; cmin1++; }
- $f(this.BindControl).value = this.Display(cmin1, csec1);
- if (this.Debug) $f("inputDebug").value = this.Display(cmin1, csec1);
- clock = window.setTimeout(function() { Timer.Repeat() }, 1000);
- },
- //Restart Timing
- Reset: function() {
- $f(this.BindControl).value = "";
- window.clearTimeout(clock);
- Timer.StartTimer();
- },
- AddTrigger: function() {
- var list = document.getElementsByTagName("INPUT");
- for (var i = 0; i < list.length; i++) {
- if (list[i].type.toUpperCase() == 'TEXT') {
- if (list[i].addEventListener) {
- list[i].addEventListener("keyup", function() { Timer.StartTimer(); }, false);
- }
- else {
- list[i].attachEvent("onkeyup", function() { Timer.StartTimer(); });
- }
- }
- }
- }
- };
- if (document.all) {
- window.attachEvent("onload", function() { Timer.AddTrigger() });
- }
- else {
- window.addEventListener("load", function() { Timer.AddTrigger() }, false);
- }
- if (Timer.Debug) {
- if (!document.getElementById("inputDebug")) {
- document.write("<input type='text' id='inputDebug' />");
- }
- }
- /* Compatible with two mode settings Cookie*/
- //$(window).unload(function() { Timer.SetValue(); });
- //$("form").submit(function() { Timer.SetValue(); });