|
| 1 | +import { FunnelsQuery, InsightVizNode } from '~/queries/schema/schema-general' |
| 2 | +import { DashboardTile, QueryBasedInsightModel } from '~/types' |
| 3 | + |
| 4 | +export interface FunnelContext { |
| 5 | + insightName: string |
| 6 | + conversionRate: number |
| 7 | + steps: string[] |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Given a list of dashboard tiles, find the funnel with the best |
| 12 | + * opportunity to create a survey. See {@link getBestSurveyOpportunityFromDashboard} |
| 13 | + * for logic details |
| 14 | + * |
| 15 | + * @param tiles list of dashboard tiles |
| 16 | + * @param linkedInsightIds list of surveys with linked insights |
| 17 | + * @returns |
| 18 | + */ |
| 19 | +export function getBestSurveyOpportunityFunnel( |
| 20 | + tiles: DashboardTile<QueryBasedInsightModel>[], |
| 21 | + linkedInsightIds: Set<number> = new Set() |
| 22 | +): DashboardTile<QueryBasedInsightModel> | null { |
| 23 | + return getBestSurveyOpportunityFromDashboard( |
| 24 | + tiles.filter((tile) => isFunnelInsight(tile.insight?.query)), |
| 25 | + linkedInsightIds |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Get some useful "context" data from a funnel insight |
| 31 | + * |
| 32 | + * @param insight funnel insight |
| 33 | + * @returns funnel "context" object with name, conversion rate (0-1), list of step names |
| 34 | + */ |
| 35 | +export function extractFunnelContext(insight: Partial<QueryBasedInsightModel>): FunnelContext | null { |
| 36 | + if (!isFunnelInsight(insight.query)) { |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + const result = insight.result |
| 41 | + if (!result || !Array.isArray(result) || result.length === 0) { |
| 42 | + return null |
| 43 | + } |
| 44 | + |
| 45 | + const conversionRate = conversionRateFromInsight(result) |
| 46 | + const steps = result.map((step: any) => step.name || 'Unknown step') |
| 47 | + |
| 48 | + return { |
| 49 | + insightName: insight.name || 'Funnel', |
| 50 | + conversionRate, |
| 51 | + steps, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Given a list of dashboard tiles (insights), find the best opportunity to |
| 57 | + * create a survey. |
| 58 | + * |
| 59 | + * Logic: |
| 60 | + * - Filter insights with existing linked surveys |
| 61 | + * - Calculate overall conversion rate |
| 62 | + * - Filter conversion rate < 50% |
| 63 | + * - Return lowest conversion rate funnel |
| 64 | + * |
| 65 | + * @param tiles list of dashboard tiles |
| 66 | + * @param linkedInsightIds list of surveys with linked insights |
| 67 | + * @returns |
| 68 | + */ |
| 69 | +function getBestSurveyOpportunityFromDashboard( |
| 70 | + tiles: DashboardTile<QueryBasedInsightModel>[], |
| 71 | + linkedInsightIds: Set<number> = new Set() |
| 72 | +): DashboardTile<QueryBasedInsightModel> | null { |
| 73 | + const funnelTiles = tiles |
| 74 | + .filter((tile) => tile.insight?.id && !linkedInsightIds.has(tile.insight.id)) |
| 75 | + .map((tile) => { |
| 76 | + const result = tile.insight?.result |
| 77 | + if (!result || !Array.isArray(result) || result.length === 0) { |
| 78 | + return { tile, conversionRate: 1 } |
| 79 | + } |
| 80 | + |
| 81 | + const conversionRate = conversionRateFromInsight(result) |
| 82 | + return { tile, conversionRate } |
| 83 | + }) |
| 84 | + .filter(({ conversionRate }) => conversionRate < 0.5) |
| 85 | + .sort((a, b) => a.conversionRate - b.conversionRate) |
| 86 | + |
| 87 | + return funnelTiles[0]?.tile || null |
| 88 | +} |
| 89 | + |
| 90 | +function conversionRateFromInsight(result: QueryBasedInsightModel['result']): number { |
| 91 | + const first = result[0]?.count || 1 |
| 92 | + const last = result[result.length - 1]?.count || 0 |
| 93 | + return last / first |
| 94 | +} |
| 95 | + |
| 96 | +function isFunnelInsight(query: any): query is InsightVizNode<FunnelsQuery> { |
| 97 | + return query?.kind === 'InsightVizNode' && query?.source?.kind === 'FunnelsQuery' |
| 98 | +} |
0 commit comments