: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(growth): at-signup enrichment snapshot event and icp_score_version stamping (#69411)
Matt Brookerc1b97d565d4b
diff --git a/.github/scripts/check-idor-model-coverage.py b/.github/scripts/check-idor-model-coverage.pyindex 9ceb39d6c4c0..30eda3c58fa2 100644--- a/.github/scripts/check-idor-model-coverage.py+++ b/.github/scripts/check-idor-model-coverage.py@@ -161,6 +161,9 @@ def get_scoped_models() -> tuple[dict[str, set[str]], set[str], set[str], set[st# OneToOne extension of Organization, read via the org relation# (enrichment_record), never looked up by user-supplied ID."OrganizationEnrichment",+ # Write-once idempotency guard keyed on the org, claimed via get_or_create from an+ # internal enrichment write-back path, never looked up by user-supplied ID.+ "EnrichmentSignupSnapshot",# Model kept to avoid a deletion migration but has no API endpoint"ErrorTrackingAutoCaptureControls","DuckLakeBackfill",diff --git a/products/growth/backend/enrichment/__init__.py b/products/growth/backend/enrichment/__init__.pynew file mode 100644index 000000000000..e69de29bb2d1diff --git a/products/growth/backend/enrichment/snapshot.py b/products/growth/backend/enrichment/snapshot.pynew file mode 100644index 000000000000..e1826d15078e--- /dev/null+++ b/products/growth/backend/enrichment/snapshot.py@@ -0,0 +1,74 @@+"""At-signup enrichment snapshot.++Captures the firmographic enrichment values as they were at signup, separate from the+live values that later enrichment runs continuously refresh onto the organization group.+The snapshot is emitted once as a person-scoped event and never updated.+"""++import dataclasses+from typing import Any, Optional++from posthoganalytics.client import Client++from products.growth.backend.models import EnrichmentSignupSnapshot++SNAPSHOT_EVENT_NAME = "enrichment_snapshot_at_signup"++SNAPSHOT_PROPERTY_SUFFIX = "_at_signup"+++class SignupEnrichmentSnapshot:+ """PII-light firmographic values captured at signup.++ Only derived firmographic and ICP fields belong here. No raw personal data beyond+ the person association that the emitted event inherently carries.+ """++ company_type: Optional[str] = None+ headcount: Optional[int] = None+ headcount_engineering: Optional[int] = None+ industry: Optional[str] = None+ country: Optional[str] = None+ founded_year: Optional[int] = None+ funding_stage: Optional[str] = None+ is_yc_company: Optional[bool] = None+ icp_score: Optional[int] = None+ icp_score_version: Optional[str] = None++ def to_event_properties(self) -> dict[str, Any]:+ """Return the snapshot as `*_at_signup` event properties, dropping unset values."""+ return {+ f"{field.name}{SNAPSHOT_PROPERTY_SUFFIX}": value+ for field in dataclasses.fields(self)+ if (value := getattr(self, field.name)) is not None+ }+++def capture_signup_enrichment_snapshot(+ pha_client: Client,+ *,+ organization_id: str,+ distinct_id: str,+ snapshot: SignupEnrichmentSnapshot,+) -> bool:+ """Emit the at-signup enrichment snapshot for an org, exactly once.++ Returns True if this call emitted the snapshot, False if one already existed.+ """+ # Claim the write-once slot atomically before emitting: the unique constraint on+ # organization_id lets concurrent or repeated runs make at most one row per org.+ _, created = EnrichmentSignupSnapshot.objects.get_or_create(organization_id=organization_id)+ if not created:+ return False++ # Emit as a person-scoped event (not org group properties): DeletionType.Person erases+ # all of a person's events, while group properties sit outside person-deletion scope,+ # so an erase-by-person request would not remove snapshot values stored on the group.+ pha_client.capture(+ distinct_id=distinct_id,+ event=SNAPSHOT_EVENT_NAME,+ properties=snapshot.to_event_properties(),+ groups={"organization": str(organization_id)},+ )+ return Truediff --git a/products/growth/backend/migrations/0003_enrichmentsignupsnapshot.py b/products/growth/backend/migrations/0003_enrichmentsignupsnapshot.pynew file mode 100644index 000000000000..82907159e730--- /dev/null+++ b/products/growth/backend/migrations/0003_enrichmentsignupsnapshot.py@@ -0,0 +1,40 @@+# Generated by Django 5.2.14 on 2026-07-08 19:29++import django.db.models.deletion+from django.db import migrations, models++import posthog.models.utils+++class Migration(migrations.Migration):+ dependencies = [+ ("growth", "0002_organizationenrichment"),+ ("posthog", "1247_oauthaccesstoken_token_idx"),+ ]++ operations = [+ migrations.CreateModel(+ name="EnrichmentSignupSnapshot",+ fields=[+ (+ "id",+ models.UUIDField(+ default=posthog.models.utils.uuid7, editable=False, primary_key=True, serialize=False+ ),+ ),+ ("created_at", models.DateTimeField(auto_now_add=True)),+ (+ "organization",+ models.OneToOneField(+ db_constraint=False,+ on_delete=django.db.models.deletion.CASCADE,+ related_name="enrichment_signup_snapshot",+ to="posthog.organization",+ ),+ ),+ ],+ options={+ "abstract": False,+ },+ ),+ ]diff --git a/products/growth/backend/migrations/max_migration.txt b/products/growth/backend/migrations/max_migration.txtindex 111c4dc7c854..db529cb0b0c9 100644--- a/products/growth/backend/migrations/max_migration.txt+++ b/products/growth/backend/migrations/max_migration.txt@@ -1 +1 @@-0002_organizationenrichment+0003_enrichmentsignupsnapshotdiff --git a/products/growth/backend/models.py b/products/growth/backend/models.pyindex 2e191e6a636d..f1f0714397cc 100644--- a/products/growth/backend/models.py+++ b/products/growth/backend/models.py@@ -113,3 +113,22 @@ class OrganizationEnrichment(UUIDModel):created_at = models.DateTimeField(auto_now_add=True)updated_at = models.DateTimeField(auto_now=True)+++class EnrichmentSignupSnapshot(UUIDModel):+ """Write-once marker that the at-signup enrichment snapshot has been emitted for an org.++ Stores no firmographic values (those live only on the person-scoped snapshot event); this+ row is purely the idempotency guard and provenance timestamp. The OneToOne unique constraint+ lets concurrent runs make at most one row per org.+ """++ # db_constraint=False keeps CreateModel off posthog_organization's lock path (hot table);+ # the OneToOne still gives the write-once uniqueness guarantee.+ organization = models.OneToOneField(+ "posthog.Organization",+ on_delete=models.CASCADE,+ db_constraint=False,+ related_name="enrichment_signup_snapshot",+ )+ created_at = models.DateTimeField(auto_now_add=True)diff --git a/products/growth/backend/tests/test_snapshot.py b/products/growth/backend/tests/test_snapshot.pynew file mode 100644index 000000000000..d38480de5eab--- /dev/null+++ b/products/growth/backend/tests/test_snapshot.py@@ -0,0 +1,112 @@+from posthog.test.base import BaseTest+from unittest.mock import MagicMock++from parameterized import parameterized++from products.growth.backend.enrichment.snapshot import (+ SNAPSHOT_EVENT_NAME,+ SignupEnrichmentSnapshot,+ capture_signup_enrichment_snapshot,+)+from products.growth.backend.models import EnrichmentSignupSnapshot+++class TestSignupEnrichmentSnapshot(BaseTest):+ def test_to_event_properties_suffixes_keys_and_drops_none(self) -> None:+ snapshot = SignupEnrichmentSnapshot(+ company_type="startup",+ headcount=42,+ industry="software",+ icp_score=80,+ )++ properties = snapshot.to_event_properties()++ assert properties == {+ "company_type_at_signup": "startup",+ "headcount_at_signup": 42,+ "industry_at_signup": "software",+ "icp_score_at_signup": 80,+ }+ assert "country_at_signup" not in properties++ def test_emits_person_scoped_event_when_snapshot_lands(self) -> None:+ pha_client = MagicMock()+ snapshot = SignupEnrichmentSnapshot(company_type="startup", headcount=42)++ emitted = capture_signup_enrichment_snapshot(+ pha_client,+ organization_id=str(self.organization.id),+ distinct_id="user-distinct-id",+ snapshot=snapshot,+ )++ assert emitted is True+ pha_client.capture.assert_called_once_with(+ distinct_id="user-distinct-id",+ event=SNAPSHOT_EVENT_NAME,+ properties={"company_type_at_signup": "startup", "headcount_at_signup": 42},+ groups={"organization": str(self.organization.id)},+ )+ assert EnrichmentSignupSnapshot.objects.filter(organization_id=self.organization.id).count() == 1++ def test_written_once_second_call_does_not_re_emit(self) -> None:+ pha_client = MagicMock()+ snapshot = SignupEnrichmentSnapshot(company_type="startup")++ first = capture_signup_enrichment_snapshot(+ pha_client,+ organization_id=str(self.organization.id),+ distinct_id="user-distinct-id",+ snapshot=snapshot,+ )+ second = capture_signup_enrichment_snapshot(+ pha_client,+ organization_id=str(self.organization.id),+ distinct_id="user-distinct-id",+ snapshot=SignupEnrichmentSnapshot(company_type="enterprise"),+ )++ assert first is True+ assert second is False+ pha_client.capture.assert_called_once()+ assert EnrichmentSignupSnapshot.objects.filter(organization_id=self.organization.id).count() == 1++ def test_does_not_emit_when_guard_row_preexists(self) -> None:+ pha_client = MagicMock()+ EnrichmentSignupSnapshot.objects.create(organization_id=self.organization.id)++ emitted = capture_signup_enrichment_snapshot(+ pha_client,+ organization_id=str(self.organization.id),+ distinct_id="user-distinct-id",+ snapshot=SignupEnrichmentSnapshot(company_type="startup"),+ )++ assert emitted is False+ pha_client.capture.assert_not_called()++ @parameterized.expand(+ [+ ("present", "2026-07-01", True),+ ("absent", None, False),+ ]+ )+ def test_icp_score_version_in_properties(+ self, _name: str, icp_score_version: str | None, expect_version: bool+ ) -> None:+ pha_client = MagicMock()+ snapshot = SignupEnrichmentSnapshot(icp_score=90, icp_score_version=icp_score_version)++ capture_signup_enrichment_snapshot(+ pha_client,+ organization_id=str(self.organization.id),+ distinct_id="user-distinct-id",+ snapshot=snapshot,+ )++ _, kwargs = pha_client.capture.call_args+ assert kwargs["properties"]["icp_score_at_signup"] == 90+ assert ("icp_score_version_at_signup" in kwargs["properties"]) is expect_version+ if expect_version:+ assert kwargs["properties"]["icp_score_version_at_signup"] == icp_score_version