Add format user data function to analytics server

This commit is contained in:
Blade Barringer
2015-07-08 08:09:43 -05:00
parent 73b2d816d1
commit 1c2ec66c93
2 changed files with 75 additions and 20 deletions

View File

@@ -78,6 +78,7 @@ describe('analytics', function() {
analytics.__set__('ga.event', googleEvent);
});
context('Amplitude', function() {
it('tracks event in amplitude', function() {
initializedAnalytics.track(event_type, analyticsData);
@@ -93,6 +94,42 @@ describe('analytics', function() {
});
});
it('sends user data if provided', function() {
var stats = { class: 'wizard', exp: 5, gp: 23, hp: 10, lvl: 4, mp: 30 };
var user = {
stats: stats,
contributor: { level: 1 },
purchased: { plan: { planId: 'foo-plan' } }
};
var analyticsDataWithUser = _.cloneDeep(analyticsData);
analyticsDataWithUser.user = user;
initializedAnalytics.track(event_type, analyticsDataWithUser);
expect(amplitudeTrack).to.be.calledOnce;
expect(amplitudeTrack).to.be.calledWith({
event_type: 'Cron',
user_id: 'unique-user-id',
event_properties: {
resting: true,
cronCount: 5
},
user_properties: {
Class: 'wizard',
Experience: 5,
Gold: 23,
Health: 10,
Level: 4,
Mana: 30,
contributorLevel: 1,
subscription: 'foo-plan'
}
});
});
});
context('Google Analytics', function() {
it('tracks event in google analytics', function() {
initializedAnalytics.track(event_type, analyticsData);
@@ -104,6 +141,7 @@ describe('analytics', function() {
);
});
});
});
describe('trackPurchase', function() {

View File

@@ -66,9 +66,26 @@ function _formatDataForAmplitude(data) {
}
function _formatUserData(user) {
var data = {};
var properties = {};
return data;
if (user.stats) {
properties.Class = user.stats.class;
properties.Experience = Math.floor(user.stats.exp);
properties.Gold = Math.floor(user.stats.gp);
properties.Health = Math.ceil(user.stats.hp);
properties.Level = user.stats.lvl;
properties.Mana = Math.floor(user.stats.mp);
}
if (user.contributor && user.contributor.level) {
properties.contributorLevel = user.contributor.level;
}
if (user.purchased && user.purchased.plan.planId) {
properties.subscription = user.purchased.plan.planId;
}
return properties;
}
function _sendPurchaseDataToGoogle(data) {