mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
Removed ModelEventsAnalytics
refs https://linear.app/tryghost/issue/BIZ-6/[wip]-update-segment-events - With the removal of the `integration.added` event, we have no more model events remaining to listen to for our analytics - Removal of the function entirely seems the easier and more straightforward way
This commit is contained in:
parent
e4b9305e2a
commit
75874151fd
@ -1,93 +0,0 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
/**
|
||||
* @typedef {import('@tryghost/logging')} logging
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('analytics-node')} analytics
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} IExceptionHandler
|
||||
* @prop {(err: Error) => void} captureException
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {import('../../lib/common/events')} events
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {object} IModelEventsAnalytics
|
||||
* @param {analytics} analytics
|
||||
* @param {logging} logging
|
||||
* @param {object} trackDefaults
|
||||
* @param {string} prefix
|
||||
* @param {IExceptionHandler} exceptionHandler
|
||||
* @param {events} events
|
||||
* @prop {} subscribeToEvents
|
||||
*/
|
||||
|
||||
/**
|
||||
* Listens to model events to layer on analytics - also uses the "fake" theme.uploaded
|
||||
* event from the theme API
|
||||
*/
|
||||
module.exports = class ModelEventsAnalytics {
|
||||
/** @type {analytics} */
|
||||
#analytics;
|
||||
/** @type {object} */
|
||||
#trackDefaults;
|
||||
/** @type {string} */
|
||||
#prefix;
|
||||
/** @type {IExceptionHandler} */
|
||||
#exceptionHandler;
|
||||
/** @type {events} */
|
||||
#events;
|
||||
/** @type {logging} */
|
||||
#logging;
|
||||
|
||||
/**
|
||||
* @type {Array<{event: string, name: string, data?: object}>}
|
||||
*/
|
||||
#toTrack = [
|
||||
{
|
||||
event: 'integration.added',
|
||||
name: 'Custom Integration Added'
|
||||
}
|
||||
];
|
||||
|
||||
constructor(deps) {
|
||||
this.#analytics = deps.analytics;
|
||||
this.#trackDefaults = deps.trackDefaults;
|
||||
this.#prefix = deps.prefix;
|
||||
this.#exceptionHandler = deps.exceptionHandler;
|
||||
this.#events = deps.events;
|
||||
this.#logging = deps.logging;
|
||||
}
|
||||
|
||||
async #handleEvent(event) {
|
||||
try {
|
||||
this.#analytics.track(event);
|
||||
} catch (err) {
|
||||
this.#logging.error(err);
|
||||
this.#exceptionHandler.captureException(err);
|
||||
}
|
||||
}
|
||||
|
||||
subscribeToEvents() {
|
||||
this.#toTrack.forEach(({event, name, data = {}}) => {
|
||||
this.#events.on(event, async (eventData = {}) => {
|
||||
// extract desired properties from eventData and rename keys if necessary
|
||||
const mappedData = _.mapValues(data || {}, v => eventData[v]);
|
||||
|
||||
const eventToTrack = {
|
||||
...this.#trackDefaults,
|
||||
event: this.#prefix + name,
|
||||
...mappedData
|
||||
};
|
||||
|
||||
await this.#handleEvent(eventToTrack);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
@ -3,9 +3,7 @@ const config = require('../../../shared/config');
|
||||
const sentry = require('../../../shared/sentry');
|
||||
const logging = require('@tryghost/logging');
|
||||
const DomainEvents = require('@tryghost/domain-events');
|
||||
const events = require('../../lib/common/events');
|
||||
|
||||
const ModelEventsAnalytics = require('./ModelEventsAnalytics');
|
||||
const DomainEventsAnalytics = require('./DomainEventsAnalytics');
|
||||
|
||||
module.exports.init = function () {
|
||||
@ -22,18 +20,6 @@ module.exports.init = function () {
|
||||
logging
|
||||
});
|
||||
|
||||
const modelEventsAnalytics = new ModelEventsAnalytics({
|
||||
analytics,
|
||||
trackDefaults,
|
||||
prefix,
|
||||
exceptionHandler: sentry,
|
||||
events,
|
||||
logging
|
||||
});
|
||||
|
||||
// Listen to model events
|
||||
modelEventsAnalytics.subscribeToEvents();
|
||||
|
||||
// Listen to domain events
|
||||
subscribeToDomainEvents.subscribeToEvents();
|
||||
};
|
||||
|
@ -1,130 +0,0 @@
|
||||
const assert = require('assert/strict');
|
||||
const sinon = require('sinon');
|
||||
|
||||
// To test
|
||||
const ModelEventsAnalytics = require('../../../../../core/server/services/segment/ModelEventsAnalytics');
|
||||
|
||||
// Stubbed dependencies
|
||||
const events = require('../../../../../core/server/lib/common/events');
|
||||
|
||||
describe('ModelEventsAnalytics', function () {
|
||||
describe('Constructor', function () {
|
||||
it('doesn\'t throw', function () {
|
||||
new ModelEventsAnalytics({});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Model events analytics service', function () {
|
||||
let modelEventsAnalytics;
|
||||
let analyticsStub;
|
||||
let exceptionHandlerStub;
|
||||
let loggingStub;
|
||||
let eventStub;
|
||||
|
||||
beforeEach(function () {
|
||||
analyticsStub = sinon.stub();
|
||||
exceptionHandlerStub = sinon.stub();
|
||||
loggingStub = sinon.stub();
|
||||
eventStub = sinon.stub();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
sinon.restore();
|
||||
});
|
||||
|
||||
it('subscribes to events', async function () {
|
||||
modelEventsAnalytics = new ModelEventsAnalytics({
|
||||
analytics: analyticsStub,
|
||||
trackDefaults: {
|
||||
userId: '1234',
|
||||
properties: {email: 'john@test.com'}
|
||||
},
|
||||
prefix: 'Pro: ',
|
||||
exceptionHandler: {
|
||||
captureException: exceptionHandlerStub
|
||||
},
|
||||
events: {
|
||||
on: eventStub
|
||||
},
|
||||
logging: {
|
||||
error: loggingStub
|
||||
}
|
||||
});
|
||||
|
||||
modelEventsAnalytics.subscribeToEvents();
|
||||
|
||||
// as per #toTrack Array
|
||||
assert(eventStub.callCount === 1);
|
||||
assert(loggingStub.callCount === 0);
|
||||
});
|
||||
|
||||
it('handles common model events', async function () {
|
||||
modelEventsAnalytics = new ModelEventsAnalytics({
|
||||
analytics: {
|
||||
track: analyticsStub
|
||||
},
|
||||
trackDefaults: {
|
||||
userId: '1234',
|
||||
properties: {email: 'john@test.com'}
|
||||
},
|
||||
prefix: 'Pro: ',
|
||||
exceptionHandler: {
|
||||
captureException: exceptionHandlerStub
|
||||
},
|
||||
events,
|
||||
logging: {
|
||||
error: loggingStub
|
||||
}
|
||||
});
|
||||
|
||||
modelEventsAnalytics.subscribeToEvents();
|
||||
|
||||
events.emit('theme.uploaded', {name: 'Custom Super Theme'});
|
||||
events.emit('post.published');
|
||||
events.emit('page.published');
|
||||
events.emit('integration.added');
|
||||
|
||||
assert(analyticsStub.callCount === 1);
|
||||
assert(analyticsStub.getCall(0).calledWithExactly({
|
||||
userId: '1234',
|
||||
properties: {email: 'john@test.com'},
|
||||
event: 'Pro: Custom Integration Added'
|
||||
}));
|
||||
|
||||
events.emit('post.unpublished');
|
||||
|
||||
// Analytics should not be called again
|
||||
assert(analyticsStub.callCount === 1);
|
||||
assert(loggingStub.callCount === 0);
|
||||
});
|
||||
|
||||
it('can handle tracking errors', async function () {
|
||||
let error = new Error('Test error');
|
||||
modelEventsAnalytics = new ModelEventsAnalytics({
|
||||
analytics: {
|
||||
track: analyticsStub.throws(error)
|
||||
},
|
||||
trackDefaults: {},
|
||||
prefix: '',
|
||||
exceptionHandler: {
|
||||
captureException: exceptionHandlerStub
|
||||
},
|
||||
events,
|
||||
logging: {
|
||||
error: loggingStub
|
||||
}
|
||||
});
|
||||
|
||||
modelEventsAnalytics.subscribeToEvents();
|
||||
|
||||
try {
|
||||
events.emit('post.published');
|
||||
} catch (err) {
|
||||
assert(analyticsStub.callCount === 1);
|
||||
assert(exceptionHandlerStub.callCount === 1);
|
||||
assert(exceptionHandlerStub.calledWith(error));
|
||||
assert(loggingStub.callCount === 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user