From 7bc74f2c11304efc5f790fd842d3f68391fb60bd Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 8 Jul 2015 21:12:32 -0500 Subject: [PATCH] Track unsubscription --- test/server_side/analytics.test.js | 115 +++++++++++----------- website/src/controllers/payments/index.js | 8 +- 2 files changed, 67 insertions(+), 56 deletions(-) diff --git a/test/server_side/analytics.test.js b/test/server_side/analytics.test.js index eb80cb5a49..73c6f60560 100644 --- a/test/server_side/analytics.test.js +++ b/test/server_side/analytics.test.js @@ -167,73 +167,78 @@ describe('analytics', function() { analytics.__set__('ga.transaction', googleTransaction); }); - it('calls amplitude.track', function() { + context('Amplitude', function() { - initializedAnalytics.trackPurchase(purchaseData); + it('calls amplitude.track', function() { + var data = _.cloneDeep(purchaseData); + initializedAnalytics.trackPurchase(data); - expect(amplitudeTrack).to.be.calledOnce; - expect(amplitudeTrack).to.be.calledWith({ - event_type: 'purchase', - user_id: 'user-id', - event_properties: { - paymentMethod: 'PayPal', - sku: 'paypal-checkout', - gift: false, - itemPurchased: 'Gems', - purchaseType: 'checkout', - quantity: 1 - }, - revenue: 8 + expect(amplitudeTrack).to.be.calledOnce; + expect(amplitudeTrack).to.be.calledWith({ + event_type: 'purchase', + user_id: 'user-id', + event_properties: { + paymentMethod: 'PayPal', + sku: 'paypal-checkout', + gift: false, + itemPurchased: 'Gems', + purchaseType: 'checkout', + quantity: 1 + }, + revenue: 8 + }); }); }); - it('calls ga.event', function() { + context('Google Analytics', function() { - initializedAnalytics.trackPurchase(purchaseData); + it('calls ga.event', function() { + var data = _.cloneDeep(purchaseData); + initializedAnalytics.trackPurchase(data); - expect(googleEvent).to.be.calledOnce; - expect(googleEvent).to.be.calledWith( - 'commerce', - 'checkout', - 'PayPal', - 8 - ); - }); + expect(googleEvent).to.be.calledOnce; + expect(googleEvent).to.be.calledWith( + 'commerce', + 'checkout', + 'PayPal', + 8 + ); + }); - it('calls ga.transaction', function() { + it('calls ga.transaction', function() { + var data = _.cloneDeep(purchaseData); + initializedAnalytics.trackPurchase(data); - initializedAnalytics.trackPurchase(purchaseData); + expect(googleTransaction).to.be.calledOnce; + expect(googleTransaction).to.be.calledWith( + 'user-id', + 8 + ); + expect(googleItem).to.be.calledOnce; + expect(googleItem).to.be.calledWith( + 8, + 1, + 'paypal-checkout', + 'Gems', + 'checkout' + ); + }); - expect(googleTransaction).to.be.calledOnce; - expect(googleTransaction).to.be.calledWith( - 'user-id', - 8 - ); - expect(googleItem).to.be.calledOnce; - expect(googleItem).to.be.calledWith( - 8, - 1, - 'paypal-checkout', - 'Gems', - 'checkout' - ); - }); + it('appends gift to variation of ga.transaction.item if gift is true', function() { - it('appends gift to variation of ga.transaction.item if gift is true', function() { + var data = _.cloneDeep(purchaseData); + data.gift = true; + initializedAnalytics.trackPurchase(data); - var purchaseDataWithGift = _.clone(purchaseData); - purchaseDataWithGift.gift = true; - - initializedAnalytics.trackPurchase(purchaseDataWithGift); - - expect(googleItem).to.be.calledOnce; - expect(googleItem).to.be.calledWith( - 8, - 1, - 'paypal-checkout', - 'Gems', - 'checkout - Gift' - ); + expect(googleItem).to.be.calledOnce; + expect(googleItem).to.be.calledWith( + 8, + 1, + 'paypal-checkout', + 'Gems', + 'checkout - Gift' + ); + }); }); }); }); diff --git a/website/src/controllers/payments/index.js b/website/src/controllers/payments/index.js index d9ce943250..b876954d62 100644 --- a/website/src/controllers/payments/index.js +++ b/website/src/controllers/payments/index.js @@ -127,7 +127,13 @@ exports.cancelSubscription = function(data, cb) { data.user.save(cb); utils.txnEmail(data.user, 'cancel-subscription'); - utils.ga.event('commerce', 'unsubscribe', data.paymentMethod).send(); + var analyticsData = { + uuid: data.user._id, + gaCategory: 'commerce', + gaLabel: data.paymentMethod, + paymentMethod: data.paymentMethod + } + utils.analytics.track('unsubscribe', analyticsData); } exports.buyGems = function(data, cb) {