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
This commit is contained in:
Aileen Booker 2023-06-08 12:11:26 -04:00 committed by Aileen Booker
parent 48f9e85e09
commit d27bb8f075
2 changed files with 81 additions and 1 deletions

View File

@ -1,4 +1,5 @@
const {MilestoneCreatedEvent} = require('@tryghost/milestones'); const {MilestoneCreatedEvent} = require('@tryghost/milestones');
const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('@tryghost/members-stripe-service').events;
/** /**
* @typedef {import('@tryghost/logging')} logging * @typedef {import('@tryghost/logging')} logging
@ -75,9 +76,34 @@ module.exports = class DomainEventsAnalytics {
} }
} }
/**
*
* @param {StripeLiveEnabledEvent|StripeLiveDisabledEvent} type
*
* @returns {Promise<void>}
*/
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() { subscribeToEvents() {
this.#DomainEvents.subscribe(MilestoneCreatedEvent, async (event) => { this.#DomainEvents.subscribe(MilestoneCreatedEvent, async (event) => {
await this.#handleMilestoneCreatedEvent(event); await this.#handleMilestoneCreatedEvent(event);
}); });
this.#DomainEvents.subscribe(StripeLiveEnabledEvent, async () => {
await this.#handleStripeEvent(StripeLiveEnabledEvent);
});
this.#DomainEvents.subscribe(StripeLiveDisabledEvent, async () => {
await this.#handleStripeEvent(StripeLiveDisabledEvent);
});
} }
}; };

View File

@ -8,6 +8,7 @@ const DomainEventsAnalytics = require('../../../../../core/server/services/segme
// Stubbed dependencies // Stubbed dependencies
const DomainEvents = require('@tryghost/domain-events'); const DomainEvents = require('@tryghost/domain-events');
const {MilestoneCreatedEvent} = require('@tryghost/milestones'); const {MilestoneCreatedEvent} = require('@tryghost/milestones');
const {StripeLiveEnabledEvent, StripeLiveDisabledEvent} = require('@tryghost/members-stripe-service').events;
describe('DomainEventsAnalytics', function () { describe('DomainEventsAnalytics', function () {
describe('Constructor', function () { describe('Constructor', function () {
@ -54,7 +55,7 @@ describe('DomainEventsAnalytics', function () {
}); });
domainEventsAnalytics.subscribeToEvents(); domainEventsAnalytics.subscribeToEvents();
assert(domainEventsStub.callCount === 1); assert(domainEventsStub.callCount === 3);
assert(loggingStub.callCount === 0); assert(loggingStub.callCount === 0);
}); });
@ -173,6 +174,59 @@ describe('DomainEventsAnalytics', function () {
assert(loggingStub.callCount === 0); 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 () { it('can handle tracking errors', async function () {
let error = new Error('Test error'); let error = new Error('Test error');
domainEventsAnalytics = new DomainEventsAnalytics({ domainEventsAnalytics = new DomainEventsAnalytics({