2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Analytics = require('analytics-node');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../shared/config');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2021-02-24 18:51:15 +03:00
|
|
|
const sentry = require('../shared/sentry');
|
2021-07-07 17:49:45 +03:00
|
|
|
|
|
|
|
// Listens to model events to layer on analytics - also uses the "fake" theme.uploaded event from the theme API
|
2021-04-27 16:18:04 +03:00
|
|
|
const events = require('./lib/common/events');
|
2017-08-22 14:23:23 +03:00
|
|
|
|
|
|
|
module.exports.init = function () {
|
2020-04-30 22:26:12 +03:00
|
|
|
const analytics = new Analytics(config.get('segment:key'));
|
2020-04-29 18:44:27 +03:00
|
|
|
const trackDefaults = config.get('segment:trackDefaults') || {};
|
|
|
|
const prefix = config.get('segment:prefix') || '';
|
2017-08-22 14:23:23 +03:00
|
|
|
|
2020-04-30 22:26:12 +03:00
|
|
|
const toTrack = [
|
2017-08-22 14:23:23 +03:00
|
|
|
{
|
|
|
|
event: 'post.published',
|
2019-02-05 19:38:40 +03:00
|
|
|
name: 'Post Published'
|
2017-08-22 14:23:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
event: 'page.published',
|
2019-02-05 19:38:40 +03:00
|
|
|
name: 'Page Published'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
event: 'theme.uploaded',
|
2021-02-12 12:19:17 +03:00
|
|
|
name: 'Theme Uploaded',
|
|
|
|
// {keyOnSuppliedEventData: keyOnTrackedEventData}
|
|
|
|
// - used to extract specific properties from event data and give them meaningful names
|
|
|
|
data: {name: 'name'}
|
2019-03-11 17:28:17 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
event: 'integration.added',
|
|
|
|
name: 'Custom Integration Added'
|
2017-08-22 14:23:23 +03:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
_.each(toTrack, function (track) {
|
2021-02-12 12:19:17 +03:00
|
|
|
events.on(track.event, function (eventData = {}) {
|
|
|
|
// extract desired properties from eventData and rename keys if necessary
|
|
|
|
const data = _.mapValues(track.data || {}, v => eventData[v]);
|
|
|
|
|
2021-02-24 18:51:15 +03:00
|
|
|
try {
|
|
|
|
analytics.track(_.extend(trackDefaults, data, {event: prefix + track.name}));
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(err);
|
|
|
|
sentry.captureException(err);
|
|
|
|
}
|
2017-08-22 14:23:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|