diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0d9691 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Created by .ignore support plugin (hsz.mobi) +.idea +node_modules \ No newline at end of file diff --git a/src/loggly.tracker-2.1.min.js b/src/loggly.tracker-2.1.min.js index 01760d0..2a5a92a 100644 --- a/src/loggly.tracker-2.1.min.js +++ b/src/loggly.tracker-2.1.min.js @@ -1,2 +1,2 @@ -!function(a,b){function c(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=16*Math.random()|0,c="x"==a?b:3&b|8;return c.toString(16)})}function d(){this.key=!1,this.sendConsoleErrors=!1,this.tag="jslogger",this.useDomainProxy=!1}function e(a,b){a.key=b,a.setSession(),i(a)}function f(a,b){a.tag=b}function g(a,b){a.useDomainProxy=b,i(a)}function h(b,c){if(b.sendConsoleErrors=c,b.sendConsoleErrors===!0){var d=a.onerror;a.onerror=function(c,e,f,g){b.push({category:"BrowserJsException",exception:{message:c,url:e,lineno:f,colno:g}}),d&&"function"==typeof d&&d.apply(a,arguments)}}}function i(b){1==b.useDomainProxy?b.inputUrl=j+a.location.host+"/"+n+"/inputs/"+b.key+"/tag/"+b.tag:b.inputUrl=j+(b.logglyCollectorDomain||k)+"/inputs/"+b.key+"/tag/"+b.tag}var j="http"+("https:"===b.location.protocol?"s":"")+"://",k="logs-01.loggly.com",l="logglytrackingsession",m=l.length+1,n="loggly";d.prototype={setSession:function(a){a?(this.session_id=a,this.setCookie(this.session_id)):this.session_id||(this.session_id=this.readCookie(),this.session_id||(this.session_id=c(),this.setCookie(this.session_id)))},push:function(a){var b=typeof a;if(a&&("object"===b||"string"===b)){var c=this;if("string"===b)a={text:a};else{if(a.logglyCollectorDomain)return void(c.logglyCollectorDomain=a.logglyCollectorDomain);if(void 0!==a.sendConsoleErrors&&h(c,a.sendConsoleErrors),a.tag&&f(c,a.tag),a.useDomainProxy&&g(c,a.useDomainProxy),a.logglyKey)return void e(c,a.logglyKey);if(a.session_id)return void c.setSession(a.session_id)}c.key&&c.track(a)}},track:function(b){b.sessionId=this.session_id;try{var c=new XMLHttpRequest;c.open("POST",this.inputUrl,!0),c.setRequestHeader("Content-Type","text/plain"),c.send(JSON.stringify(b))}catch(d){a&&a.console&&"function"==typeof a.console.log&&(console.log("Failed to log to loggly because of this exception:\n"+d),console.log("Failed log data:",b))}},readCookie:function(){var a=b.cookie,c=a.indexOf(l);if(0>c)return!1;var d=a.indexOf(";",c+1);return d=0>d?a.length:d,a.slice(c+m,d)},setCookie:function(a){b.cookie=l+"="+a}};var o=a._LTracker,p=new d;if(o&&o.length){var q=0,r=o.length;for(q=0;r>q;q++)p.push(o[q])}a._LTracker=p,a.LogglyTracker=d}(window,document); +!function(a,b){function c(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(a){var b=16*Math.random()|0,c="x"==a?b:3&b|8;return c.toString(16)})}function d(){this.key=!1,this.sendConsoleErrors=!1,this.tag="jslogger",this.useDomainProxy=!1}function e(a,b){a.key=b,a.setSession(),i(a)}function f(a,b){a.tag=b}function g(a,b){a.useDomainProxy=b,i(a)}function h(b,c){if(b.sendConsoleErrors=c,b.sendConsoleErrors===!0){var d=a.onerror;a.onerror=function(c,e,f,g){b.push({category:"BrowserJsException",exception:{message:c,url:e,lineno:f,colno:g}}),d&&"function"==typeof d&&d.apply(a,arguments)}}}function i(b){1==b.useDomainProxy?b.inputUrl=j+a.location.host+"/"+n+"/inputs/"+b.key+"/tag/"+b.tag:b.inputUrl=j+(b.logglyCollectorDomain||k)+"/inputs/"+b.key+"/tag/"+b.tag}var j="http"+("https:"===b.location.protocol?"s":"")+"://",k="logs-01.loggly.com",l="logglytrackingsession",m=l.length+1,n="loggly";d.prototype={setSession:function(a){a?(this.session_id=a,this.setCookie(this.session_id)):this.session_id||(this.session_id=this.readCookie(),this.session_id||(this.session_id=c(),this.setCookie(this.session_id)))},push:function(a){var b=typeof a;if(a&&("object"===b||"string"===b)){var c=this;if("string"===b)a={text:a};else{if(a.logglyCollectorDomain)return void(c.logglyCollectorDomain=a.logglyCollectorDomain);if(void 0!==a.sendConsoleErrors&&h(c,a.sendConsoleErrors),a.tag&&f(c,a.tag),a.useDomainProxy&&g(c,a.useDomainProxy),a.logglyKey)return void e(c,a.logglyKey);if(a.session_id)return void c.setSession(a.session_id)}c.key&&c.track(a)}},track:function(b,c){b.sessionId=this.session_id;var d=this,e=0;c&&(e=c);var f;try{var g=new XMLHttpRequest;g.open("POST",this.inputUrl,!0),g.setRequestHeader("Content-Type","text/plain"),g.send(JSON.stringify(b)),g.onreadystatechange=function(){4===g.readyState&&0===g.status?e<=12?(console.log("Failed to log to loggly because of a network condition."),console.log("Will attempt to log again in 5 seconds."),f=setTimeout(function(){d.track(b,e+1)},5e3)):(console.log("Loggly has attempted to log this data for the last minute and failed. Ceasing attempts to log."),console.log("Failed log data:",b),clearTimeout(f)):200!==g.status&&304!==g.status||f&&(console.log("Successfully logged to loggly."),clearTimeout(f),e=0)}}catch(c){a&&a.console&&"function"==typeof a.console.log&&(console.log("Failed to log to loggly because of this exception:\n"+c),console.log("Failed log data:",b))}},readCookie:function(){var a=b.cookie,c=a.indexOf(l);if(c<0)return!1;var d=a.indexOf(";",c+1);return d=d<0?a.length:d,a.slice(c+m,d)},setCookie:function(a){b.cookie=l+"="+a}};var o=a._LTracker,p=new d;if(o&&o.length){var q=0,r=o.length;for(q=0;q 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){ - tracker.push({ + window.onerror = function (msg, url, line, col) { + tracker.push({ category: 'BrowserJsException', exception: { message: msg, @@ -52,111 +52,146 @@ colno: col, } }); - + 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, attempts) { // inject session id data.sessionId = this.session_id; - + + var self = this; + + //track number of attempts to publish + var attemptCounter = 0; + if(attempts) { + attemptCounter = attempts; + } + + var postTimeout; + 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)); + xmlHttp.onreadystatechange = function() { + if(xmlHttp.readyState === 4 && xmlHttp.status === 0) { + if(attemptCounter <= 12) { + console.log("Failed to log to loggly because of a network condition."); + console.log("Will attempt to log again in 5 seconds."); + + postTimeout = setTimeout(function() { + self.track(data, attemptCounter + 1) + }, 5000); + } else { + console.log("Loggly has attempted to log this data for the last minute and failed. Ceasing" + + " attempts to log."); + console.log("Failed log data:", data); + + clearTimeout(postTimeout); + } + } else if (xmlHttp.status === 200 || xmlHttp.status === 304) { + // It will only run this if there was a previous failure + if(postTimeout) { + console.log("Successfully logged to loggly."); + clearTimeout(postTimeout); + attemptCounter = 0; + } + } + }; + } catch (ex) { if (window && window.console && typeof window.console.log === 'function') { console.log("Failed to log to loggly because of this exception:\n" + ex); @@ -165,38 +200,38 @@ } }, /** - * These cookie functions are not a global utilities. It is for purpose of this tracker only - */ - readCookie: function() { + * These cookie functions are not a global utilities. It is for purpose of this tracker only + */ + 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);