@@ -3,18 +3,31 @@ import type { OverridedMixpanel } from 'mixpanel-browser'
33import { useCurrentUser } from '@/composables/auth/useCurrentUser'
44import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
55import { useWorkflowTemplatesStore } from '@/platform/workflow/templates/repositories/workflowTemplatesStore'
6+ import { app } from '@/scripts/app'
7+ import { useNodeDefStore } from '@/stores/nodeDefStore'
8+ import { NodeSourceType } from '@/types/nodeSource'
9+ import { reduceAllNodes } from '@/utils/graphTraversalUtil'
10+ import { normalizeSurveyResponses } from '../../utils/surveyNormalization'
611
712import type {
813 AuthMetadata ,
14+ CreditTopupMetadata ,
915 ExecutionContext ,
1016 ExecutionErrorMetadata ,
1117 ExecutionSuccessMetadata ,
18+ NodeSearchMetadata ,
19+ NodeSearchResultMetadata ,
20+ PageVisibilityMetadata ,
1221 RunButtonProperties ,
1322 SurveyResponses ,
23+ TabCountMetadata ,
1424 TelemetryEventName ,
1525 TelemetryEventProperties ,
1626 TelemetryProvider ,
17- TemplateMetadata
27+ TemplateFilterMetadata ,
28+ TemplateLibraryMetadata ,
29+ TemplateMetadata ,
30+ WorkflowImportMetadata
1831} from '../../types'
1932import { TelemetryEvents } from '../../types'
2033
@@ -123,6 +136,10 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
123136 this . trackEvent ( TelemetryEvents . USER_AUTH_COMPLETED , metadata )
124137 }
125138
139+ trackUserLoggedIn ( ) : void {
140+ this . trackEvent ( TelemetryEvents . USER_LOGGED_IN )
141+ }
142+
126143 trackSubscription ( event : 'modal_opened' | 'subscribe_clicked' ) : void {
127144 const eventName =
128145 event === 'modal_opened'
@@ -132,6 +149,20 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
132149 this . trackEvent ( eventName )
133150 }
134151
152+ trackMonthlySubscriptionSucceeded ( ) : void {
153+ this . trackEvent ( TelemetryEvents . MONTHLY_SUBSCRIPTION_SUCCEEDED )
154+ }
155+
156+ trackApiCreditTopupButtonPurchaseClicked ( amount : number ) : void {
157+ const metadata : CreditTopupMetadata = {
158+ credit_amount : amount
159+ }
160+ this . trackEvent (
161+ TelemetryEvents . API_CREDIT_TOPUP_BUTTON_PURCHASE_CLICKED ,
162+ metadata
163+ )
164+ }
165+
135166 trackRunButton ( options ?: { subscribe_to_run ?: boolean } ) : void {
136167 const executionContext = this . getExecutionContext ( )
137168
@@ -153,7 +184,21 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
153184 ? TelemetryEvents . USER_SURVEY_OPENED
154185 : TelemetryEvents . USER_SURVEY_SUBMITTED
155186
156- this . trackEvent ( eventName , responses )
187+ // Apply normalization to survey responses
188+ const normalizedResponses = responses
189+ ? normalizeSurveyResponses ( responses )
190+ : undefined
191+
192+ this . trackEvent ( eventName , normalizedResponses )
193+
194+ // If this is a survey submission, also set user properties with normalized data
195+ if ( stage === 'submitted' && normalizedResponses && this . mixpanel ) {
196+ try {
197+ this . mixpanel . people . set ( normalizedResponses )
198+ } catch ( error ) {
199+ console . error ( 'Failed to set survey user properties:' , error )
200+ }
201+ }
157202 }
158203
159204 trackEmailVerification ( stage : 'opened' | 'requested' | 'completed' ) : void {
@@ -178,6 +223,34 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
178223 this . trackEvent ( TelemetryEvents . TEMPLATE_WORKFLOW_OPENED , metadata )
179224 }
180225
226+ trackTemplateLibraryOpened ( metadata : TemplateLibraryMetadata ) : void {
227+ this . trackEvent ( TelemetryEvents . TEMPLATE_LIBRARY_OPENED , metadata )
228+ }
229+
230+ trackWorkflowImported ( metadata : WorkflowImportMetadata ) : void {
231+ this . trackEvent ( TelemetryEvents . WORKFLOW_IMPORTED , metadata )
232+ }
233+
234+ trackPageVisibilityChanged ( metadata : PageVisibilityMetadata ) : void {
235+ this . trackEvent ( TelemetryEvents . PAGE_VISIBILITY_CHANGED , metadata )
236+ }
237+
238+ trackTabCount ( metadata : TabCountMetadata ) : void {
239+ this . trackEvent ( TelemetryEvents . TAB_COUNT_TRACKING , metadata )
240+ }
241+
242+ trackNodeSearch ( metadata : NodeSearchMetadata ) : void {
243+ this . trackEvent ( TelemetryEvents . NODE_SEARCH , metadata )
244+ }
245+
246+ trackNodeSearchResultSelected ( metadata : NodeSearchResultMetadata ) : void {
247+ this . trackEvent ( TelemetryEvents . NODE_SEARCH_RESULT_SELECTED , metadata )
248+ }
249+
250+ trackTemplateFilterChanged ( metadata : TemplateFilterMetadata ) : void {
251+ this . trackEvent ( TelemetryEvents . TEMPLATE_FILTER_CHANGED , metadata )
252+ }
253+
181254 trackWorkflowExecution ( ) : void {
182255 const context = this . getExecutionContext ( )
183256 this . trackEvent ( TelemetryEvents . EXECUTION_START , context )
@@ -194,8 +267,28 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
194267 getExecutionContext ( ) : ExecutionContext {
195268 const workflowStore = useWorkflowStore ( )
196269 const templatesStore = useWorkflowTemplatesStore ( )
270+ const nodeDefStore = useNodeDefStore ( )
197271 const activeWorkflow = workflowStore . activeWorkflow
198272
273+ // Calculate node metrics in a single traversal
274+ const nodeMetrics = reduceAllNodes (
275+ app . graph ,
276+ ( acc , node ) => {
277+ const nodeDef = nodeDefStore . nodeDefsByName [ node . type ]
278+ const isCustomNode =
279+ nodeDef ?. nodeSource ?. type === NodeSourceType . CustomNodes
280+ const isApiNode = nodeDef ?. api_node === true
281+ const isSubgraph = node . isSubgraphNode ?.( ) === true
282+
283+ return {
284+ custom_node_count : acc . custom_node_count + ( isCustomNode ? 1 : 0 ) ,
285+ api_node_count : acc . api_node_count + ( isApiNode ? 1 : 0 ) ,
286+ subgraph_count : acc . subgraph_count + ( isSubgraph ? 1 : 0 )
287+ }
288+ } ,
289+ { custom_node_count : 0 , api_node_count : 0 , subgraph_count : 0 }
290+ )
291+
199292 if ( activeWorkflow ?. filename ) {
200293 const isTemplate = templatesStore . knownTemplateNames . has (
201294 activeWorkflow . filename
@@ -218,19 +311,22 @@ export class MixpanelTelemetryProvider implements TelemetryProvider {
218311 template_tags : englishMetadata ?. tags ?? template ?. tags ,
219312 template_models : englishMetadata ?. models ?? template ?. models ,
220313 template_use_case : englishMetadata ?. useCase ?? template ?. useCase ,
221- template_license : englishMetadata ?. license ?? template ?. license
314+ template_license : englishMetadata ?. license ?? template ?. license ,
315+ ...nodeMetrics
222316 }
223317 }
224318
225319 return {
226320 is_template : false ,
227- workflow_name : activeWorkflow . filename
321+ workflow_name : activeWorkflow . filename ,
322+ ...nodeMetrics
228323 }
229324 }
230325
231326 return {
232327 is_template : false ,
233- workflow_name : undefined
328+ workflow_name : undefined ,
329+ ...nodeMetrics
234330 }
235331 }
236332}
0 commit comments