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.

feat(subscriptions): attribute insight query type on subscription analytics events (#70511)

Mike Warren9c7cb3976a73
diff --git a/posthog/models/test/test_subscription_model.py b/posthog/models/test/test_subscription_model.py
index 5371cf03f682..9f2d14a89075 100644
--- a/posthog/models/test/test_subscription_model.py
+++ b/posthog/models/test/test_subscription_model.py
@@ -101,6 +101,25 @@ class TestSubscription(BaseTest):
with self.assertRaises(ValueError):
_ = subscription.resource_type
+ def test_analytics_metadata_includes_insight_query_kind(self):
+ insight = Insight.objects.create(
+ team=self.team,
+ query={"kind": "InsightVizNode", "source": {"kind": "TrendsQuery", "series": []}},
+ )
+ metadata = self._create_subscription(insight=insight).get_analytics_metadata()
+
+ assert metadata["query_kind"] == "InsightVizNode"
+ assert metadata["query_source_kind"] == "TrendsQuery"
+
+ def test_analytics_metadata_omits_query_kind_for_non_insight_subscription(self):
+ # Query-kind attribution is insight-only; dashboard/prompt subs must not carry stale keys.
+ metadata = self._create_subscription(
+ dashboard=Dashboard.objects.create(team=self.team)
+ ).get_analytics_metadata()
+
+ assert "query_kind" not in metadata
+ assert "query_source_kind" not in metadata
+
@parameterized.expand(
[
("insight", 1, None, None, Subscription.ResourceType.INSIGHT),
diff --git a/products/exports/backend/models/subscription.py b/products/exports/backend/models/subscription.py
index 99bc713b94e8..10bd46439061 100644
--- a/products/exports/backend/models/subscription.py
+++ b/products/exports/backend/models/subscription.py
@@ -467,7 +467,7 @@ class Subscription(ModelActivityMixin, models.Model):
"""
Returns serialized information about the object for analytics reporting.
"""
- return {
+ metadata: dict[str, Any] = {
"id": self.id,
"resource_type": self.resource_type,
"target_type": self.target_type,
@@ -479,6 +479,12 @@ class Subscription(ModelActivityMixin, models.Model):
"prompt_length": len(self.prompt or ""),
"ai_window_mode": self.ai_window_mode if self.resource_type == self.ResourceType.AI_PROMPT else None,
}
+ # For insight subscriptions, attribute the subscribed insight's query type (e.g. TrendsQuery,
+ # FunnelsQuery) using the same keys as the "insight created/updated" events, so subscriptions
+ # can be sliced by which kinds of insights people subscribe to.
+ if self.resource_type == self.ResourceType.INSIGHT and self.insight:
+ metadata.update(self.insight.get_analytics_query_kinds())
+ return metadata
@receiver(post_save, sender=Subscription, dispatch_uid="hook-subscription-saved")