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,30 +78,68 @@ describe('analytics', function() {
analytics.__set__('ga.event', googleEvent); analytics.__set__('ga.event', googleEvent);
}); });
it('tracks event in amplitude', function() { context('Amplitude', function() {
it('tracks event in amplitude', function() {
initializedAnalytics.track(event_type, analyticsData); initializedAnalytics.track(event_type, analyticsData);
expect(amplitudeTrack).to.be.calledOnce; expect(amplitudeTrack).to.be.calledOnce;
expect(amplitudeTrack).to.be.calledWith({ expect(amplitudeTrack).to.be.calledWith({
event_type: 'Cron', event_type: 'Cron',
user_id: 'unique-user-id', user_id: 'unique-user-id',
event_properties: { event_properties: {
resting: true, resting: true,
cronCount: 5 cronCount: 5
} }
});
});
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'
}
});
}); });
}); });
it('tracks event in google analytics', function() { context('Google Analytics', function() {
initializedAnalytics.track(event_type, analyticsData); it('tracks event in google analytics', function() {
initializedAnalytics.track(event_type, analyticsData);
expect(googleEvent).to.be.calledOnce; expect(googleEvent).to.be.calledOnce;
expect(googleEvent).to.be.calledWith( expect(googleEvent).to.be.calledWith(
'behavior', 'behavior',
'Cron', 'Cron',
'Ga Label' 'Ga Label'
); );
});
}); });
}); });

View File

@@ -66,9 +66,26 @@ function _formatDataForAmplitude(data) {
} }
function _formatUserData(user) { 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) { function _sendPurchaseDataToGoogle(data) {