I am currently learning Javascript, PHP, and OPP. I decided to create a simple script for my company I work for. trackUser logs unique users who visit the website. It keeps track of; screen resolution, screen viewport, browser + version, operating system, date visited, and IP Address.
github: [url]https://github.com/peezybro/trackUser
Here is the code I wrote for the script.
[CODE]var trackUser = {
init: function(){
this.resolution = this.detectResolution();
this.viewPort = this.detectView();
this.browser = BrowserDetect.browser + ” ” + BrowserDetect.version;
this.OS = BrowserDetect.OS;
if (this.detectCookie() == false){
this.setCookie();
this.ajaxCall();
} else {
console.log(‘User is not unique, kill trackUser’); //debug
return;
};
},
detectResolution: function(){
var width = window.screen.width,
height = window.screen.height,
res = width + “x” + height;
console.log(‘detecting resolution’); //debug
return res;
},
detectView: function(){
var width = window.screen.availWidth,
height = window.screen.availHeight,
view = width + “x” + height;
console.log(‘detecting viewport’); //debug
return view;
},
detectCookie: function(){
var check = $.cookie(‘uniqueUser’);
if (check == null) {
console.log(‘cookie is not set’); //debug
return false; //cookie is not set
} else {
console.log(‘cookie is set’); //debug
return true; //cookie is set
};
},
setCookie: function(){
$.cookie(‘uniqueUser’,’set’, { expires: 1 });
console.log(‘setting cookie’); //debug
},
ajaxCall: function(){
console.log(‘called ajaxCall function’); //debug
$.ajax({
type: “POST”,
url: “saveData.php”,
data: {
resolution: this.resolution,
viewport: this.viewPort,
browser: this.browser,
os: this.OS
},
success: function(){
console.log(‘success’); //debug
return;
},
error: function(){
console.log(‘Ajax had a error while sending data to server, Please check code.’);
}
});
}
};
trackUser.init();
I wrote this as a learning project so I am sure there is properly problems with my code. Please let me know how i did, and any tips will help ?