From be79d2086458d933c41aa8fabe57fe285981360e Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Wed, 24 Jun 2015 02:33:58 -0400 Subject: [PATCH 1/5] Debug all the things --- .../public/js/services/analyticsServices.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/website/public/js/services/analyticsServices.js b/website/public/js/services/analyticsServices.js index 462397954c..a56cf3ad5f 100644 --- a/website/public/js/services/analyticsServices.js +++ b/website/public/js/services/analyticsServices.js @@ -13,9 +13,11 @@ ]; function analyticsFactory(User) { + console.debug("Analytics Factory function"); var user = User.user; + console.debug("Setting amplitude stub..."); // Amplitude var r = window.amplitude || {}; r._q = []; @@ -24,15 +26,21 @@ for (var o = 0; o < i.length; o++) {a(i[o])} window.amplitude = r; amplitude.init(window.env.AMPLITUDE_KEY); + console.debug("done"); + console.debug("Setting GA stub..."); // Google Analytics (aka Universal Analytics) window['GoogleAnalyticsObject'] = 'ga'; window['ga'] = window['ga'] || function() { (window['ga'].q = window['ga'].q || []).push(arguments) }, window['ga'].l = 1 * new Date(); ga('create', window.env.GA_ID, 'auto'); + console.debug("done"); function loadScripts() { + console.debug("Analytics.loadScripts function"); + + console.debug("Adding amplitude script..."); // Amplitude var n = document.createElement("script"); var s = document.getElementsByTagName("script")[0]; @@ -40,44 +48,56 @@ n.async = true; n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js"; s.parentNode.insertBefore(n, s); + console.debug("done"); + console.debug("Adding GA script..."); // Google Analytics var a = document.createElement('script'); var m = document.getElementsByTagName('script')[0]; a.async = 1; a.src = '//www.google-analytics.com/analytics.js'; m.parentNode.insertBefore(a, m); + console.debug("done"); } function register() { + console.debug("Analytics.register function"); amplitude.setUserId(user._id); ga('set', {'userId':user._id}); + console.debug("done"); } function login() { + console.debug("Analytics.login function"); amplitude.setUserId(user._id); ga('set', {'userId':user._id}); + console.debug("done"); } function track(properties) { + console.debug("Analytics.track function"); if(_doesNotHaveRequiredFields(properties)) { return false; } if(_doesNotHaveAllowedHitType(properties)) { return false; } amplitude.logEvent(properties.eventAction,properties); ga('send',properties); + console.debug("done"); } function updateUser(properties) { + console.debug("Analytics.updateUser function"); properties = properties || {}; _gatherUserStats(user, properties); amplitude.setUserProperties(properties); ga('set',properties); + console.debug("done"); } if (window.env.NODE_ENV === 'production') loadScripts(); + console.debug("Returning from factory"); return { loadScripts: loadScripts, register: register, From c7fea86aa557e3c8989abbf75d7ff9b0489f7874 Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Wed, 24 Jun 2015 03:21:30 -0400 Subject: [PATCH 2/5] Revert "Debug all the things" This reverts commit be79d2086458d933c41aa8fabe57fe285981360e. --- .../public/js/services/analyticsServices.js | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/website/public/js/services/analyticsServices.js b/website/public/js/services/analyticsServices.js index a56cf3ad5f..462397954c 100644 --- a/website/public/js/services/analyticsServices.js +++ b/website/public/js/services/analyticsServices.js @@ -13,11 +13,9 @@ ]; function analyticsFactory(User) { - console.debug("Analytics Factory function"); var user = User.user; - console.debug("Setting amplitude stub..."); // Amplitude var r = window.amplitude || {}; r._q = []; @@ -26,21 +24,15 @@ for (var o = 0; o < i.length; o++) {a(i[o])} window.amplitude = r; amplitude.init(window.env.AMPLITUDE_KEY); - console.debug("done"); - console.debug("Setting GA stub..."); // Google Analytics (aka Universal Analytics) window['GoogleAnalyticsObject'] = 'ga'; window['ga'] = window['ga'] || function() { (window['ga'].q = window['ga'].q || []).push(arguments) }, window['ga'].l = 1 * new Date(); ga('create', window.env.GA_ID, 'auto'); - console.debug("done"); function loadScripts() { - console.debug("Analytics.loadScripts function"); - - console.debug("Adding amplitude script..."); // Amplitude var n = document.createElement("script"); var s = document.getElementsByTagName("script")[0]; @@ -48,56 +40,44 @@ n.async = true; n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js"; s.parentNode.insertBefore(n, s); - console.debug("done"); - console.debug("Adding GA script..."); // Google Analytics var a = document.createElement('script'); var m = document.getElementsByTagName('script')[0]; a.async = 1; a.src = '//www.google-analytics.com/analytics.js'; m.parentNode.insertBefore(a, m); - console.debug("done"); } function register() { - console.debug("Analytics.register function"); amplitude.setUserId(user._id); ga('set', {'userId':user._id}); - console.debug("done"); } function login() { - console.debug("Analytics.login function"); amplitude.setUserId(user._id); ga('set', {'userId':user._id}); - console.debug("done"); } function track(properties) { - console.debug("Analytics.track function"); if(_doesNotHaveRequiredFields(properties)) { return false; } if(_doesNotHaveAllowedHitType(properties)) { return false; } amplitude.logEvent(properties.eventAction,properties); ga('send',properties); - console.debug("done"); } function updateUser(properties) { - console.debug("Analytics.updateUser function"); properties = properties || {}; _gatherUserStats(user, properties); amplitude.setUserProperties(properties); ga('set',properties); - console.debug("done"); } if (window.env.NODE_ENV === 'production') loadScripts(); - console.debug("Returning from factory"); return { loadScripts: loadScripts, register: register, From 01755a6d9863b2c3ba12c01b5838879e824bad04 Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Wed, 24 Jun 2015 03:23:48 -0400 Subject: [PATCH 3/5] setTimeout all the analytics stuff --- .../public/js/services/analyticsServices.js | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/website/public/js/services/analyticsServices.js b/website/public/js/services/analyticsServices.js index 462397954c..53f7c42186 100644 --- a/website/public/js/services/analyticsServices.js +++ b/website/public/js/services/analyticsServices.js @@ -33,47 +33,57 @@ ga('create', window.env.GA_ID, 'auto'); function loadScripts() { - // Amplitude - var n = document.createElement("script"); - var s = document.getElementsByTagName("script")[0]; - n.type = "text/javascript"; - n.async = true; - n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js"; - s.parentNode.insertBefore(n, s); + setTimeout(function() { + // Amplitude + var n = document.createElement("script"); + var s = document.getElementsByTagName("script")[0]; + n.type = "text/javascript"; + n.async = true; + n.src = "https://d24n15hnbwhuhn.cloudfront.net/libs/amplitude-2.2.0-min.gz.js"; + s.parentNode.insertBefore(n, s); - // Google Analytics - var a = document.createElement('script'); - var m = document.getElementsByTagName('script')[0]; - a.async = 1; - a.src = '//www.google-analytics.com/analytics.js'; - m.parentNode.insertBefore(a, m); + // Google Analytics + var a = document.createElement('script'); + var m = document.getElementsByTagName('script')[0]; + a.async = 1; + a.src = '//www.google-analytics.com/analytics.js'; + m.parentNode.insertBefore(a, m); + }); } function register() { - amplitude.setUserId(user._id); - ga('set', {'userId':user._id}); + setTimeout(function() { + amplitude.setUserId(user._id); + ga('set', {'userId':user._id}); + }); } function login() { - amplitude.setUserId(user._id); - ga('set', {'userId':user._id}); + setTimeout(function() { + amplitude.setUserId(user._id); + ga('set', {'userId':user._id}); + }); } function track(properties) { - if(_doesNotHaveRequiredFields(properties)) { return false; } - if(_doesNotHaveAllowedHitType(properties)) { return false; } + setTimeout(function() { + if(_doesNotHaveRequiredFields(properties)) { return false; } + if(_doesNotHaveAllowedHitType(properties)) { return false; } - amplitude.logEvent(properties.eventAction,properties); - ga('send',properties); + amplitude.logEvent(properties.eventAction,properties); + ga('send',properties); + }); } function updateUser(properties) { - properties = properties || {}; + setTimeout(function() { + properties = properties || {}; - _gatherUserStats(user, properties); + _gatherUserStats(user, properties); - amplitude.setUserProperties(properties); - ga('set',properties); + amplitude.setUserProperties(properties); + ga('set',properties); + }); } if (window.env.NODE_ENV === 'production') loadScripts(); @@ -115,4 +125,3 @@ } } }()); - From c8b5480157cd4ae30c987cc086d590cc123737a0 Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Wed, 24 Jun 2015 03:41:49 -0400 Subject: [PATCH 4/5] Repair tests to respect setTimeout --- test/spec/services/analyticsServicesSpec.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/spec/services/analyticsServicesSpec.js b/test/spec/services/analyticsServicesSpec.js index 935ace5900..25ba28aa39 100644 --- a/test/spec/services/analyticsServicesSpec.js +++ b/test/spec/services/analyticsServicesSpec.js @@ -1,9 +1,11 @@ 'use strict'; -describe('Analytics Service', function () { - var analytics, user; +describe.only('Analytics Service', function () { + var analytics, user, clock; beforeEach(function() { + clock = sandbox.useFakeTimers(); + sandbox.stub(window, 'refresher', function(){return true}); user = specHelper.newUser(); user.contributor = {}; user.purchased = { plan: {} }; @@ -26,14 +28,16 @@ describe('Analytics Service', function () { sandbox.stub(window, 'ga'); }); - it('sets up user with Amplitude', function() { + it.only('sets up user with Amplitude', function() { analytics.register(); + clock.tick(); expect(amplitude.setUserId).to.have.been.calledOnce; expect(amplitude.setUserId).to.have.been.calledWith(user._id); }); it('sets up user with Google Analytics', function() { analytics.register(); + clock.tick(); expect(ga).to.have.been.calledOnce; expect(ga).to.have.been.calledWith('set', {userId: user._id}); }); @@ -48,6 +52,7 @@ describe('Analytics Service', function () { it('sets up tracking for amplitude', function() { analytics.login(); + clock.tick(); expect(amplitude.setUserId).to.have.been.calledOnce; expect(amplitude.setUserId).to.have.been.calledWith(user._id); @@ -55,6 +60,7 @@ describe('Analytics Service', function () { it('sets up tracking for google analytics', function() { analytics.login(); + clock.tick(); expect(ga).to.have.been.calledOnce; expect(ga).to.have.been.calledWith('set', {userId: user._id}); @@ -73,6 +79,7 @@ describe('Analytics Service', function () { it('tracks a simple user action with Amplitude', function() { var properties = {'hitType':'event','eventCategory':'behavior','eventAction':'cron'}; analytics.track(properties); + clock.tick(); expect(amplitude.logEvent).to.have.been.calledOnce; expect(amplitude.logEvent).to.have.been.calledWith('cron', properties); @@ -81,6 +88,7 @@ describe('Analytics Service', function () { it('tracks a simple user action with Google Analytics', function() { var properties = {'hitType':'event','eventCategory':'behavior','eventAction':'cron'}; analytics.track(properties); + clock.tick(); expect(ga).to.have.been.calledOnce; expect(ga).to.have.been.calledWith('send', properties); @@ -89,6 +97,7 @@ describe('Analytics Service', function () { it('tracks a user action with additional properties in Amplitude', function() { var properties = {'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'}; analytics.track(properties); + clock.tick(); expect(amplitude.logEvent).to.have.been.calledOnce; expect(amplitude.logEvent).to.have.been.calledWith('cron', properties); @@ -97,6 +106,7 @@ describe('Analytics Service', function () { it('tracks a user action with additional properties in google analytics', function() { var properties = {'hitType':'event','eventCategory':'behavior','eventAction':'cron','booleanProperty':true,'numericProperty':17,'stringProperty':'bagel'}; analytics.track(properties); + clock.tick(); expect(ga).to.have.been.calledOnce; expect(ga).to.have.been.calledWith('send', properties); @@ -118,6 +128,7 @@ describe('Analytics Service', function () { analytics.track({'hitType':'pageview'}); analytics.track({'eventCategory':'green'}); analytics.track({'eventAction':'eat'}); + clock.tick(); }); it('logs errors to console', function() { @@ -136,6 +147,7 @@ describe('Analytics Service', function () { context('incorrect hit type', function() { beforeEach(function() { analytics.track({'hitType':'moogly','eventCategory':'green','eventAction':'eat'}); + clock.tick(); }); it('logs error to console', function () { @@ -181,6 +193,7 @@ describe('Analytics Service', function () { user.stats.mp = 41; analytics.updateUser(properties); + clock.tick(); }); it('calls Amplitude with provided properties and select user info', function() { @@ -219,6 +232,7 @@ describe('Analytics Service', function () { user.purchased.plan.planId = 'unique-plan-id'; analytics.updateUser(); + clock.tick(); }); it('calls Amplitude with select user info', function() { From 8c343c18456c08735c661e9d127df61928268c3e Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Wed, 24 Jun 2015 03:45:00 -0400 Subject: [PATCH 5/5] *ahem* You saw nothing. --- test/spec/services/analyticsServicesSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/spec/services/analyticsServicesSpec.js b/test/spec/services/analyticsServicesSpec.js index 25ba28aa39..0a03c3b638 100644 --- a/test/spec/services/analyticsServicesSpec.js +++ b/test/spec/services/analyticsServicesSpec.js @@ -1,6 +1,6 @@ 'use strict'; -describe.only('Analytics Service', function () { +describe('Analytics Service', function () { var analytics, user, clock; beforeEach(function() {