mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-11 09:53:32 +03:00
d12f7cd152
refs https://github.com/TryGhost/Team/issues/1054 This adds the missing listeners for the rest of the analytics events we'd like to capture.
95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
const DomainEvents = require('@tryghost/domain-events');
|
|
const {
|
|
MemberEntryViewEvent,
|
|
MemberUnsubscribeEvent,
|
|
MemberSignupEvent,
|
|
MemberPaidConverstionEvent,
|
|
MemberPaidCancellationEvent
|
|
} = require('@tryghost/member-events');
|
|
|
|
const AnalyticEvent = require('./AnalyticEvent');
|
|
|
|
class EventHandler {
|
|
/**
|
|
* @param {import('./AnalyticEventRepository')} repository
|
|
*/
|
|
constructor(repository) {
|
|
/** @private */
|
|
this.repository = repository;
|
|
}
|
|
|
|
/**
|
|
* Listens for member events and handles creating analytic events and storing them.
|
|
*/
|
|
setupSubscribers() {
|
|
DomainEvents.subscribe(MemberEntryViewEvent, async (ev) => {
|
|
const event = AnalyticEvent.create({
|
|
name: 'entry_view',
|
|
memberId: ev.data.memberId,
|
|
memberStatus: ev.data.memberStatus,
|
|
entryId: ev.data.entryId,
|
|
sourceUrl: ev.data.entryUrl,
|
|
timestamp: ev.timestamp
|
|
});
|
|
|
|
await this.repository.save(event);
|
|
});
|
|
|
|
DomainEvents.subscribe(MemberUnsubscribeEvent, async (ev) => {
|
|
const event = AnalyticEvent.create({
|
|
name: 'unsubscribe',
|
|
memberId: ev.data.memberId,
|
|
memberStatus: ev.data.memberStatus,
|
|
entryId: ev.data.entryId,
|
|
sourceUrl: ev.data.sourceUrl,
|
|
timestamp: ev.timestamp
|
|
});
|
|
|
|
await this.repository.save(event);
|
|
});
|
|
|
|
DomainEvents.subscribe(MemberSignupEvent, async (ev) => {
|
|
const event = AnalyticEvent.create({
|
|
name: 'signup',
|
|
memberId: ev.data.memberId,
|
|
memberStatus: 'free',
|
|
entryId: ev.data.entryId,
|
|
sourceUrl: ev.data.sourceUrl,
|
|
timestamp: ev.timestamp
|
|
});
|
|
|
|
await this.repository.save(event);
|
|
});
|
|
|
|
DomainEvents.subscribe(MemberPaidCancellationEvent, async (ev) => {
|
|
const event = AnalyticEvent.create({
|
|
name: 'paid_cancellation',
|
|
memberId: ev.data.memberId,
|
|
memberStatus: ev.data.memberStatus,
|
|
entryId: ev.data.entryId,
|
|
sourceUrl: ev.data.sourceUrl,
|
|
metadata: ev.data.subscriptionId,
|
|
timestamp: ev.timestamp
|
|
});
|
|
|
|
await this.repository.save(event);
|
|
});
|
|
|
|
DomainEvents.subscribe(MemberPaidConverstionEvent, async (ev) => {
|
|
const event = AnalyticEvent.create({
|
|
name: 'paid_conversion',
|
|
memberId: ev.data.memberId,
|
|
memberStatus: ev.data.memberStatus,
|
|
entryId: ev.data.entryId,
|
|
sourceUrl: ev.data.sourceUrl,
|
|
metadata: ev.data.subscriptionId,
|
|
timestamp: ev.timestamp
|
|
});
|
|
|
|
await this.repository.save(event);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = EventHandler;
|