Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactor #47

Merged
merged 12 commits into from Aug 16, 2017

refactor remove unwanted spaces

 - convert tab into space
 - format code
  • Loading branch information
Shweta Jain authored and Shwetajain148 committed Aug 8, 2017
commit c87c47149e6e3055b65d1370fbb0c0935acff41e
@@ -1,3 +1,3 @@
require('./src/loggly.tracker');
module.exports._LTracker = window._LTracker;
module.exports.LogglyTracker = window.LogglyTracker;
module.exports.LogglyTracker = window.LogglyTracker;
@@ -1,45 +1,45 @@
(function(window, document) {
var LOGGLY_INPUT_PREFIX = 'http' + ( ('https:' === document.location.protocol ? 's' : '') ) + '://',
LOGGLY_COLLECTOR_DOMAIN = 'logs-01.loggly.com',
LOGGLY_SESSION_KEY = 'logglytrackingsession',
LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1,
LOGGLY_PROXY_DOMAIN = 'loggly';
(function (window, document) {
var LOGGLY_INPUT_PREFIX = 'http' + (('https:' === document.location.protocol ? 's' : '')) + '://',
LOGGLY_COLLECTOR_DOMAIN = 'logs-01.loggly.com',
LOGGLY_SESSION_KEY = 'logglytrackingsession',
LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1,
LOGGLY_PROXY_DOMAIN = 'loggly';

function uuid() {
// lifted from here -> http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
});
}

function LogglyTracker() {
this.key = false;
this.sendConsoleErrors = false;
this.sendConsoleErrors = false;
this.tag = 'jslogger';
this.useDomainProxy = false;
}

function setKey(tracker, key) {
tracker.key = key;
tracker.setSession();
setInputUrl(tracker);
}
function setTag(tracker, tag){
tracker.tag = tag;
}
function setDomainProxy(tracker, useDomainProxy){

function setTag(tracker, tag) {
tracker.tag = tag;
}

function setDomainProxy(tracker, useDomainProxy) {
tracker.useDomainProxy = useDomainProxy;
//refresh inputUrl value
setInputUrl(tracker);
}

function setSendConsoleError(tracker, sendConsoleErrors) {
tracker.sendConsoleErrors = sendConsoleErrors;
if(tracker.sendConsoleErrors === true){
tracker.sendConsoleErrors = sendConsoleErrors;

if (tracker.sendConsoleErrors === true) {
var _onerror = window.onerror;
//send console error messages to Loggly
window.onerror = function (msg, url, line, col, err){
@@ -53,111 +53,111 @@
stack: err ? err.stack : 'n/a',
}
});

if (_onerror && typeof _onerror === 'function') {
_onerror.apply(window, arguments);
}
};
}
}

function setInputUrl(tracker) {
if(tracker.useDomainProxy == true){
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ window.location.host
+ '/'
+ LOGGLY_PROXY_DOMAIN
+ '/inputs/'
+ tracker.key
+ '/tag/'
+ tracker.tag;

if (tracker.useDomainProxy == true) {
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ window.location.host
+ '/'
+ LOGGLY_PROXY_DOMAIN
+ '/inputs/'
+ tracker.key
+ '/tag/'
+ tracker.tag;
}
else{
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
+ '/inputs/'
+ tracker.key
+ '/tag/'
+ tracker.tag;
else {
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
+ '/inputs/'
+ tracker.key
+ '/tag/'
+ tracker.tag;
}
}

LogglyTracker.prototype = {
setSession: function(session_id) {
if(session_id) {
setSession: function (session_id) {
if (session_id) {
this.session_id = session_id;
this.setCookie(this.session_id);
} else if(!this.session_id) {
} else if (!this.session_id) {
this.session_id = this.readCookie();
if(!this.session_id) {
if (!this.session_id) {
this.session_id = uuid();
this.setCookie(this.session_id);
}
}
},
push: function(data) {
push: function (data) {
var type = typeof data;
if( !data || !(type === 'object' || type === 'string') ) {

if (!data || !(type === 'object' || type === 'string')) {
return;
}

var self = this;
if(type === 'string') {


if (type === 'string') {
data = {
'text': data
};
} else {
if(data.logglyCollectorDomain) {
if (data.logglyCollectorDomain) {
self.logglyCollectorDomain = data.logglyCollectorDomain;
return;
}
if(data.sendConsoleErrors !== undefined) {
setSendConsoleError(self, data.sendConsoleErrors);

if (data.sendConsoleErrors !== undefined) {
setSendConsoleError(self, data.sendConsoleErrors);
}
if(data.tag) {

if (data.tag) {
setTag(self, data.tag);
}
if(data.useDomainProxy){

if (data.useDomainProxy) {
setDomainProxy(self, data.useDomainProxy);
}
if(data.logglyKey) {

if (data.logglyKey) {
setKey(self, data.logglyKey);
return;
}
if(data.session_id) {

if (data.session_id) {
self.setSession(data.session_id);
return;
}
}
if(!self.key) {

if (!self.key) {
return;
}

self.track(data);


},
track: function(data) {
track: function (data) {
// inject session id
data.sessionId = this.session_id;

try {
//creating an asynchronous XMLHttpRequest
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', this.inputUrl, true); //true for asynchronous request
xmlHttp.setRequestHeader('Content-Type', 'text/plain');
xmlHttp.send(JSON.stringify(data));
xmlHttp.open('POST', this.inputUrl, true); //true for asynchronous request
xmlHttp.setRequestHeader('Content-Type', 'text/plain');
xmlHttp.send(JSON.stringify(data));

} catch (ex) {
if (window && window.console && typeof window.console.log === 'function') {
console.log("Failed to log to loggly because of this exception:\n" + ex);
@@ -168,36 +168,36 @@
/**
* These cookie functions are not a global utilities. It is for purpose of this tracker only
*/
readCookie: function() {
readCookie: function () {
var cookie = document.cookie,
i = cookie.indexOf(LOGGLY_SESSION_KEY);
if(i < 0) {
i = cookie.indexOf(LOGGLY_SESSION_KEY);
if (i < 0) {
return false;
} else {
} else {
var end = cookie.indexOf(';', i + 1);
end = end < 0 ? cookie.length : end;
return cookie.slice(i + LOGGLY_SESSION_KEY_LENGTH, end);
}
},
setCookie: function(value) {
setCookie: function (value) {
document.cookie = LOGGLY_SESSION_KEY + '=' + value;
}
};

var existing = window._LTracker;

var tracker = new LogglyTracker();
if(existing && existing.length ) {

if (existing && existing.length) {
var i = 0,
eLength = existing.length;
for(i = 0; i < eLength; i++) {
eLength = existing.length;
for (i = 0; i < eLength; i++) {
tracker.push(existing[i]);
}
}

window._LTracker = tracker; // default global tracker

window.LogglyTracker = LogglyTracker; // if others want to instantiate more than one tracker

})(window, document);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.