Setup basic event tracking framework

MAILPOET-6365
This commit is contained in:
Oluwaseun Olorunsola
2024-12-18 11:19:59 +01:00
committed by Oluwaseun Olorunsola
parent 735baa2e73
commit 69aa7b906e
6 changed files with 59 additions and 4 deletions

View File

@ -25,7 +25,11 @@ import { VisualEditor } from './visual-editor/visual-editor';
import { TemplateSelection } from '../template-select';
import { recordEvent } from '../../events';
export function Layout() {
recordEvent( 'editor-layout-loaded' );
const {
isFullscreenActive,
isSidebarOpened,

View File

@ -8,6 +8,7 @@ import { InnerEditor } from './components/block-editor';
import { createStore, storeName } from './store';
import { initHooks } from './editor-hooks';
import { KeyboardShortcuts } from './components/keybord-shortcuts';
import { initEventCollector } from './events';
import './index.scss';
function Editor() {
@ -42,6 +43,7 @@ export function initialize( elementId: string ) {
if ( ! container ) {
return;
}
initEventCollector();
createStore();
initializeLayout();
initBlocks();

View File

@ -0,0 +1,16 @@
import { doAction } from '@wordpress/hooks';
import { EMAIL_STRING, dispatcher } from './event-pipeline';
const eventListenerHandler = ( eventData ) => {
doAction( 'mailpoet-email-editor-events', eventData.detail );
};
const initEventCollector = () => {
dispatcher.addEventListener( EMAIL_STRING, eventListenerHandler );
};
window.addEventListener( 'unload', function () {
dispatcher.removeEventListener( EMAIL_STRING, eventListenerHandler );
} );
export { initEventCollector };

View File

@ -0,0 +1,18 @@
const EMAIL_STRING = 'email-editor-events';
const dispatcher = new EventTarget();
const recordEvent = ( name: string, data = {} ) => {
const recordedData = typeof data !== 'object' ? { data } : data;
const eventData = {
name: `${ EMAIL_STRING }_${ name }`,
...recordedData,
};
dispatcher.dispatchEvent(
new CustomEvent( EMAIL_STRING, { detail: eventData } )
);
};
export { recordEvent, EMAIL_STRING, dispatcher };

View File

@ -0,0 +1,2 @@
export { recordEvent } from './event-pipeline';
export * from './event-collector';

View File

@ -25,6 +25,7 @@ import {
parse,
} from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities';
import { recordEvent } from '../events';
export const toggleFeature =
( feature: Feature ) =>
@ -65,19 +66,26 @@ export function updateSendPreviewEmail( toEmail: string ) {
export const openSidebar =
( key = mainSidebarDocumentTab ) =>
( { registry } ): unknown =>
registry
( { registry } ): unknown => {
recordEvent( 'editor-sidebar-opened' );
return registry
.dispatch( interfaceStore )
.enableComplementaryArea( storeName, key );
};
export const closeSidebar =
() =>
( { registry } ): unknown =>
registry
( { registry } ): unknown => {
recordEvent( 'editor-sidebar-closed' );
return registry
.dispatch( interfaceStore )
.disableComplementaryArea( storeName );
};
export function toggleSettingsSidebarActiveTab( activeTab: string ) {
recordEvent( 'editor-settings-sidebar-active-tab', { activeTab } );
return {
type: 'TOGGLE_SETTINGS_SIDEBAR_ACTIVE_TAB',
state: { activeTab } as Partial< State[ 'settingsSidebar' ] >,
@ -96,6 +104,7 @@ export function* saveEditedEmail() {
);
result.then( () => {
recordEvent( 'editor-content-saved', { postId } );
void dispatch( noticesStore ).createErrorNotice(
__( 'Email saved!', 'mailpoet' ),
{
@ -107,6 +116,7 @@ export function* saveEditedEmail() {
} );
result.catch( () => {
recordEvent( 'editor-content-not-saved', { postId } );
void dispatch( noticesStore ).createErrorNotice(
__(
'The email could not be saved. Please, clear browser cache and reload the page. If the problem persists, duplicate the email and try again.',
@ -142,6 +152,7 @@ export function* updateEmailMailPoetProperty( name: string, value: string ) {
},
}
);
recordEvent( 'updated-mailpoet-email-property', { postId } );
}
export const setTemplateToPost =
@ -192,7 +203,9 @@ export function* requestSendingNewsletterPreview(
isSendingPreviewEmail: false,
},
};
recordEvent( 'sent-preview-email', { postId, email } );
} catch ( errorResponse ) {
recordEvent( 'error-sent-preview-email', { email } );
yield {
type: 'CHANGE_PREVIEW_STATE',
state: {