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

Tag support #25

Merged
merged 4 commits into from Jul 24, 2015
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Send requests through XMLHttpRequest

  • Loading branch information
varshneyjayant committed Jul 20, 2015
commit 0896755bd16f4b74961958d2cd676cb0552588cb
@@ -1,10 +1,9 @@
(function(window, document) {
var LOGGLY_INPUT_PREFIX = 'http' + ( ('https:' === document.location.protocol ? 's' : '') ) + '://',
LOGGLY_COLLECTOR_DOMAIN = 'logs-01.loggly.com',
LOGGLY_INPUT_SUFFIX = '.gif?',
LOGGLY_SESSION_KEY = 'logglytrackingsession',
LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1;

LOGGLY_COLLECTOR_DOMAIN = 'logs-01.loggly.com',
LOGGLY_SESSION_KEY = 'logglytrackingsession',
LOGGLY_SESSION_KEY_LENGTH = LOGGLY_SESSION_KEY.length + 1;

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) {
@@ -16,52 +15,58 @@
function LogglyTracker() {
this.key = false;
this.sendConsoleErrors = false;
}
this.tag = 'jslogger';
}

function setKey(tracker, key) {
tracker.key = key;
tracker.setSession();
setInputUrl(tracker);
}

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

function setSendConsoleError(tracker, sendConsoleErrors) {
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({
category: 'BrowserJsException',
exception: {
message: msg,
url: url,
lineno: line,
colno: col,
}
});
var _onerror = window.onerror;
//send console error messages to Loggly
window.onerror = function (msg, url, line, col){
tracker.push({
category: 'BrowserJsException',
exception: {
message: msg,
url: url,
lineno: line,
colno: col,
}
});

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

function setInputUrl(tracker) {
tracker.inputUrl = LOGGLY_INPUT_PREFIX
+ (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
+ '/inputs/'
+ tracker.key
+ LOGGLY_INPUT_SUFFIX;
+ (tracker.logglyCollectorDomain || LOGGLY_COLLECTOR_DOMAIN)
+ '/inputs/'
+ tracker.key
+ '/tag/'
+ tracker.tag;
}

LogglyTracker.prototype = {
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) {
this.session_id = uuid();
@@ -75,67 +80,74 @@
if( !data || !(type === 'object' || type === 'string') ) {
return;
}

var self = this;


if(type === 'string') {

if(type === 'string') {
data = {
'text': data
};
} else {
} else {
if(data.logglyCollectorDomain) {
self.logglyCollectorDomain = data.logglyCollectorDomain;
return;
}

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

if(data.logglyKey) {
}


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

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

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

if(!self.key) {
return;
}

self.track(data);


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

try {
var im = new Image(),
q = 'PLAINTEXT=' + encodeURIComponent(JSON.stringify(data));
im.src = this.inputUrl + q;
} catch (ex) {
//creating an asynchronous XMLHttpRequest
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', this.inputUrl, true); //true for asynchronous request
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);
console.log("Failed log data:", data);
}
}
},
/**
* These cookie functions are not a global utilities. It is for purpose of this tracker only
*/
* 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);
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);
@@ -152,7 +164,7 @@

if(existing && existing.length ) {
var i = 0,
eLength = existing.length;
eLength = existing.length;
for(i = 0; i < eLength; i++) {
tracker.push(existing[i]);
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.