From 2319926397fb5070ec62e10fe119fd0950474f8d Mon Sep 17 00:00:00 2001 From: Neil Kakkar Date: Thu, 28 Mar 2024 15:42:18 +0000 Subject: [PATCH] fix(experiments): Set aggregation group type index correctly, use funnel as default --- .../scenes/experiments/experimentLogic.tsx | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsx index d2154c637a2a6..cf1a0fb6b3e93 100644 --- a/frontend/src/scenes/experiments/experimentLogic.tsx +++ b/frontend/src/scenes/experiments/experimentLogic.tsx @@ -24,6 +24,7 @@ import { groupsModel } from '~/models/groupsModel' import { filtersToQueryNode } from '~/queries/nodes/InsightQuery/utils/filtersToQueryNode' import { queryNodeToFilter } from '~/queries/nodes/InsightQuery/utils/queryNodeToFilter' import { FunnelsQuery, InsightVizNode, TrendsQuery } from '~/queries/schema' +import { isFunnelsQuery } from '~/queries/utils' import { ActionFilter as ActionFilterType, Breadcrumb, @@ -352,17 +353,7 @@ export const experimentLogic = kea([ setNewExperimentInsight: async ({ filters }) => { let newInsightFilters const aggregationGroupTypeIndex = values.experiment.parameters?.aggregation_group_type_index - if (filters?.insight === InsightType.FUNNELS) { - newInsightFilters = cleanFilters({ - insight: InsightType.FUNNELS, - funnel_viz_type: FunnelVizType.Steps, - date_from: dayjs().subtract(EXPERIMENT_DEFAULT_DURATION, 'day').format('YYYY-MM-DDTHH:mm'), - date_to: dayjs().endOf('d').format('YYYY-MM-DDTHH:mm'), - layout: FunnelLayout.horizontal, - aggregation_group_type_index: aggregationGroupTypeIndex, - ...filters, - }) - } else { + if (filters?.insight === InsightType.TRENDS) { const groupAggregation = aggregationGroupTypeIndex !== undefined ? { math: 'unique_group', math_group_type_index: aggregationGroupTypeIndex } @@ -378,6 +369,16 @@ export const experimentLogic = kea([ ...eventAddition, ...filters, }) + } else { + newInsightFilters = cleanFilters({ + insight: InsightType.FUNNELS, + funnel_viz_type: FunnelVizType.Steps, + date_from: dayjs().subtract(EXPERIMENT_DEFAULT_DURATION, 'day').format('YYYY-MM-DDTHH:mm'), + date_to: dayjs().endOf('d').format('YYYY-MM-DDTHH:mm'), + layout: FunnelLayout.horizontal, + aggregation_group_type_index: aggregationGroupTypeIndex, + ...filters, + }) } // This allows switching between insight types. It's necessary as `updateQuerySource` merges @@ -389,6 +390,13 @@ export const experimentLogic = kea([ } else { ;(newQuery as FunnelsQuery).funnelsFilter = undefined } + + // TRICKY: We always know what the group type index should be for funnel queries, so we don't care + // what the previous value was. Hence, instead of a partial update with `updateQuerySource`, we always + // explicitly set it to what it should be + if (isFunnelsQuery(newQuery)) { + newQuery.aggregation_group_type_index = aggregationGroupTypeIndex + } actions.updateQuerySource(newQuery) }, // sync form value `filters` with query @@ -670,7 +678,7 @@ export const experimentLogic = kea([ experimentInsightType: [ (s) => [s.experiment], (experiment): InsightType => { - return experiment?.filters?.insight || InsightType.TRENDS + return experiment?.filters?.insight || InsightType.FUNNELS }, ], isExperimentRunning: [