Ghost/ghost/members-events-service/lib/event-storage.js
Rishabh bb0d900937 Handled storing referrer information in DB
refs https://github.com/TryGhost/Team/issues/1931

- stores `referrer_source`, `referrer_medium` and `referrer_url` in event tables for new members and paid subscriptions
2022-09-21 19:32:18 +05:30

72 lines
2.5 KiB
JavaScript

const {MemberCreatedEvent, SubscriptionCreatedEvent} = require('@tryghost/member-events');
/**
* Store events in the database
*/
class EventStorage {
/**
*
* @param {Object} deps
* @param {Object} deps.labsService
* @param {Object} deps.models
* @param {Object} deps.models.MemberCreatedEvent
* @param {Object} deps.models.SubscriptionCreatedEvent
*/
constructor({labsService, models}) {
this.models = models;
this.labsService = labsService;
}
/**
* Subscribe to events of this domainEvents service
* @param {Object} domainEvents The DomainEvents service
*/
subscribe(domainEvents) {
domainEvents.subscribe(MemberCreatedEvent, async (event) => {
let attribution = event.data.attribution;
if (!this.labsService.isSet('memberAttribution')){
// Prevent storing attribution
// Can replace this later with a privacy toggle
attribution = {};
}
await this.models.MemberCreatedEvent.add({
member_id: event.data.memberId,
created_at: event.timestamp,
attribution_id: attribution?.id ?? null,
attribution_url: attribution?.url ?? null,
attribution_type: attribution?.type ?? null,
source: event.data.source,
referrer_source: attribution?.refSource ?? null,
referrer_medium: attribution?.refMedium ?? null,
referrer_url: attribution?.refUrl ?? null
});
});
domainEvents.subscribe(SubscriptionCreatedEvent, async (event) => {
let attribution = event.data.attribution;
if (!this.labsService.isSet('memberAttribution')){
// Prevent storing attribution
// Can replace this later with a privacy toggle
attribution = {};
}
await this.models.SubscriptionCreatedEvent.add({
member_id: event.data.memberId,
subscription_id: event.data.subscriptionId,
created_at: event.timestamp,
attribution_id: attribution?.id ?? null,
attribution_url: attribution?.url ?? null,
attribution_type: attribution?.type ?? null,
referrer_source: attribution?.refSource ?? null,
referrer_medium: attribution?.refMedium ?? null,
referrer_url: attribution?.refUrl ?? null
});
});
}
}
module.exports = EventStorage;