UNPKG

forest-express

Version:

Official package for all Forest Express Lianas

47 lines (45 loc) 1.76 kB
"use strict"; var moment = require('moment'); var logger = require('../../../services/logger'); function MixpanelEventsGetter(Implementation, params, options, integrationInfo) { var MixpanelExport = options.integrations.mixpanel.mixpanel; var panel = new MixpanelExport({ api_key: options.integrations.mixpanel.apiKey, api_secret: options.integrations.mixpanel.apiSecret }); this.perform = function () { var collectionFieldName = integrationInfo.field; var collectionModel = integrationInfo.collection; return Implementation.Mixpanel.getUser(collectionModel, params.recordId).then(function (user) { var script = "function main() {\n return People().filter(function (user) {\n return user.properties.$email == '".concat(user[collectionFieldName], "';\n });\n }"); // NOTICE: The mixpanel's API doesn't allow to retrieve events that are // more than 60 days old. var fromDate = moment().subtract(60, 'days'); var toDate = moment(); return panel.get('jql', { script: script }).then(function (result) { if (!result || !result[0]) { return { results: { events: [] } }; } return panel.get('stream/query', { from_date: fromDate.format('YYYY-MM-DD'), to_date: toDate.format('YYYY-MM-DD'), distinct_ids: [result[0].distinct_id], limit: 100 }); }).then(function (result) { if (result.error) { logger.error('Cannot retrieve mixpanel events: ', result.error); return []; } return result.results.events.reverse(); }); }); }; } module.exports = MixpanelEventsGetter;