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(analytics): add CLI source attribution for posthog-cli requests (#65205)

Mike Warrenae1978bc4125
diff --git a/posthog/event_usage.py b/posthog/event_usage.py
index a64c69de580f..dc0f3903215a 100644
--- a/posthog/event_usage.py
+++ b/posthog/event_usage.py
@@ -272,6 +272,7 @@ def report_user_organization_membership_level_changed(
class EventSource(StrEnum):
WEB = "web"
API = "api"
+ CLI = "cli"
POSTHOG_AI = "posthog_ai"
POSTHOG_CODE = "posthog_code"
TERRAFORM = "terraform"
@@ -319,14 +320,17 @@ def get_event_source(request) -> EventSource:
user_agent = request.headers.get("user-agent", "") or ""
if not isinstance(user_agent, str):
user_agent = ""
- # Wizard, posthog-code etc. all wrap MCP — their UA tokens must win over the
- # X-PostHog-Client header so the source reflects the outer caller.
+ # Wizard, posthog-code, posthog-cli etc. all wrap MCP — their UA tokens
+ # must win over the X-PostHog-Client header so the source reflects the
+ # outer caller.
if "posthog/terraform-provider" in user_agent:
return EventSource.TERRAFORM
if "posthog/wizard" in user_agent:
return EventSource.WIZARD
if _POSTHOG_CODE_UA_RE.search(user_agent):
return EventSource.POSTHOG_CODE
+ if user_agent == "posthog-cli" or user_agent.startswith("posthog-cli/"):
+ return EventSource.CLI
if "posthog/mcp-server" in user_agent or request.headers.get("X-Posthog-Client") == "mcp":
return EventSource.MCP
# DRF sets successful_authenticator during view dispatch; before that
diff --git a/posthog/test/test_event_usage.py b/posthog/test/test_event_usage.py
index 109f51843138..3193aaf2f7ad 100644
--- a/posthog/test/test_event_usage.py
+++ b/posthog/test/test_event_usage.py
@@ -256,6 +256,9 @@ class TestGetEventSource(BaseTest):
@parameterized.expand(
[
("terraform", "posthog/terraform-provider 1.0", EventSource.TERRAFORM),
+ ("cli_exact", "posthog-cli", EventSource.CLI),
+ ("cli_with_version", "posthog-cli/0.7.30", EventSource.CLI),
+ ("cli_via_mcp_is_cli", "posthog-cli/posthog-cli posthog/mcp-server; version: 1.0.0", EventSource.CLI),
("wizard", "posthog/wizard 1.0", EventSource.WIZARD),
("posthog_code", "posthog/code 1.2.3", EventSource.POSTHOG_CODE),
("hog_dev_subdomain", "posthog/desktop.hog.dev 0.1.0", EventSource.POSTHOG_CODE),