224 lines
7.6 KiB
JavaScript
224 lines
7.6 KiB
JavaScript
/*! evtrack -- UI module */
|
|
(function(f) {
|
|
var g = f.document;
|
|
var c = "mousedown mouseup mousemove mouseover mouseout mousewheel ";
|
|
c += "touchstart touchend touchmove keydown keyup keypress ";
|
|
c += "click dblclick scroll change select submit reset contextmenu cut copy paste";
|
|
var d = "load unload beforeunload blur focus resize error online offline";
|
|
c = c.split(" ");
|
|
d = d.split(" ");
|
|
var a = c.concat(d);
|
|
var b = " "
|
|
, e = "|||";
|
|
var h = 0
|
|
, k = 0
|
|
, j = [];
|
|
var i = {
|
|
settings: {
|
|
postServer: "//my.server.org/save.script",
|
|
postInterval: 30,
|
|
regularEvents: "*",
|
|
pollingEvents: "",
|
|
pollingMs: 150,
|
|
taskName: "evtrack",
|
|
callback: null,
|
|
saveAttributes: true,
|
|
layoutType: "liquid",
|
|
debug: false
|
|
},
|
|
record: function(l) {
|
|
k = new Date().getTime();
|
|
for (var m in i.settings) {
|
|
if (l.hasOwnProperty(m) && l[m] !== null) {
|
|
i.settings[m] = l[m]
|
|
}
|
|
}
|
|
i.log("Recording starts...", k, i.settings);
|
|
i.addEventListeners();
|
|
setTimeout(function() {
|
|
i.initNewData(true)
|
|
}, i.settings.postInterval * 1000)
|
|
},
|
|
addEventListeners: function() {
|
|
if (i.settings.regularEvents == "*") {
|
|
i.addCustomEventListeners(a)
|
|
} else {
|
|
i.log("Settings regular events...");
|
|
i.settings.regularEvents = i.settings.regularEvents.split(" ");
|
|
i.addCustomEventListeners(i.settings.regularEvents)
|
|
}
|
|
if (i.settings.pollingEvents == "*") {
|
|
i.addCustomEventListeners(a)
|
|
} else {
|
|
i.log("Settings polling events...");
|
|
i.settings.pollingEvents = i.settings.pollingEvents.split(" ");
|
|
i.addCustomEventListeners(i.settings.pollingEvents)
|
|
}
|
|
var l = (typeof f.onbeforeunload === "function") ? "beforeunload" : "unload";
|
|
TrackLib.Events.add(f, l, i.flush)
|
|
},
|
|
addCustomEventListeners: function(n) {
|
|
i.log("Adding event listeners:", n);
|
|
for (var l = 0; l < n.length; ++l) {
|
|
var m = n[l];
|
|
if (!m) {
|
|
continue
|
|
}
|
|
if (c.indexOf(m) > -1) {
|
|
TrackLib.Events.add(g, m, i.docHandler);
|
|
i.log("Adding document event:", m);
|
|
if (g.attachEvent) {
|
|
if (m == "focus") {
|
|
TrackLib.Events.add(g.body, "focusin", i.winHandler)
|
|
}
|
|
if (m == "blur") {
|
|
TrackLib.Events.add(g.body, "focusout", i.winHandler)
|
|
}
|
|
}
|
|
} else {
|
|
if (d.indexOf(m) > -1) {
|
|
TrackLib.Events.add(f, m, i.winHandler);
|
|
i.log("Adding window event:", m)
|
|
}
|
|
}
|
|
}
|
|
},
|
|
initNewData: function(l) {
|
|
var o = TrackLib.Dimension.getWindowSize()
|
|
, n = TrackLib.Dimension.getDocumentSize()
|
|
, m = "url=" + encodeURIComponent(f.location.href);
|
|
m += "&screenw=" + screen.width;
|
|
m += "&screenh=" + screen.height;
|
|
m += "&winw=" + o.width;
|
|
m += "&winh=" + o.height;
|
|
m += "&docw=" + n.width;
|
|
m += "&doch=" + n.height;
|
|
m += "&info=" + encodeURIComponent(j.join(e));
|
|
m += "&task=" + encodeURIComponent(i.settings.taskName);
|
|
m += "&action=init";
|
|
i.send({
|
|
async: l,
|
|
postdata: m,
|
|
callback: i.setUserId
|
|
});
|
|
j = []
|
|
},
|
|
setUserId: function(l) {
|
|
h = parseInt(l.responseText);
|
|
i.log("setUserId:", h);
|
|
if (h) {
|
|
setInterval(function() {
|
|
i.appendData(true)
|
|
}, i.settings.postInterval * 1000)
|
|
}
|
|
},
|
|
appendData: function(l) {
|
|
var m = "uid=" + h;
|
|
m += "&info=" + encodeURIComponent(j.join(e));
|
|
m += "&task=" + encodeURIComponent(i.settings.taskName);
|
|
m += "&action=append";
|
|
i.send({
|
|
async: l,
|
|
postdata: m
|
|
});
|
|
j = []
|
|
},
|
|
send: function(l) {
|
|
l.url = i.settings.postServer;
|
|
TrackLib.XHR.sendAjaxRequest(l)
|
|
},
|
|
docHandler: function(l) {
|
|
if (l.type.indexOf("touch") > -1) {
|
|
i.touchHandler(l)
|
|
} else {
|
|
i.eventHandler(l)
|
|
}
|
|
},
|
|
winHandler: function(l) {
|
|
i.eventHandler(l)
|
|
},
|
|
eventHandler: function(q) {
|
|
q = TrackLib.Events.fix(q);
|
|
var n = new Date().getTime()
|
|
, m = q.type
|
|
, o = true;
|
|
if (i.settings.pollingMs > 0 && i.settings.pollingEvents.indexOf(m) > -1) {
|
|
o = (n - k >= i.settings.pollingMs)
|
|
}
|
|
if (o) {
|
|
var s = i.getMousePos(q)
|
|
, r = TrackLib.XPath.getXPath(q.target)
|
|
, l = i.settings.saveAttributes ? TrackLib.Util.serializeAttrs(q.target) : "{}"
|
|
, p = {};
|
|
if (typeof i.settings.callback === "function") {
|
|
p = i.settings.callback(q)
|
|
}
|
|
i.fillInfo(q.id, n, s.x, s.y, m, r, l, JSON.stringify(p));
|
|
k = n
|
|
}
|
|
},
|
|
touchHandler: function(n) {
|
|
n = TrackLib.Events.fix(n);
|
|
var m = n.changedTouches;
|
|
if (m) {
|
|
for (var l = 0, o; l < m.length; ++l) {
|
|
o = m[l];
|
|
o.type = n.type;
|
|
i.eventHandler(o)
|
|
}
|
|
}
|
|
},
|
|
getMousePos: function(m) {
|
|
m = TrackLib.Events.fix(m);
|
|
var l = 0
|
|
, n = 0;
|
|
if (m.pageX || m.pageY) {
|
|
l = m.pageX;
|
|
n = m.pageY
|
|
} else {
|
|
if (m.clientX || m.clientY) {
|
|
l = m.clientX + g.body.scrollLeft + g.documentElement.scrollLeft;
|
|
n = m.clientY + g.body.scrollTop + g.documentElement.scrollTop
|
|
}
|
|
}
|
|
if (!l || l < 0) {
|
|
l = 0
|
|
}
|
|
if (!n || n < 0) {
|
|
n = 0
|
|
}
|
|
return {
|
|
x: l,
|
|
y: n
|
|
}
|
|
},
|
|
fillInfo: function() {
|
|
var l = [].slice.apply(arguments);
|
|
j.push(l.join(b));
|
|
i.log(l)
|
|
},
|
|
flush: function(m) {
|
|
i.log("Flushing data...", h);
|
|
var l;
|
|
for (l = 0; l < c.length; ++l) {
|
|
TrackLib.Events.remove(g, c[l], i.docHandler)
|
|
}
|
|
for (l = 0; l < d.length; ++l) {
|
|
TrackLib.Events.remove(f, d[l], i.winHandler)
|
|
}
|
|
if (h) {
|
|
i.appendData(false)
|
|
} else {
|
|
i.initNewData(false)
|
|
}
|
|
},
|
|
log: function() {
|
|
if (i.settings.debug && typeof console.log === "function") {
|
|
console.log.apply(console, arguments)
|
|
}
|
|
}
|
|
};
|
|
f.TrackUI = i
|
|
}
|
|
)(this);
|