From d27bb8f07574c50352ed7ba235532fee8cf10ffb Mon Sep 17 00:00:00 2001 From: Aileen Booker Date: Thu, 8 Jun 2023 12:11:26 -0400 Subject: [PATCH] Added new Stripe events to `DomainEventsAnalytics` no issue - We need to send information about Stripe being enabled or disabled in live mode to analytics - This hooks up the Domain events listeners in the analytics service and processes this information accordingly --- .../services/segment/DomainEventsAnalytics.js | 26 +++++++++ .../segment/DomainEventsAnalytics.test.js | 56 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/ghost/core/core/server/services/segment/DomainEventsAnalytics.js b/ghost/core/core/server/services/segment/DomainEventsAnalytics.js index 28965be222..a49bdcb97e 100644 --- a/ghost/core/core/server/services/segment/DomainEventsAnalytics.js +++ b/ghost/core/core/server/services/segment/DomainEventsAnalytics.js @@ -1,4 +1,5 @@ const {MilestoneCreatedEvent} = require('@tryghost/milestones'); +const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('@tryghost/members-stripe-service').events; /** * @typedef {import('@tryghost/logging')} logging @@ -75,9 +76,34 @@ module.exports = class DomainEventsAnalytics { } } + /** + * + * @param {StripeLiveEnabledEvent|StripeLiveDisabledEvent} type + * + * @returns {Promise} + */ + async #handleStripeEvent(type) { + const eventName = type === StripeLiveDisabledEvent ? 'Stripe Live Disabled' : 'Stripe Live Enabled'; + + try { + this.#analytics.track(Object.assign(this.#trackDefaults, {}, {event: this.#prefix + eventName})); + } catch (err) { + this.#logging.error(err); + this.#exceptionHandler.captureException(err); + } + } + subscribeToEvents() { this.#DomainEvents.subscribe(MilestoneCreatedEvent, async (event) => { await this.#handleMilestoneCreatedEvent(event); }); + + this.#DomainEvents.subscribe(StripeLiveEnabledEvent, async () => { + await this.#handleStripeEvent(StripeLiveEnabledEvent); + }); + + this.#DomainEvents.subscribe(StripeLiveDisabledEvent, async () => { + await this.#handleStripeEvent(StripeLiveDisabledEvent); + }); } }; diff --git a/ghost/core/test/unit/server/services/segment/DomainEventsAnalytics.test.js b/ghost/core/test/unit/server/services/segment/DomainEventsAnalytics.test.js index fae638ba37..e9d0621706 100644 --- a/ghost/core/test/unit/server/services/segment/DomainEventsAnalytics.test.js +++ b/ghost/core/test/unit/server/services/segment/DomainEventsAnalytics.test.js @@ -8,6 +8,7 @@ const DomainEventsAnalytics = require('../../../../../core/server/services/segme // Stubbed dependencies const DomainEvents = require('@tryghost/domain-events'); const {MilestoneCreatedEvent} = require('@tryghost/milestones'); +const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('@tryghost/members-stripe-service').events; describe('DomainEventsAnalytics', function () { describe('Constructor', function () { @@ -54,7 +55,7 @@ describe('DomainEventsAnalytics', function () { }); domainEventsAnalytics.subscribeToEvents(); - assert(domainEventsStub.callCount === 1); + assert(domainEventsStub.callCount === 3); assert(loggingStub.callCount === 0); }); @@ -173,6 +174,59 @@ describe('DomainEventsAnalytics', function () { assert(loggingStub.callCount === 0); }); + it('handles Stripe live enabled and disabled events', async function () { + domainEventsAnalytics = new DomainEventsAnalytics({ + analytics: { + track: analyticsStub + }, + trackDefaults: { + userId: '9876', + properties: {email: 'john+stripe@test.com'} + }, + prefix: 'Pro: ', + exceptionHandler: { + captureException: exceptionHandlerStub + }, + DomainEvents, + logging: { + error: loggingStub + } + }); + + domainEventsAnalytics.subscribeToEvents(); + + DomainEvents.dispatch(StripeLiveEnabledEvent.create({ + message: 'Stripe live mode enabled' + })); + + await DomainEvents.allSettled(); + + assert(analyticsStub.callCount === 1); + assert(analyticsStub.getCall(0).calledWith({ + userId: '9876', + properties: {email: 'john+stripe@test.com'}, + event: 'Pro: Stripe Live Enabled' + })); + assert(loggingStub.callCount === 0); + + DomainEvents.dispatch(StripeLiveDisabledEvent.create({ + message: 'Stripe live mode disabled' + })); + + await DomainEvents.allSettled(); + + assert(analyticsStub.callCount === 2); + assert(analyticsStub.getCall(1).calledWith({ + userId: '9876', + properties: {email: 'john+stripe@test.com'}, + event: 'Pro: Stripe Live Disabled' + })); + + await DomainEvents.allSettled(); + + assert(loggingStub.callCount === 0); + }); + it('can handle tracking errors', async function () { let error = new Error('Test error'); domainEventsAnalytics = new DomainEventsAnalytics({