2022-08-10 13:16:55 +03:00
|
|
|
const MailgunClient = require('@tryghost/mailgun-client');
|
2020-11-26 16:09:38 +03:00
|
|
|
|
|
|
|
const EVENT_FILTER = 'delivered OR opened OR failed OR unsubscribed OR complained';
|
|
|
|
const PAGE_LIMIT = 300;
|
|
|
|
const DEFAULT_TAGS = ['bulk-email'];
|
|
|
|
|
2021-01-16 19:22:52 +03:00
|
|
|
class EmailAnalyticsProviderMailgun {
|
2022-08-11 11:26:41 +03:00
|
|
|
mailgunClient;
|
2020-11-26 16:09:38 +03:00
|
|
|
|
2022-08-10 13:16:55 +03:00
|
|
|
constructor({config, settings}) {
|
2022-08-11 11:26:41 +03:00
|
|
|
this.mailgunClient = new MailgunClient({config, settings});
|
2022-08-10 13:16:55 +03:00
|
|
|
this.tags = [...DEFAULT_TAGS];
|
2020-11-26 16:09:38 +03:00
|
|
|
|
2022-08-10 13:16:55 +03:00
|
|
|
if (config.get('bulkEmail:mailgun:tag')) {
|
|
|
|
this.tags.push(config.get('bulkEmail:mailgun:tag'));
|
2020-11-26 16:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
/**
|
|
|
|
* Fetch from the last known timestamp-TRUST_THRESHOLD then work forwards
|
|
|
|
* through pages until we get a blank response. This lets us get events
|
|
|
|
* quicker than the TRUST_THRESHOLD
|
|
|
|
*
|
|
|
|
* @param {Function} batchHandler
|
|
|
|
* @param {Object} [options]
|
2023-02-20 18:44:13 +03:00
|
|
|
* @param {Number} [options.maxEvents] Not a strict maximum. We stop fetching after we reached the maximum AND received at least one event after begin (not equal) to prevent deadlocks.
|
|
|
|
* @param {Date} [options.begin]
|
|
|
|
* @param {Date} [options.end]
|
2022-08-11 11:26:41 +03:00
|
|
|
*/
|
2023-02-20 18:44:13 +03:00
|
|
|
fetchLatest(batchHandler, options) {
|
2020-11-26 16:09:38 +03:00
|
|
|
const mailgunOptions = {
|
|
|
|
limit: PAGE_LIMIT,
|
|
|
|
event: EVENT_FILTER,
|
|
|
|
tags: this.tags.join(' AND '),
|
2023-02-20 18:44:13 +03:00
|
|
|
begin: options.begin ? options.begin.getTime() / 1000 : undefined,
|
|
|
|
end: options.end ? options.end.getTime() / 1000 : undefined,
|
2020-11-26 16:09:38 +03:00
|
|
|
ascending: 'yes'
|
|
|
|
};
|
|
|
|
|
2023-02-20 18:44:13 +03:00
|
|
|
return this.#fetchAnalytics(mailgunOptions, batchHandler, {
|
|
|
|
maxEvents: options.maxEvents
|
|
|
|
});
|
2020-11-26 16:09:38 +03:00
|
|
|
}
|
|
|
|
|
2022-08-11 11:26:41 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} mailgunOptions
|
|
|
|
* @param {Number} mailgunOptions.limit
|
|
|
|
* @param {String} mailgunOptions.event
|
|
|
|
* @param {String} mailgunOptions.tags
|
|
|
|
* @param {String} [mailgunOptions.begin]
|
|
|
|
* @param {String} [mailgunOptions.ascending]
|
|
|
|
* @param {Function} batchHandler
|
|
|
|
* @param {Object} [options]
|
2023-02-20 18:44:13 +03:00
|
|
|
* @param {Number} [options.maxEvents] Not a strict maximum. We stop fetching after we reached the maximum AND received at least one event after begin (not equal) to prevent deadlocks.
|
2022-08-11 11:26:41 +03:00
|
|
|
*/
|
2022-08-10 13:16:55 +03:00
|
|
|
async #fetchAnalytics(mailgunOptions, batchHandler, options) {
|
2023-02-20 18:44:13 +03:00
|
|
|
return await this.mailgunClient.fetchEvents(mailgunOptions, batchHandler, options);
|
2020-11-26 16:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:22:52 +03:00
|
|
|
module.exports = EmailAnalyticsProviderMailgun;
|