const autoenable = false;
console.log("Opolytics Tracking is active.");
function build_http_query(names,data) {
	var string = names[0] + "=" + data[0];
	var i;
	for (i = 1; i < names.length; i++) {
		string += "&" + names[i] + "=" + data[i];
	}
	return string;
}
const site = window.location.href;
const screen_width = window.screen.width;
const screen_orientation = window.orientation;
const platform = navigator.platform;
const plugins = navigator.plugins;
var time_on_site = 0;
window.setInterval(function() {
	time_on_site++;
},1000);
var title;
if(document.getElementsByTagName("title")[0]) title = document.getElementsByTagName("title")[0].innerHTML;
var meta_tags = document.getElementsByTagName("description");
var description;
var i;
for (i = 0; i < meta_tags.length; i++) {
	if(meta_tags[i]) if(meta_tags[i].getAttribute("name")=="description" && meta_tags[i].hasAttribute("content")) description = meta_tags[i].getAttribute("description");
}
description = 0;
var actions = 0;
window.addEventListener("click",function() {
	actions++;
});
// Prepare and send this to server

var xhr = new XMLHttpRequest();
var params = {"site":site,"screen_width":screen_width,"screen_orientation":screen_orientation,"platform":platform,"plugins":plugins,"time_on_site":time_on_site,"title":title,"description":description,"actions":actions};
function opolytics_enable() {
xhr.open("POST","https://" + window.location.host + "/opolytics/tracker.php");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.addEventListener("load",function() {
	console.log("Tracker response is: " + this.responseText);
});
xhr.send(build_http_query(Object.keys(params),Object.values(params)));
window.setTimeout(liveupdate,1000);
window.setInterval(liveupdate,10000);
}
function liveupdate() {
	var lxhr = new XMLHttpRequest();
	lparams = { "referrer":document.referrer,"actions":actions,"time_on_site":time_on_site };
	lxhr.open("POST","https://" + window.location.host + "/opolytics/liveupdate.php?" + Math.random());
	lxhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	lxhr.addEventListener("load",function() {
		console.log("Liveupdate response is: " + this.responseText);
	});
	lxhr.send(build_http_query(Object.keys(lparams),Object.values(lparams)));
}
console.log("Seems to work.");
if(autoenable===true) opolytics_enable();