Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(feature-flags): match $feature_interaction in enriched analytics query (#81660)

Asheesh Kumarc870b35b56a8
diff --git a/posthog/test/test_feature_flag_analytics.py b/posthog/test/test_feature_flag_analytics.py
index a41949749a28..0a93462b94d4 100644
--- a/posthog/test/test_feature_flag_analytics.py
+++ b/posthog/test/test_feature_flag_analytics.py
@@ -934,6 +934,12 @@ class TestEnrichedAnalytics(BaseTest):
key="beta-feature3",
created_by=self.user,
)
+ f5 = FeatureFlag.objects.create(
+ team=self.team,
+ name="Beta feature",
+ key="beta-feature4",
+ created_by=self.user,
+ )
# create usage dashboard for f1 and f3
_create_usage_dashboard(f1, self.user)
@@ -962,6 +968,14 @@ class TestEnrichedAnalytics(BaseTest):
properties={"feature_flag": "test_flag"},
timestamp="2021-01-12T12:00:10Z",
)
+ # out of bounds for f5 - should not set has_enriched_analytics
+ _create_event(
+ team=self.team,
+ distinct_id="test8",
+ event="$feature_view",
+ properties={"feature_flag": "beta-feature4"},
+ timestamp="2021-01-12T12:00:10Z",
+ )
# different flag
_create_event(
team=self.team,
@@ -1006,11 +1020,13 @@ class TestEnrichedAnalytics(BaseTest):
f2.refresh_from_db()
f3.refresh_from_db()
f4.refresh_from_db()
+ f5.refresh_from_db()
self.assertEqual(f1.has_enriched_analytics, True)
self.assertEqual(f2.has_enriched_analytics, True)
self.assertEqual(f3.has_enriched_analytics, False)
self.assertEqual(f4.has_enriched_analytics, False)
+ self.assertEqual(f5.has_enriched_analytics, False)
# now try deleting a usage dashboard. It should not delete the feature flag
assert f1.usage_dashboard is not None
@@ -1036,6 +1052,34 @@ class TestEnrichedAnalytics(BaseTest):
self.assertEqual(f1.has_enriched_analytics, True)
self.assertEqual(f1.usage_dashboard, None)
+ def test_find_flags_with_enriched_analytics_via_feature_interaction_only(self):
+ # A flag that only ever receives $feature_interaction (no $feature_view) should still be
+ # detected as enriched, since the generated usage dashboard charts both events.
+ flag = FeatureFlag.objects.create(
+ team=self.team,
+ name="Interaction only feature",
+ key="interaction-only-flag",
+ created_by=self.user,
+ )
+
+ _create_event(
+ team=self.team,
+ distinct_id="test",
+ event="$feature_interaction",
+ properties={"feature_flag": "interaction-only-flag"},
+ timestamp="2021-01-01T12:00:00Z",
+ )
+
+ flush_persons_and_events()
+
+ start = datetime.datetime(2021, 1, 1, 0, 0, 0)
+ end = datetime.datetime(2021, 1, 2, 0, 0, 0)
+
+ find_flags_with_enriched_analytics(start, end)
+
+ flag.refresh_from_db()
+ self.assertEqual(flag.has_enriched_analytics, True)
+
class TestFindFlagsWithEnrichedAnalyticsTask(BaseTest):
@patch("products.feature_flags.backend.flag_analytics.find_flags_with_enriched_analytics")
diff --git a/products/feature_flags/backend/flag_analytics.py b/products/feature_flags/backend/flag_analytics.py
index adb652865829..0efe895c22ee 100644
--- a/products/feature_flags/backend/flag_analytics.py
+++ b/products/feature_flags/backend/flag_analytics.py
@@ -244,7 +244,8 @@ def _build_enriched_analytics_query() -> str:
return f"""
SELECT team_id, {_enriched_flag_key_expr_sql()} as flag_key
FROM events
- WHERE timestamp between %(begin)s AND %(end)s AND event = '$feature_view'
+ PREWHERE event IN ('$feature_view', '$feature_interaction')
+ AND timestamp between %(begin)s AND %(end)s
GROUP BY team_id, flag_key
"""