From 1c4f2325b4dd0df4b68c59c924a2e1e4e6bd2a03 Mon Sep 17 00:00:00 2001 From: adityamittal3107 Date: Tue, 9 Sep 2025 11:35:41 +0530 Subject: [PATCH] Added mixpanel events for CBCA --- src/embed/ts-embed.spec.ts | 74 ++++++++++++++++++++++++++++++++++++++ src/embed/ts-embed.ts | 10 ++++-- src/mixpanel-service.ts | 1 + 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/embed/ts-embed.spec.ts b/src/embed/ts-embed.spec.ts index 1c8708d1..c03ccb92 100644 --- a/src/embed/ts-embed.spec.ts +++ b/src/embed/ts-embed.spec.ts @@ -1143,6 +1143,80 @@ describe('Unit test case for ts embed', () => { expect(appInitData.customActions[1].name).toBe('Valid Action'); }); }); + + test('should trigger CODE_BASED_CUSTOM_ACTION_COUNT mixpanel event with correct count', async () => { + const mockEmbedEventPayload = { + type: EmbedEvent.APP_INIT, + data: {}, + }; + + const customActions = [ + { + id: 'action1', + name: 'Custom Action 1', + target: CustomActionTarget.LIVEBOARD, + position: CustomActionsPosition.PRIMARY, + metadataIds: { liveboardIds: ['lb123'] } + }, + { + id: 'action2', + name: 'Custom Action 2', + target: CustomActionTarget.VIZ, + position: CustomActionsPosition.MENU, + metadataIds: { vizIds: ['viz456'] } + } + ]; + + const searchEmbed = new SearchEmbed(getRootEl(), { + ...defaultViewConfig, + customActions + }); + + searchEmbed.render(); + const mockPort: any = { + postMessage: jest.fn(), + }; + await executeAfterWait(() => { + const iframe = getIFrameEl(); + postMessageToParent(iframe.contentWindow, mockEmbedEventPayload, mockPort); + }); + + await executeAfterWait(() => { + // Verify that CODE_BASED_CUSTOM_ACTION_COUNT mixpanel event is called with correct count + expect(mockMixPanelEvent).toHaveBeenCalledWith( + MIXPANEL_EVENT.CODE_BASED_CUSTOM_ACTION_COUNT, + { + count: 2, + } + ); + }); + }); + + test('should not trigger CODE_BASED_CUSTOM_ACTION_COUNT mixpanel event when no custom actions', async () => { + const mockEmbedEventPayload = { + type: EmbedEvent.APP_INIT, + data: {}, + }; + + const searchEmbed = new SearchEmbed(getRootEl(), { + ...defaultViewConfig, + // No customActions provided + }); + + searchEmbed.render(); + const mockPort: any = { + postMessage: jest.fn(), + }; + await executeAfterWait(() => { + const iframe = getIFrameEl(); + postMessageToParent(iframe.contentWindow, mockEmbedEventPayload, mockPort); + }); + // Verify that CODE_BASED_CUSTOM_ACTION_COUNT mixpanel event is NOT called when count is 0 + expect(mockMixPanelEvent).not.toHaveBeenCalledWith( + MIXPANEL_EVENT.CODE_BASED_CUSTOM_ACTION_COUNT, + expect.anything() + ); + }); }); describe('Token fetch fails in cookieless authentication authType', () => { diff --git a/src/embed/ts-embed.ts b/src/embed/ts-embed.ts index 546cb222..977ac738 100644 --- a/src/embed/ts-embed.ts +++ b/src/embed/ts-embed.ts @@ -381,10 +381,16 @@ export class TsEmbed { protected async getDefaultAppInitData(): Promise { const authToken = await this.getAuthTokenForCookielessInit(); - const customActionsResult = getCustomActions([ + const customActions = [ ...(this.viewConfig.customActions || []), ...(this.embedConfig.customActions || []) - ]); + ]; + const customActionsResult = getCustomActions(customActions); + if (customActions.length > 0) { + uploadMixpanelEvent(MIXPANEL_EVENT.CODE_BASED_CUSTOM_ACTION_COUNT, { + count: customActions.length, + }); + } if (customActionsResult.errors.length > 0) { this.handleError({ type: 'CUSTOM_ACTION_VALIDATION', diff --git a/src/mixpanel-service.ts b/src/mixpanel-service.ts index 5da17db5..f341121e 100644 --- a/src/mixpanel-service.ts +++ b/src/mixpanel-service.ts @@ -25,6 +25,7 @@ export const MIXPANEL_EVENT = { VISUAL_SDK_IFRAME_LOAD_PERFORMANCE: 'visual-sdk-iframe-load-performance', VISUAL_SDK_EMBED_CREATE: 'visual-sdk-embed-create', VERCEL_INTEGRATION_COMPLETED: 'vercel-integration-completed', + CODE_BASED_CUSTOM_ACTION_COUNT: 'code-based-custom-action-count', }; let isMixpanelInitialized = false;