Skip to content

Commit

Permalink
tracking service added
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksandr Bardanov committed Aug 14, 2014
1 parent 15a9bb5 commit 4164742
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ define(function (require, exports, module) {

require('./services/injector').init();

//require('./services/onlineTracking').init();
require('./services/onlineTracking').init();
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Documents Toolbar",
"description": "Adds toolbar with a list of open documents on the top of the editor.",
"homepage": "https://github.com/dnbard/brackets-documents-toolbar",
"version": "0.1.0",
"version": "0.1.1",
"author": "Alex Bardanov <dnbard@gmail.com>",
"license": "MIT",
"engines": {
Expand Down
64 changes: 64 additions & 0 deletions services/onlineTracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// This is example of a client for extenstion online tracking service
// Part of Brackets Extension Rating extension by Alex Bardanov
// https://github.com/dnbard/brackets-extension-rating

// use require('./onlineTrackingClient').init() to activate tracking

define(function(require, exports){
var trackingServiceUrl = 'http://brackets-online.herokuapp.com/',
// http://brackets-online.herokuapp.com/ is an address of default tracking service
// Change it if you use self-hosting instance of online tracking service
appToken = '53eca7616073b96ad100017a',
// read https://github.com/dnbard/brackets-extension-rating/wiki/Online-and-max-users-counters-in-this-extension
// to learn on how to obtain an application token for your extension
mins60 = 60 * 60 * 1000,
mins5 = 5 * 60 * 1000,
keyId = 'ext-online-id';

function tick(){
var userId = getUserId(appToken, keyId),
url;

if (userId){
url = trackingServiceUrl + 'tick/' + appToken + '/' + userId;
} else {
url = trackingServiceUrl + 'tick/' + appToken;
}

$.ajax({ url: url })
.success(function(data){
//TODO: create complex model of data in local storage to support any number of extensions
if (data && data !== 'OK' && data !== 'ERROR'){
saveUserId(data, appToken, keyId);
}
}).error(function(){
console.log('Can\'t track online status, retry in 5 mins');
setTimeout(tick, mins5);
});
}

function init(){
tick();
setInterval(tick, mins60);
}

function getUserId(appToken, keyId){
if (typeof appToken !== 'string' || typeof keyId !== 'string'){
throw new Error('Invalid argument');
}

return JSON.parse(localStorage.getItem(keyId) || '{ }')[appToken];
}

function saveUserId(id, appToken, keyId){
if (typeof id !== 'string' || typeof appToken !== 'string' || typeof keyId !== 'string'){
throw new Error('Invalid argument');
}

var obj = JSON.parse(localStorage.getItem(keyId) || '{ }');
obj[appToken] = id;
localStorage.setItem(keyId, JSON.stringify(obj));
}

exports.init = init;
});

0 comments on commit 4164742

Please sign in to comment.