: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(api): flag orgs created by posthog.com users as excluded from crm (#82423)
Abhischekd5e06e4763e3
diff --git a/posthog/api/organization.py b/posthog/api/organization.pyindex 6b1ea1f9a6d6..108a231187d0 100644--- a/posthog/api/organization.py+++ b/posthog/api/organization.py@@ -26,6 +26,7 @@ from posthog.cloud_utils import get_cached_instance_license, is_cloudfrom posthog.constants import INTERNAL_BOT_EMAIL_SUFFIX, AvailableFeaturefrom posthog.data_freshness import LOOKBACK_DAYS, QUIET_AFTER_DAYS, Freshness, get_organization_data_freshnessfrom posthog.event_usage import (+ exclude_internal_organization_from_crm,groups,report_organization_action,report_organization_deleted,@@ -242,6 +243,7 @@ class OrganizationSerializer(serializers.raise_errors_on_nested_writes("create", self, validated_data)user = self.context["request"].userorganization, _, _ = Organization.objects.bootstrap(user, **validated_data)+ exclude_internal_organization_from_crm(organization, user)return organization@tracer.start_as_current_span("organization_serializer.membership_level")diff --git a/posthog/api/test/test_organization.py b/posthog/api/test/test_organization.pyindex 1d5d0f8f5405..96a55e1e0ac4 100644--- a/posthog/api/test/test_organization.py+++ b/posthog/api/test/test_organization.py@@ -83,6 +83,32 @@ class TestOrganizationAPI(APIBaseTest):self.assertEqual(Organization.objects.count(), 2)self.assertEqual(response.json()["plugins_access_level"], 3)+ @parameterized.expand(+ [+ ("posthog_staff", "[email protected]", True),+ ("customer", "[email protected]", False),+ ]+ )+ @patch("posthog.event_usage.posthoganalytics.group_identify")+ def test_organizations_created_by_posthog_staff_are_excluded_from_crm(+ self, _name, email, expect_flagged, mock_group_identify+ ):+ user = self._create_user(email)+ self.client.force_login(user)++ with self.is_cloud(True):+ response = self.client.post("/api/organizations/", {"name": "New org"})++ self.assertEqual(response.status_code, status.HTTP_201_CREATED)+ if expect_flagged:+ mock_group_identify.assert_called_once_with(+ "organization",+ response.json()["id"],+ properties={"exclude_from_crm": True},+ )+ else:+ mock_group_identify.assert_not_called()+# Updating organizationsdef test_update_organization_if_admin(self):diff --git a/posthog/constants.py b/posthog/constants.pyindex 25a743c4c4a6..b8e8404fb1bb 100644--- a/posthog/constants.py+++ b/posthog/constants.py@@ -5,6 +5,7 @@ from semantic_version import VersionFROZEN_POSTHOG_VERSION = Version("1.43.0") # Frozen at the last self-hosted version, just for backwards compat nowINTERNAL_BOT_EMAIL_SUFFIX = "@posthogbot.user"+POSTHOG_INTERNAL_EMAIL_SUFFIX = "@posthog.com"# N.B. Keep this in sync with frontend enum (types.ts)diff --git a/posthog/event_usage.py b/posthog/event_usage.pyindex 2101636318fa..3b78b39beeae 100644--- a/posthog/event_usage.py+++ b/posthog/event_usage.py@@ -14,6 +14,7 @@ from opentelemetry import tracefrom rest_framework.authentication import SessionAuthenticationfrom posthog.clickhouse.query_tagging import get_query_tag_value+from posthog.constants import POSTHOG_INTERNAL_EMAIL_SUFFIXfrom posthog.models import Organization, Userfrom posthog.models.activity_logging.model_activity import is_impersonated_sessionfrom posthog.models.team import Team@@ -72,6 +73,9 @@ def report_user_signed_up(groups=groups(user.organization, user.team),)+ if is_organization_first_user and user.organization is not None:+ exclude_internal_organization_from_crm(user.organization, user)+def report_user_verified_email(current_user: User) -> None:"""@@ -610,3 +614,18 @@ def report_organization_action(if group_properties:posthoganalytics.group_identify("organization", str(organization.id), properties=group_properties)+++def exclude_internal_organization_from_crm(organization: Organization, creator: Optional[User]) -> None:+ """+ Organizations created by PostHog staff are internal demo or test workspaces, which must never+ be synced to customer-facing CRM tooling. The `exclude_from_crm` organization group property+ gates the CDP destinations that push organizations and their members to those tools.+ """+ if creator is None or not creator.email or not creator.email.endswith(POSTHOG_INTERNAL_EMAIL_SUFFIX):+ return+ posthoganalytics.group_identify(+ "organization",+ str(organization.id),+ properties={"exclude_from_crm": True},+ )diff --git a/posthog/test/test_event_usage.py b/posthog/test/test_event_usage.pyindex 041f7c500928..baec5c3eb8b8 100644--- a/posthog/test/test_event_usage.py+++ b/posthog/test/test_event_usage.py@@ -14,6 +14,7 @@ from posthog.event_usage import (get_mcp_properties,is_wizard_self_driving_program,report_user_action,+ report_user_signed_up,sanitize_header_value,)@@ -426,6 +427,33 @@ class TestGetMcpProperties(BaseTest):}+class TestSignupCrmExclusion(BaseTest):+ @parameterized.expand(+ [+ ("staff_creating_new_organization", "[email protected]", True, True),+ ("staff_joining_existing_organization", "[email protected]", False, False),+ ("customer_creating_new_organization", "[email protected]", True, False),+ ]+ )+ @patch("posthog.event_usage.posthoganalytics.group_identify")+ @patch("posthog.event_usage.posthoganalytics.capture")+ def test_new_organizations_of_posthog_staff_are_excluded_from_crm(+ self, _name, email, is_organization_first_user, expect_flagged, _mock_capture, mock_group_identify+ ):+ user = self._create_user(email)++ report_user_signed_up(user, is_instance_first_user=False, is_organization_first_user=is_organization_first_user)++ if expect_flagged:+ mock_group_identify.assert_called_once_with(+ "organization",+ str(self.organization.id),+ properties={"exclude_from_crm": True},+ )+ else:+ mock_group_identify.assert_not_called()++class TestSanitizeHeaderValue(BaseTest):@parameterized.expand([