@atlaskit/editor-plugin-ncs-step-metrics
Version:
NcsStepMetrics plugin for @atlaskit/editor-core
88 lines (86 loc) • 4.21 kB
JavaScript
import { bind } from 'bind-event-listener';
import { fireAnalyticsEvent } from '@atlaskit/editor-common/analytics';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { getPayload } from './pm-plugins/utils/analytics';
import { getNcsSessionStepMetrics, clearNcsSessionStepMetrics, clearNcsActiveSession, checkForUnfinishedNcsSessions } from './pm-plugins/utils/session';
/**
* NCS Session Step Metrics plugin to be added to an `EditorPresetBuilder` and used with `ComposableEditor`
* from `@atlaskit/editor-core`.
*/
export var ncsStepMetricsPlugin = function ncsStepMetricsPlugin(_ref) {
var api = _ref.api;
var sessionId;
var createAnalyticsEvent;
return {
name: 'ncsStepMetrics',
pmPlugins: function pmPlugins() {
return [{
name: 'ncsStepMetricsPlugin',
plugin: function plugin() {
return new SafePlugin({
view: function view() {
var _api$collabEdit, _api$analytics;
api === null || api === void 0 || (_api$collabEdit = api.collabEdit) === null || _api$collabEdit === void 0 || (_api$collabEdit = _api$collabEdit.sharedState) === null || _api$collabEdit === void 0 || _api$collabEdit.onChange(function (_ref2) {
var nextSharedState = _ref2.nextSharedState;
if (nextSharedState.sessionId !== undefined && sessionId !== nextSharedState.sessionId) {
sessionId = nextSharedState.sessionId;
checkForUnfinishedNcsSessions(api);
}
});
var unsubscribeAnalytics = api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.sharedState) === null || _api$analytics === void 0 ? void 0 : _api$analytics.onChange(function (_ref3) {
var nextSharedState = _ref3.nextSharedState;
if (nextSharedState.createAnalyticsEvent) {
createAnalyticsEvent = nextSharedState.createAnalyticsEvent;
unsubscribeAnalytics === null || unsubscribeAnalytics === void 0 || unsubscribeAnalytics();
}
});
var analyticsEventSent = false;
var sendAnalyticsEvent = function sendAnalyticsEvent() {
if (analyticsEventSent || !createAnalyticsEvent || !sessionId) {
return;
}
analyticsEventSent = true;
var ncsSessionStepMetrics = getNcsSessionStepMetrics(sessionId);
if (!ncsSessionStepMetrics) {
return;
}
// At this point in the editor lifecycle, we no longer have access to the analytics api
// So we use the stored `createAnalyticsEvent` function to send the event
fireAnalyticsEvent(createAnalyticsEvent, {
immediate: true
})({
payload: getPayload(ncsSessionStepMetrics)
});
clearNcsSessionStepMetrics(sessionId);
};
var handleBeforeUnload = function handleBeforeUnload() {
// On beforeunload, we want to clear the active session
// So when the editor is re-initialized, it will send the stored analytics event
clearNcsActiveSession(sessionId);
};
var unbindBeforeUnload = bind(window, {
type: 'beforeunload',
listener: handleBeforeUnload
});
return {
destroy: function destroy() {
/**
* We use requestAnimationFrame to ensure that the editor has been unmounted
* before we send the analytics event.
*/
requestAnimationFrame(function () {
var akEditor = document.querySelector('.akEditor');
if (!akEditor) {
sendAnalyticsEvent();
unbindBeforeUnload();
}
});
}
};
}
});
}
}];
}
};
};