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 { TemplateSelection } from '../template-select';
import { recordEvent } from '../../events';
export function Layout() { export function Layout() {
recordEvent( 'editor-layout-loaded' );
const { const {
isFullscreenActive, isFullscreenActive,
isSidebarOpened, isSidebarOpened,

View File

@ -8,6 +8,7 @@ import { InnerEditor } from './components/block-editor';
import { createStore, storeName } from './store'; import { createStore, storeName } from './store';
import { initHooks } from './editor-hooks'; import { initHooks } from './editor-hooks';
import { KeyboardShortcuts } from './components/keybord-shortcuts'; import { KeyboardShortcuts } from './components/keybord-shortcuts';
import { initEventCollector } from './events';
import './index.scss'; import './index.scss';
function Editor() { function Editor() {
@ -42,6 +43,7 @@ export function initialize( elementId: string ) {
if ( ! container ) { if ( ! container ) {
return; return;
} }
initEventCollector();
createStore(); createStore();
initializeLayout(); initializeLayout();
initBlocks(); 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, parse,
} from '@wordpress/blocks'; } from '@wordpress/blocks';
import { decodeEntities } from '@wordpress/html-entities'; import { decodeEntities } from '@wordpress/html-entities';
import { recordEvent } from '../events';
export const toggleFeature = export const toggleFeature =
( feature: Feature ) => ( feature: Feature ) =>
@ -65,19 +66,26 @@ export function updateSendPreviewEmail( toEmail: string ) {
export const openSidebar = export const openSidebar =
( key = mainSidebarDocumentTab ) => ( key = mainSidebarDocumentTab ) =>
( { registry } ): unknown => ( { registry } ): unknown => {
registry recordEvent( 'editor-sidebar-opened' );
return registry
.dispatch( interfaceStore ) .dispatch( interfaceStore )
.enableComplementaryArea( storeName, key ); .enableComplementaryArea( storeName, key );
};
export const closeSidebar = export const closeSidebar =
() => () =>
( { registry } ): unknown => ( { registry } ): unknown => {
registry recordEvent( 'editor-sidebar-closed' );
return registry
.dispatch( interfaceStore ) .dispatch( interfaceStore )
.disableComplementaryArea( storeName ); .disableComplementaryArea( storeName );
};
export function toggleSettingsSidebarActiveTab( activeTab: string ) { export function toggleSettingsSidebarActiveTab( activeTab: string ) {
recordEvent( 'editor-settings-sidebar-active-tab', { activeTab } );
return { return {
type: 'TOGGLE_SETTINGS_SIDEBAR_ACTIVE_TAB', type: 'TOGGLE_SETTINGS_SIDEBAR_ACTIVE_TAB',
state: { activeTab } as Partial< State[ 'settingsSidebar' ] >, state: { activeTab } as Partial< State[ 'settingsSidebar' ] >,
@ -96,6 +104,7 @@ export function* saveEditedEmail() {
); );
result.then( () => { result.then( () => {
recordEvent( 'editor-content-saved', { postId } );
void dispatch( noticesStore ).createErrorNotice( void dispatch( noticesStore ).createErrorNotice(
__( 'Email saved!', 'mailpoet' ), __( 'Email saved!', 'mailpoet' ),
{ {
@ -107,6 +116,7 @@ export function* saveEditedEmail() {
} ); } );
result.catch( () => { result.catch( () => {
recordEvent( 'editor-content-not-saved', { postId } );
void dispatch( noticesStore ).createErrorNotice( 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.', '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 = export const setTemplateToPost =
@ -192,7 +203,9 @@ export function* requestSendingNewsletterPreview(
isSendingPreviewEmail: false, isSendingPreviewEmail: false,
}, },
}; };
recordEvent( 'sent-preview-email', { postId, email } );
} catch ( errorResponse ) { } catch ( errorResponse ) {
recordEvent( 'error-sent-preview-email', { email } );
yield { yield {
type: 'CHANGE_PREVIEW_STATE', type: 'CHANGE_PREVIEW_STATE',
state: { state: {