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

Add Cache Buster to url #17

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -4,16 +4,16 @@ describe("loggly.tracker", function() {
function resetCookie() {
document.cookie = 'logglytrackingsession=; expires=Thu, 01 Jan 1970 00:00:01 GMT';
}

beforeEach(function() {
resetCookie();
_LTracker.setSession();
jasmine.Clock.useMock();

_LTracker.push({'logglyKey': baseLogglyKey})
jasmine.Clock.tick(20);
});

afterEach(function() {
_LTracker.session_id = '';
resetCookie();
@@ -22,148 +22,158 @@ describe("loggly.tracker", function() {
it("exists", function() {
expect(_LTracker).not.toBe(null);
});

it("sets and reads cookies", function() {
var value = 'randomcookievaluelogglytracker';
_LTracker.setCookie(value);
var i = document.cookie.indexOf(value);

expect(document.cookie.indexOf(value)).toBeGreaterThan(-1);
expect(document.cookie.indexOf('logglytrackingsession')).toBeGreaterThan(-1);
expect(_LTracker.readCookie()).toBe(value);
});
it("sets uuid automatically to object and cookie", function() {

it("sets uuid automatically to object and cookie", function() {
var uuid = _LTracker.session_id;

expect(uuid).not.toBe(null);
expect(document.cookie.indexOf(uuid)).toBeGreaterThan(-1);
expect(_LTracker.readCookie()).toBe(uuid);
});
it("sets uuid manually to object and cookie", function() {

it("sets uuid manually to object and cookie", function() {
var initial = '1324123412';

spyOn(_LTracker, 'setCookie').andCallThrough();
_LTracker.setSession(initial);

expect(_LTracker.setCookie).toHaveBeenCalledWith(initial);
expect(_LTracker.session_id).toBe(initial);
expect(document.cookie.indexOf(initial)).toBeGreaterThan(-1);
expect(_LTracker.readCookie()).toBe(initial);

var replacement = '9932483828';
_LTracker.setSession(replacement);
expect(_LTracker.readCookie()).toBe(replacement);

_LTracker.session_id = ''; // reset
});

it("does not auto replace session id", function() {
var uuid = _LTracker.session_id;
_LTracker.setSession(); // second call that should be no op

expect(_LTracker.readCookie()).toBe(uuid);

_LTracker.session_id = '';
});

it("sets loggly key correctly by pushing", function() {
var originalKey = _LTracker.key,
madeupKey = 'madeupkey';

spyOn(_LTracker, 'track');
_LTracker.push({'logglyKey': madeupKey});

jasmine.Clock.tick(20);

var key = _LTracker.key;
expect(_LTracker.key).toBe(madeupKey);
expect(_LTracker.inputUrl).toContain(madeupKey + '.gif');
expect(_LTracker.track).not.toHaveBeenCalled();

_LTracker.push({'logglyKey': originalKey}); // put it back to original state
jasmine.Clock.tick(20);

expect(_LTracker.key).toBe(originalKey);
});

it("sets custom collector domain", function() {
var customDomain = "logglyidontexist.com";
_LTracker.push({'logglyCollectorDomain': customDomain});
_LTracker.push({'logglyKey': _LTracker.key}); // kick it so it sets the collector domain

jasmine.Clock.tick(20);

expect(_LTracker.logglyCollectorDomain).toBe(customDomain);
expect(_LTracker.inputUrl).toContain('http://' + customDomain + '/inputs/');

delete _LTracker.logglyCollectorDomain;
_LTracker.push({'logglyKey': _LTracker.key});
jasmine.Clock.tick(20);

});

it("calls track when pushing normal tracking object", function() {
spyOn(_LTracker, 'track');

var data = {'hoover': 'isabeaver'};

_LTracker.push(data);
jasmine.Clock.tick(20);

expect(_LTracker.track).toHaveBeenCalledWith(data);
});

it("calls track with formatted message if single string is passed", function() {
spyOn(_LTracker, 'track');

var data = 'hooverisabeaver';

_LTracker.push(data);
jasmine.Clock.tick(20);

expect(_LTracker.track).toHaveBeenCalledWith({'text': data});
});

it("does not call track when pushed junk", function() {
spyOn(_LTracker, 'track');

_LTracker.push('');
jasmine.Clock.tick(20);

expect(_LTracker.track).not.toHaveBeenCalled();

_LTracker.push();
jasmine.Clock.tick(20);

expect(_LTracker.track).not.toHaveBeenCalled();
});

it("can instantiate additional loggly tracker", function() {
expect(LogglyTracker).not.toBe(null);

var localTracker = new LogglyTracker(),
madeupKey = 'madeupkey',
lKey = _LTracker.key;

localTracker.push({'logglyKey':madeupKey});
jasmine.Clock.tick(20);

expect(localTracker.key).toBe(madeupKey);
expect(_LTracker.key).not.toBe(madeupKey);
expect(_LTracker.key).toBe(lKey);

spyOn(localTracker, 'track');
spyOn(_LTracker, 'track');

var data = {'randomdata':'random'};

localTracker.push(data);
jasmine.Clock.tick(20);

expect(localTracker.track).toHaveBeenCalledWith(data);
expect(_LTracker.track).not.toHaveBeenCalled()
});

});

it("contains a cachebuster string", function() {
var imageStub = {};
spyOn(window, 'Image').andReturn(imageStub);
spyOn(window.Math, 'random').andReturn(0.1234567890123456);

_LTracker.push('test');
jasmine.Clock.tick(20);

expect(imageStub.src).toContain('&CACHEBUST=1234567890123456');
});
});
@@ -10,28 +10,28 @@
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;
}

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

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({
tracker.push({
category: 'BrowserJsException',
exception: {
message: msg,
@@ -40,22 +40,22 @@
colno: col,
}
});

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

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

LogglyTracker.prototype = {
setSession: function(session_id) {
if(session_id) {
@@ -71,14 +71,14 @@
},
push: function(data) {
var type = typeof data;

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

var self = this;


if(type === 'string') {
data = {
'text': data
@@ -88,38 +88,40 @@
self.logglyCollectorDomain = data.logglyCollectorDomain;
return;
}

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

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;
q = 'PLAINTEXT=' + encodeURIComponent(JSON.stringify(data)),
// prevent browsers and proxy servers from caching tracking pixel
cacheBuster = 'CACHEBUST=' + Math.random() * 10000000000000000;
im.src = this.inputUrl + q + '&' + cacheBuster;
} catch (ex) {
if (window && window.console && typeof window.console.log === 'function') {
console.log("Failed to log to loggly because of this exception:\n" + ex);
@@ -145,21 +147,21 @@
document.cookie = LOGGLY_SESSION_KEY + '=' + value;
}
};

var existing = window._LTracker;

var tracker = new LogglyTracker();

if(existing && existing.length ) {
var i = 0,
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);

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.