Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experiments HogQL): UI update for funnels, sec metrics #25901

Merged
merged 73 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
fda9ae2
init
jurajmajerik Oct 23, 2024
9a90b22
merge
jurajmajerik Oct 25, 2024
992b8a0
wip
jurajmajerik Oct 25, 2024
7d7222f
wip
jurajmajerik Oct 25, 2024
3f2e415
wip
jurajmajerik Oct 28, 2024
8c59f91
init
jurajmajerik Oct 28, 2024
d6d5747
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 28, 2024
67b77a0
fix
jurajmajerik Oct 28, 2024
9de8788
fix
jurajmajerik Oct 28, 2024
d42a00e
Merge branch 'master' of https://github.com/PostHog/posthog into expe…
jurajmajerik Oct 28, 2024
d4a5d0c
wip
jurajmajerik Oct 28, 2024
22b0cc6
wip
jurajmajerik Oct 28, 2024
8259258
wip
jurajmajerik Oct 28, 2024
b12c6de
wip
jurajmajerik Oct 28, 2024
6354836
resolve
jurajmajerik Oct 28, 2024
026e740
Update query snapshots
github-actions[bot] Oct 28, 2024
317428a
wip
jurajmajerik Oct 28, 2024
0cc074f
Merge branch 'experiments-hogql-ui' of https://github.com/PostHog/pos…
jurajmajerik Oct 28, 2024
a913691
Update query snapshots
github-actions[bot] Oct 28, 2024
2a1c680
wip
jurajmajerik Oct 28, 2024
f1208fb
Update query snapshots
github-actions[bot] Oct 28, 2024
8ebdf2a
Merge branch 'master' into experiments-hogql-ui
jurajmajerik Oct 29, 2024
6fe46a8
fix
jurajmajerik Oct 29, 2024
f0b8c3a
Merge branch 'experiments-hogql-ui' of https://github.com/PostHog/pos…
jurajmajerik Oct 29, 2024
1e84e59
resolve
jurajmajerik Oct 29, 2024
62dc558
Merge branch 'master' of https://github.com/PostHog/posthog into expe…
jurajmajerik Oct 29, 2024
773d400
Update query snapshots
github-actions[bot] Oct 29, 2024
48a4428
Update query snapshots
github-actions[bot] Oct 29, 2024
bc08bfa
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
f7f4397
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
885ec28
Update query snapshots
github-actions[bot] Oct 29, 2024
9d5ed8a
Update query snapshots
github-actions[bot] Oct 29, 2024
91038c5
Update query snapshots
github-actions[bot] Oct 29, 2024
b080417
Update query snapshots
github-actions[bot] Oct 29, 2024
39b3d8b
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 29, 2024
0f43482
Update query snapshots
github-actions[bot] Oct 29, 2024
fe112bb
Update UI snapshots for `chromium` (2)
github-actions[bot] Oct 29, 2024
264b104
Update query snapshots
github-actions[bot] Oct 29, 2024
bf292ad
Update query snapshots
github-actions[bot] Oct 29, 2024
ac0b393
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
59ac985
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
70d121d
Update query snapshots
github-actions[bot] Oct 29, 2024
7691fc1
Update query snapshots
github-actions[bot] Oct 29, 2024
eb291a4
Update query snapshots
github-actions[bot] Oct 29, 2024
89a3095
Update query snapshots
github-actions[bot] Oct 29, 2024
a5b1d1b
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
6c75c91
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
395fb6e
Update query snapshots
github-actions[bot] Oct 29, 2024
08349f6
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
c6d74c2
Update query snapshots
github-actions[bot] Oct 29, 2024
0f00247
resolve
jurajmajerik Oct 29, 2024
3fbff3a
Update query snapshots
github-actions[bot] Oct 29, 2024
5f0a1d4
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 29, 2024
1b247d8
Update query snapshots
github-actions[bot] Oct 29, 2024
5c38867
Update query snapshots
github-actions[bot] Oct 29, 2024
c400641
Update query snapshots
github-actions[bot] Oct 29, 2024
5077d8f
resolve
jurajmajerik Oct 29, 2024
50690db
Update query snapshots
github-actions[bot] Oct 29, 2024
0d122bc
Update query snapshots
github-actions[bot] Oct 29, 2024
91d798d
Update query snapshots
github-actions[bot] Oct 30, 2024
df8187f
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 30, 2024
92b610d
Update query snapshots
github-actions[bot] Oct 30, 2024
85f981d
Update UI snapshots for `chromium` (1)
github-actions[bot] Oct 30, 2024
df50996
Update query snapshots
github-actions[bot] Oct 30, 2024
66e9766
Update query snapshots
github-actions[bot] Oct 30, 2024
8ef65f0
Update query snapshots
github-actions[bot] Oct 30, 2024
cc1e297
Update query snapshots
github-actions[bot] Oct 30, 2024
1c1903d
Update query snapshots
github-actions[bot] Oct 30, 2024
315eecd
fix types
jurajmajerik Oct 30, 2024
2ae367b
Merge branch 'experiments-hogql-ui' of https://github.com/PostHog/pos…
jurajmajerik Oct 30, 2024
340b8ce
init
jurajmajerik Oct 30, 2024
14b732f
resolve
jurajmajerik Oct 30, 2024
1a65ec9
wip
jurajmajerik Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5236,6 +5236,9 @@
"experiment_id": {
"type": "integer"
},
"funnels_query": {
"$ref": "#/definitions/FunnelsQuery"
},
"kind": {
"const": "ExperimentFunnelsQuery",
"type": "string"
Expand All @@ -5246,12 +5249,9 @@
},
"response": {
"$ref": "#/definitions/ExperimentFunnelsQueryResponse"
},
"source": {
"$ref": "#/definitions/FunnelsQuery"
}
},
"required": ["experiment_id", "kind", "source"],
"required": ["experiment_id", "funnels_query", "kind"],
"type": "object"
},
"ExperimentFunnelsQueryResponse": {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ export type CachedExperimentFunnelsQueryResponse = CachedQueryResponse<Experimen

export interface ExperimentFunnelsQuery extends DataNode<ExperimentFunnelsQueryResponse> {
kind: NodeKind.ExperimentFunnelsQuery
source: FunnelsQuery
funnels_query: FunnelsQuery
experiment_id: integer
}

Expand Down
65 changes: 58 additions & 7 deletions frontend/src/scenes/experiments/experimentLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -827,9 +827,42 @@ export const experimentLogic = kea<experimentLogicType>([
},
],
secondaryMetricResults: [
null as SecondaryMetricResults[] | null,
null as
| SecondaryMetricResults[]
| (CachedExperimentTrendsQueryResponse | CachedExperimentFunnelsQueryResponse)[]
| null,
{
loadSecondaryMetricResults: async (refresh?: boolean) => {
loadSecondaryMetricResults: async (
refresh?: boolean
): Promise<
| SecondaryMetricResults[]
| (CachedExperimentTrendsQueryResponse | CachedExperimentFunnelsQueryResponse)[]
| null
> => {
if (values.featureFlags[FEATURE_FLAGS.EXPERIMENTS_HOGQL]) {
const secondaryMetrics =
values.experiment?.metrics?.filter((metric) => metric.type === 'secondary') || []

return (await Promise.all(
secondaryMetrics.map(async (metric) => {
try {
const response: ExperimentResults = await api.create(
`api/projects/${values.currentTeamId}/query`,
{ query: metric.query }
)

return {
...response,
fakeInsightId: Math.random().toString(36).substring(2, 15),
last_refresh: response.last_refresh || '',
}
} catch (error) {
return {}
}
})
)) as unknown as (CachedExperimentTrendsQueryResponse | CachedExperimentFunnelsQueryResponse)[]
}

const refreshParam = refresh ? '&refresh=true' : ''

return await Promise.all(
Expand All @@ -846,6 +879,7 @@ export const experimentLogic = kea<experimentLogicType>([
last_refresh: secResults.last_refresh,
}
}

return {
...secResults.result,
fakeInsightId: Math.random().toString(36).substring(2, 15),
Expand Down Expand Up @@ -1255,9 +1289,10 @@ export const experimentLogic = kea<experimentLogicType>([
| CachedExperimentTrendsQueryResponse
| CachedExperimentFunnelsQueryResponse
| null,
variant: string
variant: string,
type: 'primary' | 'secondary' = 'primary'
): number | null => {
const usingMathAggregationType = experimentMathAggregationForTrends()
const usingMathAggregationType = type === 'primary' ? experimentMathAggregationForTrends() : false
if (!experimentResults || !experimentResults.insight) {
return null
}
Expand Down Expand Up @@ -1392,15 +1427,31 @@ export const experimentLogic = kea<experimentLogicType>([
},
],
tabularSecondaryMetricResults: [
(s) => [s.experiment, s.secondaryMetricResults],
(experiment, secondaryMetricResults): TabularSecondaryMetricResults[] => {
(s) => [s.experiment, s.secondaryMetricResults, s.conversionRateForVariant, s.countDataForVariant],
(
experiment,
secondaryMetricResults,
conversionRateForVariant,
countDataForVariant
): TabularSecondaryMetricResults[] => {
if (!secondaryMetricResults) {
return []
}

const variantsWithResults: TabularSecondaryMetricResults[] = []
experiment?.parameters?.feature_flag_variants?.forEach((variant) => {
const metricResults: SecondaryMetricResult[] = []
experiment?.secondary_metrics?.forEach((metric, idx) => {
let result
if (metric.filters.insight === InsightType.FUNNELS) {
result = conversionRateForVariant(secondaryMetricResults?.[idx], variant.key)
} else {
result = countDataForVariant(secondaryMetricResults?.[idx], variant.key, 'secondary')
}

metricResults.push({
insightType: metric.filters.insight || InsightType.TRENDS,
result: secondaryMetricResults?.[idx]?.result?.[variant.key],
result: result || undefined,
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ExperimentFunnelsQueryResponse,
ExperimentSignificanceCode,
ExperimentVariantFunnelsBaseStats,
FunnelsFilter,
FunnelsQuery,
FunnelsQueryResponse,
InsightDateRange,
Expand Down Expand Up @@ -46,6 +47,11 @@ def calculate(self) -> ExperimentFunnelsQueryResponse:

self._validate_event_variants(funnels_result)

# Filter results to only include valid variants in the first step
funnels_result.results = [
result for result in funnels_result.results if result[0]["breakdown_value"][0] in self.variants
]

# Statistical analysis
control_variant, test_variants = self._get_variants_with_base_stats(funnels_result)
probabilities = calculate_probabilities(control_variant, test_variants)
Expand Down Expand Up @@ -76,8 +82,8 @@ def _prepare_funnel_query(self) -> FunnelsQuery:
2. Configure the breakdown to use the feature flag key, which allows us
to separate results for different experiment variants.
"""
# Clone the source query
prepared_funnels_query = FunnelsQuery(**self.query.source.model_dump())
# Clone the funnels query
prepared_funnels_query = FunnelsQuery(**self.query.funnels_query.model_dump())

# Set the date range to match the experiment's duration, using the project's timezone
if self.team.timezone:
Expand All @@ -100,6 +106,10 @@ def _prepare_funnel_query(self) -> FunnelsQuery:
breakdown_type="event",
)

prepared_funnels_query.funnelsFilter = FunnelsFilter(
funnelVizType="steps",
)

return prepared_funnels_query

def _get_variants_with_base_stats(
Expand Down Expand Up @@ -180,4 +190,4 @@ def _validate_event_variants(self, funnels_result: FunnelsQueryResponse):
raise ValidationError(detail=json.dumps(errors))

def to_query(self) -> ast.SelectQuery:
raise ValueError(f"Cannot convert source query of type {self.query.source.kind} to query")
raise ValueError(f"Cannot convert source query of type {self.query.funnels_query.kind} to query")
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_query_runner(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

experiment.metrics = [{"type": "primary", "query": experiment_query.model_dump()}]
Expand Down Expand Up @@ -130,7 +130,7 @@ def test_query_runner_standard_flow(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

experiment.metrics = [{"type": "primary", "query": experiment_query.model_dump()}]
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_validate_event_variants_no_events(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

query_runner = ExperimentFunnelsQueryRunner(query=experiment_query, team=self.team)
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_validate_event_variants_no_control(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

query_runner = ExperimentFunnelsQueryRunner(query=experiment_query, team=self.team)
Expand Down Expand Up @@ -297,7 +297,7 @@ def test_validate_event_variants_no_test(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

query_runner = ExperimentFunnelsQueryRunner(query=experiment_query, team=self.team)
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_validate_event_variants_no_flag_info(self):
experiment_query = ExperimentFunnelsQuery(
experiment_id=experiment.id,
kind="ExperimentFunnelsQuery",
source=funnels_query,
funnels_query=funnels_query,
)

query_runner = ExperimentFunnelsQueryRunner(query=experiment_query, team=self.team)
Expand Down
2 changes: 1 addition & 1 deletion posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6183,12 +6183,12 @@ class ExperimentFunnelsQuery(BaseModel):
extra="forbid",
)
experiment_id: int
funnels_query: FunnelsQuery
kind: Literal["ExperimentFunnelsQuery"] = "ExperimentFunnelsQuery"
modifiers: Optional[HogQLQueryModifiers] = Field(
default=None, description="Modifiers used when performing the query"
)
response: Optional[ExperimentFunnelsQueryResponse] = None
source: FunnelsQuery


class FunnelCorrelationQuery(BaseModel):
Expand Down
Loading