Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(function() {
var clickData = [];
var _id;
var _tag;
var analyticsData = {};
var config = {
url : 'DATA_POST_URL'
};
var clickHandler = function(event) {
console.log(event);
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
var cdata = {
isTrusted : event.isTrusted,
clientX : event.clientX,
clientY : event.clientY,
layerX : event.layerX,
layerY : event.layerY,
offsetX: event.offsetX,
offsetY: event.offsetY,
pageX : event.pageX,
pageY: event.pageY,
target : event.target.localName,
className : event.target.className,
id : event.target.id,
x : event.x,
y : event.y,
windowW : w,
windowH : h
};
clickData.push(cdata);
};
var unloadHandler = function(event) {
//Push Data to server.
// navigator.sendBeacon('/website', data);
analyticsData.clickData = clickData;
alert(analyticsData);
pushData(config.url, analyticsData);
}
var pushData = (url, data) => {
// console.log(data);
// return 0;
console.info("Pushing to server : " + data);
alert(data);
if(config.useBeacon) {
window.navigator.sendBeacon(url, JSON.stringify(data));
console.log("Beacon");
return 0;
}
else {
// if("withCredentials" in new window.XMLHttpRequest()) {
// }
if("XMLHttpRequest" in window) {
var req = new window.XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader('Content-Type', "application/json");
req.send(JSON.stringify(data));
return 0;
}
else {
return 0;
}
}
}
cookie = (function() {
var set = function(name, value, seconds) {
var expires = seconds ? '; expires=' + new Date(new Date().getTime() + seconds * 1000).toGMTString() : '';
document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=/';
};
var map = function() {
var map = {};
var kvs = document.cookie.split('; ');
for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i].split('=');
map[kv[0]] = decodeURIComponent(kv[1]);
}
return map;
};
var get = function(name) {
return map()[name];
};
var remove = function(name) {
set(name, '', -1);
};
return {
set: set,
get: get,
remove: remove,
map: map
};
})();
var Opm = function() {
};
Opm.prototype.getReferrer = function() {
return document.referrer;
};
Opm.prototype.init = function() {
if("sendBeacon" in window.navigator) {
config.useBeacon = true;
}
else {
config.useBeacon = false;
}
window.opm('hook');
analyticsData.urlParams = window.opm('getUrlParams');
analyticsData.referrer = window.opm('getReferrer');
analyticsData.browserInfo = window.opm('getBrowserData');
};
Opm.prototype.hook = function() {
if("addEventListener" in window) {
window.addEventListener('click', clickHandler, false);
window.addEventListener('beforeunload', unloadHandler, false);
window.addEventListener('unload', unloadHandler, false);
}
window.onunload = unloadHandler;
window.onbeforeunload = unloadHandler;
};
Opm.prototype.pageView = function(testArg) {
//Submit page view and set cookie.
if(cookie.get("opm-analytics") == "visited") {
cookie.set("opm-analytics", "returning", 30 * 60);
}
else {
cookie.set("opm-analytics", "visited",30 * 60);
}
return testArg;
};
Opm.prototype.getBrowserData = function() {
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) <
(verOffset=nAgt.lastIndexOf('/')) )
{
browserName = nAgt.substring(nameOffset,verOffset);
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}
var txt = {
BrowserName : browserName,
BrowserVersion : nVer,
CookiesEnabled : navigator.cookieEnabled,
BrowserLanguage : navigator.language,
MajorVersion : majorVersion,
Platform : OSName,
UserAgent : nAgt
};
// console.log(txt);
// pushData(config.url, txt);
return txt;
};
Opm.prototype.getUrlParams = function() {
var url = window.location.href;
var protocol = window.location.protocol;
var hostname = window.location.hostname;
var searchQuery = window.location.search;
//Do things with URL
return getParams(searchQuery);
};
function getParams (str) {
var queryString = str || window.location.search || '';
var keyValPairs = [];
var params = {};
queryString = queryString.replace(/.*?\?/,"");
if (queryString.length)
{
keyValPairs = queryString.split('&');
for (pairNum in keyValPairs)
{
var key = keyValPairs[pairNum].split('=')[0];
if (!key.length) continue;
if (typeof params[key] === 'undefined')
params[key] = {};
params[key] = keyValPairs[pairNum].split('=')[1];
// params[key] = [];
// params[key].push(keyValPairs[pairNum].split('=')[1]);
}
}
return params;
}
Opm.prototype.track = function(id) {
var ele = document.getElement(id);
//var inputs = ele.getElementsByTagName("input");
ele.onsubmit = trackElement;
};
var trackElement = function(event) {
var inputs = event.target.getElementById("input");
var values = "";
for(var input of inputs) {
if(input.type == "hidden" || input.type == "password") {
//don't log input types.
} else {
values += input._id + ":" + input.value + " ";
}
}
console.log(values);
values = values.trim();
// pushData(config.url, values);
}
function getElement (id) {
if ("getElementById" in document) {
return document.getElementById(id);
} else if ("all" in document) {
return window.document.all[id];
} else if ("layers" in document) {
return window.document.layers[id];
}
}
var funcList = new Opm();
window.opm = function() {
if(arguments.length == 0) {
var time = new Date();
console.log(this.time);
} else {
// console.log(arguments);
var args = Array.from(arguments);
//Call this functions as opm('functionName', ['argument1', 'argument2']);
var functionName = args.splice(0,1)[0];
// console.log(functionName);
return funcList[functionName].apply(this, args);
}
};
// mocha.run();
})();
opm = window.opm;
window.opm('init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment