Skip to content

Commit

Permalink
CHARTS-470 track data how users are using charts (#21)
Browse files Browse the repository at this point in the history
* Fixed problem with async initializing of stitch

* Fixed problem with passing metadata for user resource
  • Loading branch information
mikhail-hatsilau committed Apr 4, 2018
1 parent d14f176 commit 42b0a7a
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 35 deletions.
5 changes: 4 additions & 1 deletion packages/metrics/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"extends": [
"mongodb-js/node",
"mongodb-js/browser"
]
],
"globals": {
"Promise": true
}
}
5 changes: 1 addition & 4 deletions packages/metrics/lib/resources/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ module.exports = BaseResource.extend({
developer: 'boolean',
twitter: 'string'
},
login: function(callback) {
var options = {
userId: this.userId
};
login: function(options, callback) {
this._send_event(options, callback);
}
});
75 changes: 51 additions & 24 deletions packages/metrics/lib/trackers/stitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var StitchTracker = State.extend({
_usersDatabaseName: 'any',
_usersCollectionName: 'any',
_client: 'any',
_callsQueue: ['array', true, function() { return []; }], // array of object with format: { fn: Function, args: Array }
enabled: ['boolean', true, false],
hasBooted: ['boolean', true, false]
},
Expand All @@ -80,15 +81,30 @@ var StitchTracker = State.extend({
initialize: function() {
this._identify = this._identify.bind(this);
this._enabledConfiguredChanged = this._enabledConfiguredChanged.bind(this);
this._trackFromQueue = this._trackFromQueue.bind(this);

this.on('change:enabledAndConfigured', this._enabledConfiguredChanged);
},
_enabledConfiguredChanged: function() {
if (this.enabledAndConfigured) {
this._setup();
this._identify();
// tracks all events from queue and calls _identify only when stitch client has been initialized
this._setup()
.then(function() {
this._trackFromQueue();
this._identify();
}.bind(this));
}
},
_trackFromQueue: function() {
var callData;
while (this._callsQueue.length) {
callData = this._callsQueue.shift();
callData.fn.apply(this, callData.args);
}
},
_isTrackerReady: function() {
return this.enabledAndConfigured && this._client;
},
_setup: function() {
var eventsNS = parseNamespaceString(this.events);
this._eventsDatabaseName = eventsNS.database;
Expand All @@ -99,21 +115,22 @@ var StitchTracker = State.extend({
this._usersCollectionName = usersNS.collection;

var self = this;
stitch.StitchClientFactory.create(this.appId)
return stitch.StitchClientFactory.create(this.appId)
.then(function(client) {
client.login().then(function() {
self._client = client;

debug('setup client', {
_client: self._client,
_eventsDatabaseName: self._eventsDatabaseName,
_eventsCollectionName: self._eventsCollectionName,
_usersDatabaseName: self._usersDatabaseName,
_usersCollectionName: self._usersCollectionName
return client.login()
.then(function() {
self._client = client;

debug('setup client', {
_client: self._client,
_eventsDatabaseName: self._eventsDatabaseName,
_eventsCollectionName: self._eventsCollectionName,
_usersDatabaseName: self._usersDatabaseName,
_usersCollectionName: self._usersCollectionName
});
}).catch(function(e) {
debug('error logging in via stitch: %s', e.message);
});
}).catch(function(e) {
debug('error logging in via stitch: %s', e.message);
});
});
},
_identify: function() {
Expand Down Expand Up @@ -165,7 +182,7 @@ var StitchTracker = State.extend({
);
},
_getCollection: function(db, name, fn) {
if (!this.enabledAndConfigured || !this._client) {
if (!this._isTrackerReady()) {
return fn(new Error('stitch tracker not configured yet.'));
}
return fn(null, this._client.service('mongodb', 'mongodb-atlas').db(db).collection(name));
Expand Down Expand Up @@ -202,18 +219,28 @@ var StitchTracker = State.extend({
delete metadata['event id'];
payload.metadata = snakeCase(redact(metadata));

var getCollectionCallback = function(err, collection) {
if (err) {
return debug('error sending event to stitch: %s', err.message);
}
payload.stitch_user_id = this._client.authedId();
debug('sending event `%s`', eventName, payload);
return collection.insertOne(payload);
}.bind(this);

if (!this._isTrackerReady()) {
this._callsQueue.push({
fn: this._getCollection,
args: [this._eventsDatabaseName, this._eventsCollectionName, getCollectionCallback]
});
return;
}

// send payload
return this._getCollection(
this._eventsDatabaseName,
this._eventsCollectionName,
function(err, collection) {
if (err) {
return debug('error sending event to stitch: %s', err.message);
}
payload.stitch_user_id = this._client.authedId();
debug('sending event `%s`', eventName, payload);
return collection.insertOne(payload);
}.bind(this)
getCollectionCallback
);
}
});
Expand Down
99 changes: 99 additions & 0 deletions packages/metrics/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"mocha": "^3.4.2",
"mongodb-js-fmt": "0.0.3",
"mongodb-js-precommit": "^0.3.0",
"pre-commit": "^1.1.2"
"pre-commit": "^1.1.2",
"sinon": "^4.5.0"
},
"license": "Apache-2.0",
"precommit": [
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/test/metrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('metrics', function() {
metrics.addResource(app);
metrics.addResource(user);

metrics.track('User', 'login', function(err, res) {
metrics.track('User', 'login', { appName: 'test' }, function(err, res) {
assert.ifError(err);
var resp = res.ga[0][0];
assert.equal(resp.statusCode, 200);
Expand Down
Loading

0 comments on commit 42b0a7a

Please sign in to comment.