Ghost/core/server/analytics-events.js
Daniel Lockyer 0db07b57a0 Wrapped analytics tracker in try-catch
no issue

- the analytice-node v4 update contains a breaking change that throws an
  exception if the message is over 32kb
- I'm pretty sure we won't hit this, but it's good to track these errors
  anyway and stop Ghost from bombing out if there's an issue
- this commit wraps the tracking call and adds Sentry + logging to the
  error
2021-02-24 15:57:38 +00:00

49 lines
1.6 KiB
JavaScript

const _ = require('lodash');
const Analytics = require('analytics-node');
const config = require('../shared/config');
const logging = require('../shared/logging');
const sentry = require('../shared/sentry');
const {events} = require('./lib/common');
module.exports.init = function () {
const analytics = new Analytics(config.get('segment:key'));
const trackDefaults = config.get('segment:trackDefaults') || {};
const prefix = config.get('segment:prefix') || '';
const toTrack = [
{
event: 'post.published',
name: 'Post Published'
},
{
event: 'page.published',
name: 'Page Published'
},
{
event: 'theme.uploaded',
name: 'Theme Uploaded',
// {keyOnSuppliedEventData: keyOnTrackedEventData}
// - used to extract specific properties from event data and give them meaningful names
data: {name: 'name'}
},
{
event: 'integration.added',
name: 'Custom Integration Added'
}
];
_.each(toTrack, function (track) {
events.on(track.event, function (eventData = {}) {
// extract desired properties from eventData and rename keys if necessary
const data = _.mapValues(track.data || {}, v => eventData[v]);
try {
analytics.track(_.extend(trackDefaults, data, {event: prefix + track.name}));
} catch (err) {
logging.error(err);
sentry.captureException(err);
}
});
});
};