2015-06-18 20:43:57 +00:00
|
|
|
/**
|
|
|
|
|
* Created by Sabe on 6/11/2015.
|
|
|
|
|
*/
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
describe('Analytics Service', function () {
|
2015-06-20 20:50:06 +00:00
|
|
|
var analytics, user;
|
2015-06-18 20:43:57 +00:00
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2015-06-20 20:50:06 +00:00
|
|
|
user = specHelper.newUser();
|
2015-06-20 21:04:34 +00:00
|
|
|
user.contributor = {};
|
|
|
|
|
user.purchased = { plan: {} };
|
2015-06-20 20:50:06 +00:00
|
|
|
|
|
|
|
|
module(function($provide) {
|
|
|
|
|
$provide.value('User', {user: user});
|
|
|
|
|
});
|
|
|
|
|
|
2015-06-18 20:43:57 +00:00
|
|
|
inject(function(Analytics) {
|
|
|
|
|
analytics = Analytics;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('error handling', function() {
|
|
|
|
|
|
2015-06-20 18:25:34 +00:00
|
|
|
beforeEach(function() {
|
2015-06-20 20:00:30 +00:00
|
|
|
sandbox.stub(console, 'log');
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not accept tracking events without required properties', function() {
|
|
|
|
|
analytics.track('action');
|
|
|
|
|
analytics.track({'hitType':'pageview','eventCategory':'green'});
|
|
|
|
|
analytics.track({'hitType':'pageview','eventAction':'eat'});
|
|
|
|
|
analytics.track({'eventCategory':'green','eventAction':'eat'});
|
|
|
|
|
analytics.track({'hitType':'pageview'});
|
|
|
|
|
analytics.track({'eventCategory':'green'});
|
|
|
|
|
analytics.track({'eventAction':'eat'});
|
|
|
|
|
expect(console.log.callCount).to.eql(7);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not accept tracking events with incorrect hit type', function () {
|
|
|
|
|
analytics.track({'hitType':'moogly','eventCategory':'green','eventAction':'eat'});
|
|
|
|
|
expect(console.log).to.have.been.calledOnce;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('Amplitude', function() {
|
|
|
|
|
|
2015-06-20 18:25:34 +00:00
|
|
|
beforeEach(function() {
|
2015-06-20 20:00:30 +00:00
|
|
|
sandbox.stub(amplitude, 'setUserId');
|
|
|
|
|
sandbox.stub(amplitude, 'logEvent');
|
|
|
|
|
sandbox.stub(amplitude, 'setUserProperties');
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
|
2015-06-20 21:37:30 +00:00
|
|
|
describe('register', function() {
|
|
|
|
|
it('sets up tracking when user registers', function() {
|
|
|
|
|
analytics.register();
|
|
|
|
|
expect(amplitude.setUserId).to.have.been.calledOnce;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('login', function() {
|
|
|
|
|
it('sets up tracking when user logs in', function() {
|
|
|
|
|
analytics.login();
|
|
|
|
|
expect(amplitude.setUserId).to.have.been.calledOnce;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('track', function() {
|
|
|
|
|
it('tracks a simple user action', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
expect(amplitude.logEvent).to.have.been.calledOnce;
|
|
|
|
|
expect(amplitude.logEvent).to.have.been.calledWith('cron',{'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('tracks a user action with additional properties', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
expect(amplitude.logEvent).to.have.been.calledOnce;
|
|
|
|
|
expect(amplitude.logEvent).to.have.been.calledWith('cron',{'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('updateUser', function() {
|
|
|
|
|
it('updates user-level properties with values provided in properties', function() {
|
|
|
|
|
analytics.updateUser({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
expect(amplitude.setUserProperties).to.have.been.calledOnce;
|
|
|
|
|
expect(amplitude.setUserProperties).to.have.been.calledWith({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updates user-level properties with certain user values when no properties are provided', function() {
|
|
|
|
|
user._id = 'unique-user-id';
|
|
|
|
|
user.stats.class = 'wizard';
|
|
|
|
|
user.stats.exp = 35.7;
|
|
|
|
|
user.stats.gp = 43.2;
|
|
|
|
|
user.stats.hp = 47.8;
|
|
|
|
|
user.stats.lvl = 24;
|
|
|
|
|
user.stats.mp = 41;
|
|
|
|
|
user.contributor.level = 1;
|
|
|
|
|
user.purchased.plan.planId = 'unique-plan-id';
|
|
|
|
|
|
|
|
|
|
analytics.updateUser();
|
|
|
|
|
expect(amplitude.setUserProperties).to.have.been.calledOnce;
|
|
|
|
|
expect(amplitude.setUserProperties).to.have.been.calledWith({
|
|
|
|
|
UUID: 'unique-user-id',
|
|
|
|
|
Class: 'wizard',
|
|
|
|
|
Experience: 35,
|
|
|
|
|
Gold: 43,
|
|
|
|
|
Health: 48,
|
|
|
|
|
Level: 24,
|
|
|
|
|
Mana: 41,
|
|
|
|
|
contributorLevel: 1,
|
|
|
|
|
subscription: 'unique-plan-id'
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('Google Analytics', function() {
|
|
|
|
|
|
2015-06-20 18:25:34 +00:00
|
|
|
beforeEach(function() {
|
2015-06-20 20:00:30 +00:00
|
|
|
sandbox.stub(window, 'ga');
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
|
2015-06-20 21:37:30 +00:00
|
|
|
describe('register', function() {
|
|
|
|
|
it('sets up tracking when user registers', function() {
|
|
|
|
|
analytics.register();
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('set');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('login', function() {
|
|
|
|
|
it('sets up tracking when user logs in', function() {
|
|
|
|
|
analytics.login();
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('set');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('track', function() {
|
|
|
|
|
it('tracks a simple user action', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('send',{'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('tracks a user action with additional properties', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('send',{'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('updateUser', function() {
|
|
|
|
|
it('updates user-level properties with values provided in properties', function() {
|
|
|
|
|
analytics.updateUser({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('set', {'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updates user-level properties', function() {
|
|
|
|
|
user._id = 'unique-user-id';
|
|
|
|
|
user.stats.class = 'wizard';
|
|
|
|
|
user.stats.exp = 35.7;
|
|
|
|
|
user.stats.gp = 43.2;
|
|
|
|
|
user.stats.hp = 47.8;
|
|
|
|
|
user.stats.lvl = 24;
|
|
|
|
|
user.stats.mp = 41;
|
|
|
|
|
user.contributor.level = 1;
|
|
|
|
|
user.purchased.plan.planId = 'unique-plan-id';
|
|
|
|
|
|
|
|
|
|
analytics.updateUser();
|
|
|
|
|
expect(ga).to.have.been.calledOnce;
|
|
|
|
|
expect(ga).to.have.been.calledWith('set',{
|
|
|
|
|
UUID: 'unique-user-id',
|
|
|
|
|
Class: 'wizard',
|
|
|
|
|
Experience: 35,
|
|
|
|
|
Gold: 43,
|
|
|
|
|
Health: 48,
|
|
|
|
|
Level: 24,
|
|
|
|
|
Mana: 41,
|
|
|
|
|
contributorLevel: 1,
|
|
|
|
|
subscription: 'unique-plan-id'
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-06-19 01:33:54 +00:00
|
|
|
context.skip('Mixpanel', function() { // Mixpanel not currently in use
|
2015-06-18 20:43:57 +00:00
|
|
|
|
2015-06-20 18:25:34 +00:00
|
|
|
beforeEach(function() {
|
2015-06-20 20:00:30 +00:00
|
|
|
sandbox.stub(mixpanel, 'alias');
|
|
|
|
|
sandbox.stub(mixpanel, 'identify');
|
|
|
|
|
sandbox.stub(mixpanel, 'track');
|
|
|
|
|
sandbox.stub(mixpanel, 'register');
|
2015-06-18 20:43:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets up tracking when user registers', function() {
|
|
|
|
|
analytics.register();
|
|
|
|
|
expect(mixpanel.alias).to.have.been.calledOnce;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('sets up tracking when user logs in', function() {
|
|
|
|
|
analytics.login();
|
|
|
|
|
expect(mixpanel.identify).to.have.been.calledOnce;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('tracks a simple user action', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
expect(mixpanel.track).to.have.been.calledOnce;
|
|
|
|
|
expect(mixpanel.track).to.have.been.calledWith('cron',{'hitType':'event','eventCategory':'behavior','eventAction':'cron'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('tracks a user action with additional properties', function() {
|
|
|
|
|
analytics.track({'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
expect(mixpanel.track).to.have.been.calledOnce;
|
|
|
|
|
expect(mixpanel.track).to.have.been.calledWith('cron',{'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('updates user-level properties', function() {
|
|
|
|
|
analytics.updateUser({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
expect(mixpanel.register).to.have.been.calledOnce;
|
|
|
|
|
expect(mixpanel.register).to.have.been.calledWith({'userBoolean': false, 'userNumber': -8, 'userString': 'Enlightened'});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|