mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
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:
parent
48f9e85e09
commit
d27bb8f075
@ -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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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({
|
||||||
|
Loading…
Reference in New Issue
Block a user