Ghost/ghost/member-analytics-service/lib/EventHandler.js

35 lines
997 B
JavaScript
Raw Normal View History

const DomainEvents = require('@tryghost/domain-events');
const {MemberEntryViewEvent} = 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);
});
}
}
module.exports = EventHandler;