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(phai): attribute Max web insight queries to posthog_ai source (#69315)

Georgiy Tarasov7b60d41e6942
diff --git a/posthog/api/query.py b/posthog/api/query.py
index 01a7f25e332f..227c4cb69226 100644
--- a/posthog/api/query.py
+++ b/posthog/api/query.py
@@ -50,7 +50,7 @@ from posthog.clickhouse.client.limit import ConcurrencyLimitExceeded
from posthog.clickhouse.query_tagging import get_query_tag_value, get_query_tags, tag_queries
from posthog.constants import AvailableFeature
from posthog.errors import ExposedCHQueryError, InternalCHQueryError
-from posthog.event_usage import get_request_analytics_properties, report_user_or_team_action
+from posthog.event_usage import EventSource, get_request_analytics_properties, report_user_or_team_action
from posthog.exceptions_capture import capture_exception
from posthog.hogql_queries.apply_dashboard_filters import apply_dashboard_filters, apply_dashboard_variables
from posthog.hogql_queries.hogql_query_runner import HogQLQueryRunner
@@ -199,6 +199,10 @@ class QueryViewSet(QueryCoalescingMixin, TeamAndOrgViewSetMixin, PydanticModelMi
if data.limit_context == SchemaLimitContext.POSTHOG_AI:
limit_context: LimitContext | None = LimitContext.POSTHOG_AI
+ # Max's insight tiles run in the browser, so the request looks like a session
+ # web request and get_event_source classifies it as "web". Attribute it to
+ # posthog_ai instead, matching the server-side executor's tagging.
+ analytics_props["source"] = EventSource.POSTHOG_AI
elif (
is_async_query(query_dict)
or is_insight_actors_query(query_dict)
diff --git a/posthog/api/test/test_query.py b/posthog/api/test/test_query.py
index 62092bfa060a..f43f8b0ff500 100644
--- a/posthog/api/test/test_query.py
+++ b/posthog/api/test/test_query.py
@@ -38,6 +38,7 @@ from posthog.api.services.query import process_query_dict, process_query_model
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.client.limit import ConcurrencyLimitExceeded
from posthog.clickhouse.query_tagging import Product, QueryTags
+from posthog.event_usage import EventSource
from posthog.models.utils import UUIDT
from products.event_definitions.backend.models.property_definition import PropertyDefinition, PropertyType
@@ -736,6 +737,9 @@ class TestQuery(ClickhouseTestMixin, APIBaseTest):
)
mock_process_query_model.assert_called_once()
self.assertEqual(mock_process_query_model.call_args[1]["limit_context"], LimitContext.POSTHOG_AI)
+ # The posthog_ai limit context also retags the analytics source, so Max's insight tiles
+ # (browser session requests that would otherwise read as "web") are attributed to posthog_ai.
+ self.assertEqual(mock_process_query_model.call_args[1]["analytics_props"]["source"], EventSource.POSTHOG_AI)
@patch("posthog.api.query.process_query_model")
def test_query_limit_context_default(self, mock_process_query_model):