AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): send the MailerLite segmentation fields to PostHog too (#15466)
Nicholas Tindlef40cc6892a3c
diff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill.pyindex a685fe9616..61e56964d2 100644--- a/autogpt_platform/backend/backend/cli/mailerlite_backfill.py+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill.py@@ -234,14 +234,14 @@ async def stripe_billing_countries(async for customer in stripe_list_items(page):country = (customer.get("address") or {}).get("country")_keep_country(countries, customer.id, country)- for session in sessions if sessions is not None else await _checkout_sessions():+ for session in sessions if sessions is not None else await checkout_sessions():customer = session.get("customer")if isinstance(customer, str):_keep_country(countries, customer, billing_country(session))return countries-async def _checkout_sessions() -> list[dict]:+async def checkout_sessions() -> list[dict]:"""Every Stripe Checkout Session."""import stripe@@ -251,6 +251,18 @@ async def _checkout_sessions() -> list[dict]:return [session async for session in stripe_list_items(page)]+def first_opens(sessions: list[dict]) -> dict[str, int]:+ """When each Stripe customer first opened checkout: its first Checkout+ Session's creation, as a Unix timestamp, by customer ID."""+ opens: dict[str, int] = {}+ for session in sessions:+ customer = session.get("customer")+ if isinstance(customer, str):+ created = int(session["created"])+ opens[customer] = min(opens.get(customer, created), created)+ return opens++def _keep_country(countries: dict[str, str], customer_id: str, country: str | None) -> None:@@ -442,20 +454,15 @@ async def _run_checkout(*, apply: bool, yes: bool) -> None:stripe.api_key = settings.secrets.stripe_api_key# Listed once, for the first opens and the billing countries alike.- sessions = await _checkout_sessions()- first_open: dict[str, int] = {}- for session in sessions:- customer = session.get("customer")- if isinstance(customer, str):- created = int(session["created"])- first_open[customer] = min(first_open.get(customer, created), created)+ sessions = await checkout_sessions()+ first_open = first_opens(sessions)billing_countries = await stripe_billing_countries(sessions)subscriptions = await _stripe_subscriptions()await connect()try:people = await _people(subscriptions, billing_countries)- providers = await _signin_providers()+ providers = await signin_providers_by_account()finally:await disconnect()@@ -521,7 +528,7 @@ def _finish_checkout(ok: int, failed: int, skipped: int) -> None:click.echo("Run the dry run again to confirm nothing is left.")-async def _signin_providers() -> dict[str, list[str]]:+async def signin_providers_by_account() -> dict[str, list[str]]:"""Every account's Better Auth providers, by account id."""from backend.data.db import query_raw_with_schemadiff --git a/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.pyindex c5f592032a..e433ed5451 100644--- a/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py+++ b/autogpt_platform/backend/backend/cli/mailerlite_backfill_test.py@@ -230,3 +230,13 @@ async def test_sessions_already_listed_are_not_listed_again(monkeypatch):monkeypatch.setattr("backend.data.stripe_client.stripe_call", call)monkeypatch.setattr("backend.data.stripe_client.stripe_list_items", listed)assert await cli.stripe_billing_countries(sessions) == {"cus_a": "IR"}+++def test_each_customers_first_open_is_its_earliest_session():+ sessions = [+ {"customer": "cus_a", "created": 300},+ {"customer": "cus_a", "created": 100},+ {"customer": "cus_b", "created": 200},+ {"customer": None, "created": 50},+ ]+ assert cli.first_opens(sessions) == {"cus_a": 100, "cus_b": 200}diff --git a/autogpt_platform/backend/backend/cli/main.py b/autogpt_platform/backend/backend/cli/main.pyindex 6bed8e2854..ce1c0bb865 100644--- a/autogpt_platform/backend/backend/cli/main.py+++ b/autogpt_platform/backend/backend/cli/main.py@@ -15,6 +15,7 @@ from .mailerlite_backfill import (mailerlite_fields_command,)from .onboarding_role_backfill import onboarding_role_backfill_command+from .posthog_audience_backfill import posthog_audience_backfill_commandfrom .rotate_key import rotate_encryption_keyfrom .store import storefrom .test import test@@ -31,6 +32,7 @@ main.add_command(mailerlite_backfill_command)main.add_command(mailerlite_checkout_backfill_command)main.add_command(mailerlite_fields_command)main.add_command(onboarding_role_backfill_command)+main.add_command(posthog_audience_backfill_command)main.add_command(rotate_encryption_key)main.add_command(store)main.add_command(test)diff --git a/autogpt_platform/backend/backend/cli/onboarding_role_backfill.py b/autogpt_platform/backend/backend/cli/onboarding_role_backfill.pyindex 24582cc918..b49371bf62 100644--- a/autogpt_platform/backend/backend/cli/onboarding_role_backfill.py+++ b/autogpt_platform/backend/backend/cli/onboarding_role_backfill.py@@ -43,9 +43,12 @@ def onboarding_role_backfill_command(apply: bool, yes: bool):opted out of marketing or whom a signal places in Iran or Russia, theirStripe billing country included, is left out.- Dry run by default, with counts only. Idempotent, so a repeated --apply- resumes an interrupted one; run the dry run again afterwards to confirm- nothing is left.+ Dry run by default, with counts only. A run that PostHog or MailerLite+ didn't take in full fails after reporting. Idempotent, so a repeated+ --apply resumes an interrupted or failed one. A dry run afterwards shows+ whether any MailerLite subscribers are left to fill in. It can't read+ PostHog, so it lists every reliable role there again; a run that finished+ without failing sent them all."""# Keep Prisma and client chatter out of the report.logging.disable(logging.INFO)@@ -60,10 +63,10 @@ async def _run(*, apply: bool, yes: bool) -> None:from backend.notifications import mailerlite_field_backfill as field_backfillfrom backend.notifications import role_backfillfrom backend.notifications.mailerlite import MailerLiteNotConfigured- from backend.util.posthog_client import get_posthog_clientfrom backend.util.settings import Settings- if not Settings().secrets.stripe_api_key:+ settings = Settings()+ if not settings.secrets.stripe_api_key:raise click.ClickException("STRIPE_API_KEY is not set.")try:current = await field_backfill.read_current()@@ -85,7 +88,7 @@ async def _run(*, apply: bool, yes: bool) -> None:if not plan.posthog and not plan.mailerlite:click.echo("\nNothing to write.")return- if get_posthog_client() is None:+ if not settings.secrets.posthog_api_key:raise click.ClickException("POSTHOG_API_KEY is not set.")if not yes:click.confirm(@@ -93,8 +96,10 @@ async def _run(*, apply: bool, yes: bool) -> None:f"{len(plan.mailerlite)} MailerLite subscribers?",abort=True,)- await role_backfill.send_to_posthog(plan.posthog)- click.echo(f"PostHog: {len(plan.posthog)} sent")+ posthog_failed = await role_backfill.send_to_posthog(plan.posthog)+ click.echo(+ f"PostHog: {len(plan.posthog) - posthog_failed} sent, {posthog_failed} failed"+ )if plan.mailerlite:await mailerlite.ensure_fields()# Connected again for the writes: each subscriber is checked against@@ -107,6 +112,11 @@ async def _run(*, apply: bool, yes: bool) -> None:finally:await disconnect()_finish(ok, failed, skipped)+ if posthog_failed:+ raise click.ClickException(+ f"{posthog_failed} PostHog persons were not sent (the log says why); "+ "run again to retry them"+ )async def _records() -> "list[RoleRecord]":@@ -143,7 +153,10 @@ def _report(plan: "RolePlan") -> None:click.echo(f" reliable, an exact option in the understanding: {plan.exact}")click.echo(f" skipped, Other's text or an AutoPilot rewrite: {plan.skipped}")click.echo(f"Roles: {roles}")- click.echo(f"\nPostHog: {len(plan.posthog)} persons ($set_once)")+ click.echo(+ f"\nPostHog: {len(plan.posthog)} persons ($set_once; PostHog can't be "+ "read, so every run sends them all)"+ )click.echo(f"MailerLite: {len(plan.mailerlite)} subscribers to fill in")click.echo(f" already have a role (kept): {plan.already_set}")click.echo(f" not subscribers (never created): {plan.not_subscribers}")@@ -170,4 +183,7 @@ def _finish(ok: int, failed: int, skipped: int) -> None:f"{failed} MailerLite subscribers were not written (the log says ""why); run again to retry them")- click.echo("Run the dry run again to confirm nothing is left.")+ click.echo(+ "Run the dry run again to confirm no MailerLite subscriber is left to "+ "fill in."+ )diff --git a/autogpt_platform/backend/backend/cli/onboarding_role_backfill_test.py b/autogpt_platform/backend/backend/cli/onboarding_role_backfill_test.pyindex fe72545be3..c2c15b92d7 100644--- a/autogpt_platform/backend/backend/cli/onboarding_role_backfill_test.py+++ b/autogpt_platform/backend/backend/cli/onboarding_role_backfill_test.py@@ -1,14 +1,22 @@"""The role backfill's dry run reports reliable against skipped accounts andwhere each one would go, by count only, and a run that could not write-everyone fails as a whole after reporting."""+everyone, in PostHog or MailerLite, fails as a whole after reporting."""++from types import SimpleNamespace+from unittest.mock import AsyncMockimport clickimport pytestfrom click.testing import CliRunner+from backend.cli import mailerlite_backfillfrom backend.cli import onboarding_role_backfill as cli+from backend.data import db+from backend.data import user as user_data+from backend.notifications import mailerlite_field_backfill as field_backfillfrom backend.notifications import role_backfillfrom backend.notifications.role_backfill import RoleRecord+from backend.util import settings# The root conftest spins a full test server for every test via an autouse@@ -43,7 +51,8 @@ def test_the_dry_run_report_counts_reliable_and_skipped(capsys):assert "reliable, an exact option in the understanding: 2" in outassert "skipped, Other's text or an AutoPilot rewrite: 1" in outassert "Roles: Marketing=2, Engineering=1" in out- assert "PostHog: 3 persons" in out+ # PostHog can't be read, so a dry run after a full apply lists them again.+ assert "PostHog: 3 persons ($set_once; PostHog can't be read" in outassert "MailerLite: 1 subscribers to fill in" in outassert "already have a role (kept): 1" in outassert "placed in Iran or Russia (skipped): 1" in out@@ -54,7 +63,7 @@ def test_a_complete_run_reports_and_succeeds(capsys):cli._finish(5, 0, 2)out = capsys.readouterr().outassert "5 ok, 0 failed, 2 no longer needed" in out- assert "Run the dry run again" in out+ assert "dry run again to confirm no MailerLite subscriber is left" in outdef test_the_command_exits_non_zero_when_anyone_failed(monkeypatch):@@ -83,3 +92,60 @@ def test_each_record_gets_its_stripe_billing_country():]filled = cli._with_billing_countries(records, {"cus_1": "RU", "cus_2": "US"})assert [r.billing_country for r in filled] == ["RU", "US", None]+++def applied(monkeypatch: pytest.MonkeyPatch) -> AsyncMock:+ """An --apply run with one reliable pick that is not a MailerLite+ subscriber, so only PostHog is written; returns where it is sent."""+ # The run reads the opt-out right before each MailerLite write; there are+ # none here.+ monkeypatch.setattr(+ user_data, "is_marketing_opted_out", AsyncMock(return_value=False)+ )+ secrets = SimpleNamespace(stripe_api_key="sk_test", posthog_api_key="phc_test")+ monkeypatch.setattr(settings, "Settings", lambda: SimpleNamespace(secrets=secrets))+ monkeypatch.setattr(field_backfill, "read_current", AsyncMock(return_value={}))+ monkeypatch.setattr(+ mailerlite_backfill, "stripe_billing_countries", AsyncMock(return_value={})+ )+ monkeypatch.setattr(db, "connect", AsyncMock())+ monkeypatch.setattr(db, "disconnect", AsyncMock())+ monkeypatch.setattr(+ cli,+ "_records",+ AsyncMock(+ return_value=[RoleRecord(user_id="1", email="[email protected]", choice="Marketing")]+ ),+ )+ send = AsyncMock(return_value=0)+ monkeypatch.setattr(role_backfill, "send_to_posthog", send)+ return send+++async def test_a_run_posthog_took_in_full_reports_and_succeeds(applied, capsys):+ await cli._run(apply=True, yes=True)+ assert "PostHog: 1 sent, 0 failed" in capsys.readouterr().out+++async def test_a_run_posthog_did_not_take_in_full_fails_after_reporting(+ applied, capsys+):+ """So a Job or script never reads a partial run as done."""+ applied.return_value = 1+ with pytest.raises(click.ClickException, match="1 PostHog persons"):+ await cli._run(apply=True, yes=True)+ assert "PostHog: 0 sent, 1 failed" in capsys.readouterr().out+++async def test_an_applied_run_without_posthog_fails_before_sending(+ applied, monkeypatch: pytest.MonkeyPatch+):+ secrets = SimpleNamespace(stripe_api_key="sk_test", posthog_api_key="")+ monkeypatch.setattr(settings, "Settings", lambda: SimpleNamespace(secrets=secrets))+ with pytest.raises(click.ClickException, match="POSTHOG_API_KEY"):+ await cli._run(apply=True, yes=True)+ applied.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/cli/posthog_audience_backfill.py b/autogpt_platform/backend/backend/cli/posthog_audience_backfill.pynew file mode 100644index 0000000000..2491603334--- /dev/null+++ b/autogpt_platform/backend/backend/cli/posthog_audience_backfill.py@@ -0,0 +1,125 @@+import asyncio+import logging+from typing import TYPE_CHECKING++import click++if TYPE_CHECKING:+ from backend.notifications.posthog_audience_backfill import Account, AudiencePlan++_ACCOUNTS = """+ SELECT u."id" AS user_id, u."email", u."createdAt" AS created_at,+ u."timezone", u."stripeCustomerId" AS stripe_customer_id+ FROM {schema_prefix}"User" u+"""++[email protected](name="posthog-audience-backfill")[email protected]("--apply", is_flag=True, help="Send the properties. Without it, dry run.")[email protected]("--yes", is_flag=True, help="With --apply, skip the confirmation.")+def posthog_audience_backfill_command(apply: bool, yes: bool):+ """Give existing accounts the PostHog person properties that mirror+ MailerLite's segmentation (SECRT-2895).++ Every account gets email_type and signin_method. Every account with a+ Stripe Checkout Session also gets checkout_opened_at, country,+ country_code, country_source and exclude_de_at, worked out as+ mailerlite-checkout-backfill works them out: from the Stripe billing+ country, or else the browser's timezone. Nobody is left out, since PostHog+ is analytics, not marketing.++ checkout_opened_at is $set from the first Checkout Session. A timezone+ country and an exclude_de_at "no" are $set_once, so what a live checkout+ recorded (its IP country, a German or Austrian signal) stays.++ Dry run by default, with counts only. A run that PostHog didn't take in+ full, at queueing or upload, fails after reporting; a repeated --apply+ sends the same values, so running it again is harmless.+ """+ # Keep Prisma and client chatter out of the report.+ logging.disable(logging.INFO)+ asyncio.run(_run(apply=apply, yes=yes))+++async def _run(*, apply: bool, yes: bool) -> None:+ import stripe++ from backend.cli.mailerlite_backfill import (+ checkout_sessions,+ first_opens,+ signin_providers_by_account,+ stripe_billing_countries,+ )+ from backend.data.db import connect, disconnect+ from backend.notifications import posthog_audience_backfill as backfill+ from backend.util.settings import Settings++ settings = Settings()+ if not settings.secrets.stripe_api_key:+ raise click.ClickException("STRIPE_API_KEY is not set.")+ if apply and not settings.secrets.posthog_api_key:+ raise click.ClickException("POSTHOG_API_KEY is not set.")+ stripe.api_key = settings.secrets.stripe_api_key+ # Listed once, for the first opens and the billing countries alike.+ sessions = await checkout_sessions()+ billing_countries = await stripe_billing_countries(sessions)+ await connect()+ try:+ accounts = await _accounts()+ providers = await signin_providers_by_account()+ finally:+ await disconnect()++ plan = backfill.plan(accounts, providers, first_opens(sessions), billing_countries)+ _report(plan)++ if not apply:+ click.echo("\nDry run: nothing was sent. Re-run with --apply to send.")+ return+ if not plan.updates:+ click.echo("\nNothing to send.")+ return+ if not yes:+ click.confirm(+ f"\nSend the properties of {len(plan.updates)} accounts to PostHog?",+ abort=True,+ )+ _finish(len(plan.updates), await backfill.send(plan.updates))+++async def _accounts() -> "list[Account]":+ from backend.data.db import query_raw_with_schema+ from backend.notifications.posthog_audience_backfill import Account++ return await query_raw_with_schema(_ACCOUNTS, model=Account)+++def _report(plan: "AudiencePlan") -> None:+ click.echo(f"Accounts: {len(plan.updates)}")+ click.echo(f" email_type: {_counts(plan.email_types)}")+ click.echo(f" signin_method: {_counts(plan.signin_methods)}")+ click.echo(f"Checkout openers (a Stripe Checkout Session): {plan.openers}")+ click.echo(f" country_source: {_counts(plan.country_sources)}")+ click.echo(f" exclude_de_at yes: {plan.exclude_de_at}")+ click.echo(+ "These are the values the run sends. A timezone country and an "+ 'exclude_de_at "no" are $set_once: they land only where the person '+ "holds none yet."+ )+ click.echo(f"\nPostHog: {len(plan.updates)} persons")+++def _finish(total: int, failed: int) -> None:+ """Report the run, and fail it when PostHog didn't take everyone, so a Job+ or script never reads a partial run as done."""+ click.echo(f"PostHog: {total - failed} sent, {failed} failed")+ if failed:+ raise click.ClickException(+ f"{failed} accounts were not sent (the log says why); run again to "+ "retry them, since a run sends the same values every time"+ )+++def _counts(counts: dict[str, int]) -> str:+ ordered = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0]))+ return ", ".join(f"{value}={count}" for value, count in ordered) or "-"diff --git a/autogpt_platform/backend/backend/cli/posthog_audience_backfill_db_test.py b/autogpt_platform/backend/backend/cli/posthog_audience_backfill_db_test.pynew file mode 100644index 0000000000..eca9825545--- /dev/null+++ b/autogpt_platform/backend/backend/cli/posthog_audience_backfill_db_test.py@@ -0,0 +1,60 @@+"""The PostHog audience backfill's reads against the real tables: every account+with what its plan needs, and every account's sign-in providers. The unit tests+patch both out, so a renamed column would leave them green."""++from uuid import uuid4++import pytest+from prisma.models import AuthAccount, AuthUser, User++from backend.cli import mailerlite_backfill+from backend.cli import posthog_audience_backfill as cli+from backend.util.test import SpinTestServer+++async def account(server: SpinTestServer):+ """An account with an email sign-in, a browser timezone and a Stripe+ customer."""+ user_id = str(uuid4())+ email = f"{user_id}@example.com"+ await AuthUser.prisma().create(+ data={"id": user_id, "name": "PostHog backfill", "email": email}+ )+ await AuthAccount.prisma().create(+ data={+ "id": str(uuid4()),+ "accountId": user_id,+ "providerId": "credential",+ "userId": user_id,+ }+ )+ await User.prisma().create(+ data={+ "id": user_id,+ "email": email,+ "timezone": "Europe/Vienna",+ "stripeCustomerId": f"cus_{user_id}",+ }+ )+ yield user_id+ await User.prisma().delete(where={"id": user_id})+ # Takes its AuthAccount with it.+ await AuthUser.prisma().delete(where={"id": user_id})++[email protected](loop_scope="session")+async def test_every_account_is_read_with_what_its_plan_needs(account: str):+ (row,) = [a for a in await cli._accounts() if a.user_id == account]++ assert row.email == f"{account}@example.com"+ assert row.timezone == "Europe/Vienna"+ assert row.stripe_customer_id == f"cus_{account}"+ assert row.created_at is not None++[email protected](loop_scope="session")+async def test_each_accounts_sign_in_providers_are_read(account: str):+ providers = await mailerlite_backfill.signin_providers_by_account()++ assert providers[account] == ["credential"]diff --git a/autogpt_platform/backend/backend/cli/posthog_audience_backfill_test.py b/autogpt_platform/backend/backend/cli/posthog_audience_backfill_test.pynew file mode 100644index 0000000000..9299e64cbf--- /dev/null+++ b/autogpt_platform/backend/backend/cli/posthog_audience_backfill_test.py@@ -0,0 +1,144 @@+"""The PostHog backfill's dry run reports counts only and sends nothing, an+--apply run sends the whole plan, and a run PostHog didn't take in full fails+after reporting."""++from datetime import UTC, datetime+from types import SimpleNamespace+from unittest.mock import AsyncMock++import click+import pytest+import stripe++from backend.cli import mailerlite_backfill+from backend.cli import posthog_audience_backfill as cli+from backend.data import db+from backend.notifications import posthog_audience_backfill as backfill+from backend.notifications.posthog_audience_backfill import Account+from backend.util import settings++CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+ACCOUNTS = [+ Account(+ user_id="1",+ email="[email protected]",+ created_at=CREATED,+ timezone="Europe/Vienna",+ stripe_customer_id="cus_1",+ ),+ Account(user_id="2", email="[email protected]", created_at=CREATED),+ Account(user_id="3", email="[email protected]", created_at=CREATED),+]+SESSIONS = [+ {"customer": "cus_1", "created": 1788305400},+ {"customer": "cus_1", "created": 1788305000},+ {"customer": None, "created": 1788300000},+]+++def _settings(stripe_api_key: str = "sk_test", posthog_api_key: str = "phc_test"):+ secrets = SimpleNamespace(+ stripe_api_key=stripe_api_key, posthog_api_key=posthog_api_key+ )+ return lambda: SimpleNamespace(secrets=secrets)+++def wired(monkeypatch: pytest.MonkeyPatch) -> AsyncMock:+ """Stripe, the database and PostHog as the run sees them; returns where+ the plan is sent, which reports nobody failed."""+ # Restored afterwards: the run sets the module-wide key.+ monkeypatch.setattr(stripe, "api_key", stripe.api_key)+ monkeypatch.setattr(settings, "Settings", _settings())+ monkeypatch.setattr(+ mailerlite_backfill, "checkout_sessions", AsyncMock(return_value=SESSIONS)+ )+ monkeypatch.setattr(+ mailerlite_backfill,+ "stripe_billing_countries",+ AsyncMock(return_value={"cus_1": "US"}),+ )+ monkeypatch.setattr(+ mailerlite_backfill,+ "signin_providers_by_account",+ AsyncMock(return_value={"1": ["google"]}),+ )+ monkeypatch.setattr(db, "connect", AsyncMock())+ monkeypatch.setattr(db, "disconnect", AsyncMock())+ monkeypatch.setattr(cli, "_accounts", AsyncMock(return_value=ACCOUNTS))+ send = AsyncMock(return_value=0)+ monkeypatch.setattr(backfill, "send", send)+ return send+++async def test_a_dry_run_reports_counts_and_sends_nothing(wired: AsyncMock, capsys):+ await cli._run(apply=False, yes=False)+ out = capsys.readouterr().out+ assert "Accounts: 3" in out+ assert "email_type: business=2, personal=1" in out+ assert "signin_method: none=2, google=1" in out+ assert "Checkout openers (a Stripe Checkout Session): 1" in out+ assert "country_source: stripe=1" in out+ assert "exclude_de_at yes: 1" in out+ # Not what PostHog will hold: a $set_once value may not land.+ assert "These are the values the run sends" in out+ assert "PostHog: 3 persons" in out+ assert "Dry run: nothing was sent" in out+ assert "acme.io" not in out and "gmail.com" not in out+ wired.assert_not_awaited()+++async def test_an_applied_run_sends_the_whole_plan(wired: AsyncMock, capsys):+ await cli._run(apply=True, yes=True)+ (updates,) = wired.await_args.args+ assert [change.user_id for change in updates] == ["1", "2", "3"]+ opener = updates[0].update+ # The first of the customer's Checkout Sessions, not the latest.+ assert opener.to_set["checkout_opened_at"] == "2026-09-01T23:23:20+00:00"+ assert opener.to_set["country_source"] == "stripe"+ assert "PostHog: 3 sent, 0 failed" in capsys.readouterr().out+++async def test_a_run_posthog_did_not_take_in_full_fails_after_reporting(+ wired: AsyncMock, capsys+):+ """So a Job or script never reads a partial run as done."""+ wired.return_value = 2+ with pytest.raises(click.ClickException, match="2 accounts were not sent"):+ await cli._run(apply=True, yes=True)+ assert "PostHog: 1 sent, 2 failed" in capsys.readouterr().out+++async def test_an_applied_run_without_posthog_fails_before_reading_anything(+ wired: AsyncMock, monkeypatch+):+ listed = AsyncMock(return_value=SESSIONS)+ monkeypatch.setattr(mailerlite_backfill, "checkout_sessions", listed)+ monkeypatch.setattr(settings, "Settings", _settings(posthog_api_key=""))+ with pytest.raises(click.ClickException, match="POSTHOG_API_KEY"):+ await cli._run(apply=True, yes=True)+ listed.assert_not_awaited()+ wired.assert_not_awaited()+++async def test_a_dry_run_needs_no_posthog(wired: AsyncMock, monkeypatch, capsys):+ monkeypatch.setattr(settings, "Settings", _settings(posthog_api_key=""))+ await cli._run(apply=False, yes=False)+ assert "Dry run: nothing was sent" in capsys.readouterr().out+++async def test_a_run_without_stripe_fails_before_reading_anything(+ wired: AsyncMock, monkeypatch+):+ listed = AsyncMock(return_value=SESSIONS)+ monkeypatch.setattr(mailerlite_backfill, "checkout_sessions", listed)+ monkeypatch.setattr(settings, "Settings", _settings(stripe_api_key=""))+ with pytest.raises(click.ClickException, match="STRIPE_API_KEY"):+ await cli._run(apply=False, yes=False)+ listed.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/data/checkout_audience.py b/autogpt_platform/backend/backend/data/checkout_audience.pyindex 89661959d7..659c8bf550 100644--- a/autogpt_platform/backend/backend/data/checkout_audience.py+++ b/autogpt_platform/backend/backend/data/checkout_audience.py@@ -1,19 +1,23 @@-"""Queue the checkout-opened audience change for an account.+"""Queue the checkout-opened audience change for an account, and set the same+segmentation on its PostHog person.Called by the checkout routes once a Stripe Checkout Session exists, with thevisitor's IP country, and by the checkout.session.completed webhook with theStripe billing country. Firing on every checkout, not once, is safe: thenotification service keeps the first open date, an existing status, and the-strongest country it has seen (`audience_enrichment.merge_with_held`).+strongest country it has seen (`audience_enrichment.merge_with_held`), and+PostHog gets the same rules from `$set_once` (`notifications/posthog_audience.py`).It runs in the background and never raises. A checkout must not wait on, or-fail because of, MailerLite bookkeeping; the backfill catches anyone missed.+fail because of, MailerLite or PostHog bookkeeping; the backfills catch anyone+missed.Nothing is queued for an account that opted out of marketing, or that anysignal places in Iran or Russia, the IP and billing countries included: it-never enters MailerLite (`notifications/consent.py`). An Iranian or Russian-IP or billing country is also recorded on the account, since the trial and-billing events that follow a checkout don't carry it.+never enters MailerLite (`notifications/consent.py`). PostHog is analytics,+not marketing, so it gets every opener. An Iranian or Russian IP or billing+country is also recorded on the account, since the trial and billing events+that follow a checkout don't carry it."""import asyncio@@ -26,10 +30,12 @@ from backend.data.db import query_raw_with_schemafrom backend.data.notifications import AudienceActionfrom backend.data.onboarding_role import OnboardingRole, get_onboarding_rolefrom backend.data.user import get_user_by_id, record_excluded_country+from backend.notifications import posthog_audiencefrom backend.notifications.audience_enrichment import (billing_country,checkout_fields,excluded_country,+ role_fields,)from backend.notifications.consent import audience_change_allowedfrom backend.notifications.subscriber_fields import queue_fields@@ -75,10 +81,6 @@ async def queue_checkout_opened(try:await _record_excluded_country(user_id, (ip_country, stripe_country))user = await get_user_by_id(user_id)- if not audience_change_allowed(- user, AudienceAction.CHECKOUT_OPENED, (ip_country, stripe_country)- ):- returnfields = checkout_fields(email=user.email,created_at=user.created_at,@@ -87,8 +89,16 @@ async def queue_checkout_opened(timezone=user.timezone,stripe_country=stripe_country,ip_country=ip_country,- role=await _role(user_id),)+ posthog_audience.set_checkout_properties(user_id, fields, opened_at)+ if not audience_change_allowed(+ user, AudienceAction.CHECKOUT_OPENED, (ip_country, stripe_country)+ ):+ return+ # The profile step sends the role to PostHog itself; only MailerLite+ # gets it here.+ if role := await _role(user_id):+ fields.update(role_fields(role))await queue_fields(user_id, user.email, fields, AudienceAction.CHECKOUT_OPENED)except Exception:logger.exception(f"Could not queue the MailerLite checkout for {user_id}")diff --git a/autogpt_platform/backend/backend/data/checkout_audience_test.py b/autogpt_platform/backend/backend/data/checkout_audience_test.pyindex 88b1051d6e..b9524f2da4 100644--- a/autogpt_platform/backend/backend/data/checkout_audience_test.py+++ b/autogpt_platform/backend/backend/data/checkout_audience_test.py@@ -1,6 +1,7 @@-"""Opening Stripe checkout queues the checkout opener for MailerLite, from-each of the three checkout routes and the completed-checkout webhook, and none-of it can cost the checkout or fail the webhook."""+"""Opening Stripe checkout queues the checkout opener for MailerLite and sets+the same segmentation on the PostHog person, from each of the three checkout+routes and the completed-checkout webhook, and none of it can cost the+checkout or fail the webhook."""import inspectimport logging@@ -24,7 +25,7 @@ from backend.data.notifications import ()from backend.data.onboarding_role import OnboardingRolefrom backend.data.subscription_trial_checkout import TrialUnavailable-from backend.notifications import consent, subscriber_fields+from backend.notifications import consent, posthog_audience, subscriber_fieldsfrom backend.notifications.consent_test import _cached_before_consentfrom backend.notifications.mailerlite import pseudonym@@ -48,6 +49,22 @@ def graph_cleanup():# ── queueing it ──────────────────────────────────────────────────────────────[email protected](autouse=True)+def posthog(monkeypatch) -> MagicMock:+ """Where the opener's PostHog person properties go."""+ send = MagicMock()+ monkeypatch.setattr(posthog_audience, "send", send)+ return send+++def _person(posthog: MagicMock) -> dict[str, str]:+ """Everything the one PostHog update carries."""+ posthog.assert_called_once()+ user_id, update = posthog.call_args.args+ assert user_id == "user-1"+ return {**update.to_set_once, **update.to_set}++@pytest.fixturedef recorded(monkeypatch):"""Where an Iranian or Russian country would be recorded on the account."""@@ -99,6 +116,36 @@ async def test_opening_checkout_queues_the_enriched_change(queued, recorded):assert event.fields[SubscriberField.EXCLUDE_DE_AT] == "yes"+async def test_opening_checkout_sets_the_same_values_on_the_posthog_person(+ queued, posthog+):+ await checkout_audience.queue_checkout_opened("user-1", ip_country="US")+ fields = queued.await_args.args[0].fields+ person = _person(posthog)+ for field in (+ SubscriberField.EMAIL_TYPE,+ SubscriberField.SIGNIN_METHOD,+ SubscriberField.COUNTRY,+ SubscriberField.COUNTRY_CODE,+ SubscriberField.COUNTRY_SOURCE,+ SubscriberField.EXCLUDE_DE_AT,+ ):+ assert person[field.value] == fields[field]+ assert "checkout_opened_at" in person+ # The profile step sends the role to PostHog itself.+ assert "role" not in person+++async def test_a_posthog_failure_never_costs_the_mailerlite_change(queued, monkeypatch):+ monkeypatch.setattr(+ posthog_audience, "checkout_update", MagicMock(side_effect=RuntimeError("bug"))+ )+ await checkout_audience.queue_checkout_opened("user-1", ip_country="US")+ assert queued.await_args.args[0].action is AudienceAction.CHECKOUT_OPENED++@pytest.mark.asyncio@pytest.mark.parametrize("role",@@ -130,17 +177,20 @@ async def test_an_opener_without_a_readable_role_is_still_queued(],)async def test_an_opener_seen_in_iran_or_russia_is_recorded_and_never_queued(- queued, recorded, monkeypatch, caplog, countries, code+ queued, recorded, posthog, monkeypatch, caplog, countries, code):"""Recorded on the account, since the trial and billing events that- follow carry no IP or billing country."""- providers = AsyncMock(return_value=["credential"])- monkeypatch.setattr(checkout_audience, "signin_providers", providers)+ follow carry no IP or billing country. PostHog is analytics, not+ marketing, so it still gets the opener; the role, which only MailerLite+ gets from here, is not read."""+ role = AsyncMock(return_value=OnboardingRole(choice="Marketing"))+ monkeypatch.setattr(checkout_audience, "get_onboarding_role", role)with caplog.at_level(logging.DEBUG, logger=consent.__name__):await checkout_audience.queue_checkout_opened("user-1", **countries)recorded.assert_awaited_once_with("user-1", code)queued.assert_not_awaited()- providers.assert_not_awaited()+ role.assert_not_awaited()+ assert _person(posthog)["country_code"] == codeassert pseudonym(EMAIL) in caplog.textassert EMAIL not in caplog.text@@ -177,15 +227,16 @@ async def test_an_opener_whose_browser_sits_in_moscow_is_never_queued(@pytest.mark.asyncio-async def test_a_failure_never_reaches_the_checkout(queued, monkeypatch):+async def test_a_failure_never_reaches_the_checkout(queued, posthog, monkeypatch):monkeypatch.setattr(checkout_audience, "get_user_by_id", AsyncMock(side_effect=RuntimeError("db")))await checkout_audience.queue_checkout_opened("user-1", ip_country="US")queued.assert_not_awaited()+ posthog.assert_not_called()-def _opt_out(monkeypatch) -> tuple[AsyncMock, AsyncMock]:+def _opt_out(monkeypatch) -> AsyncMock:"""The account refused marketing at signup."""lookup = AsyncMock(return_value=SimpleNamespace(@@ -195,19 +246,23 @@ def _opt_out(monkeypatch) -> tuple[AsyncMock, AsyncMock]:marketing_opt_out_at=CREATED,))- providers = AsyncMock(return_value=["credential"])monkeypatch.setattr(checkout_audience, "get_user_by_id", lookup)- monkeypatch.setattr(checkout_audience, "signin_providers", providers)- return lookup, providers+ return lookup@pytest.mark.asyncio-async def test_an_opted_out_opener_is_never_queued(queued, monkeypatch, caplog):- _, providers = _opt_out(monkeypatch)+async def test_an_opted_out_opener_is_never_queued(+ queued, posthog, monkeypatch, caplog+):+ """Only for MailerLite: PostHog is analytics, not marketing."""+ _opt_out(monkeypatch)+ role = AsyncMock(return_value=OnboardingRole(choice="Marketing"))+ monkeypatch.setattr(checkout_audience, "get_onboarding_role", role)with caplog.at_level(logging.DEBUG, logger=consent.__name__):await checkout_audience.queue_checkout_opened("user-1", ip_country="US")queued.assert_not_awaited()- providers.assert_not_awaited()+ role.assert_not_awaited()+ assert _person(posthog)["country_code"] == "US"assert pseudonym(EMAIL) in caplog.textassert EMAIL not in caplog.text@@ -234,7 +289,7 @@ async def test_an_opener_cached_before_the_consent_fields_queues_nothing(async def test_an_opted_out_customers_completed_checkout_queues_nothing(queued, monkeypatch):- lookup, _ = _opt_out(monkeypatch)+ lookup = _opt_out(monkeypatch)with patch("prisma.models.User.prisma",return_value=_user_prisma(SimpleNamespace(id="user-1")),diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.pyindex 44004ffee4..b055e381a3 100644--- a/autogpt_platform/backend/backend/data/user.py+++ b/autogpt_platform/backend/backend/data/user.py@@ -35,6 +35,7 @@ from backend.data.posthog_lifecycle_sync import schedule_posthog_lifecycle_syncfrom backend.data.profile import ensure_profilefrom backend.data.subscription_trial import get_subscription_trialfrom backend.notifications.audience_enrichment import points_at_excluded_country+from backend.notifications.posthog_audience import set_account_propertiesfrom backend.notifications.queue import queue_audience_changefrom backend.notifications.subscriber_fields import audience_eventfrom backend.util.cache import cached@@ -129,9 +130,9 @@ async def _get_or_create_user(user_data: dict) -> UserCreationResult:# new account: the personal org this call bootstraps does.was_created = row_created or org_createdif was_created:- track_signup_completed(- user_id=user.id, signup_method=await _signup_method(user.id, user_data)- )+ signup_method = await _signup_method(user.id, user_data)+ track_signup_completed(user_id=user.id, signup_method=signup_method)+ set_account_properties(user.id, user.email, signup_method)schedule_posthog_lifecycle_sync(user.id)return UserCreationResult(user=User.from_db(user), was_created=was_created)diff --git a/autogpt_platform/backend/backend/data/user_test.py b/autogpt_platform/backend/backend/data/user_test.pyindex 4fea585ff1..5270c326c8 100644--- a/autogpt_platform/backend/backend/data/user_test.py+++ b/autogpt_platform/backend/backend/data/user_test.py@@ -1139,6 +1139,7 @@ class TestGetOrCreateUserStatus:) as posthog_sync,patch.object(user_module, "AuthAccount") as auth_account,patch.object(user_module, "track_signup_completed"),+ patch.object(user_module, "set_account_properties"),):auth_account.prisma.return_value.find_first = AsyncMock(return_value=None)yield ensure_org, posthog_sync@@ -1175,6 +1176,7 @@ class TestGetOrCreateUserStatus:"from_db",return_value=_application_user("user-existing", "[email protected]"),),+ patch.object(user_module, "set_account_properties") as person,):mock_prisma.user.find_unique = AsyncMock(return_value=db_user)@@ -1184,6 +1186,7 @@ class TestGetOrCreateUserStatus:assert result.was_created is Falsemock_prisma.user.create.assert_not_called()+ person.assert_not_called()@pytest.mark.asyncioasync def test_reports_created_for_a_row_the_auth_hook_inserted_bare(@@ -1206,6 +1209,7 @@ class TestGetOrCreateUserStatus:),patch.object(user_module, "AuthAccount") as auth_account,patch.object(user_module, "track_signup_completed") as track,+ patch.object(user_module, "set_account_properties") as person,):mock_prisma.user.find_unique = AsyncMock(return_value=db_user)auth_account.prisma.return_value.find_first = AsyncMock(@@ -1220,6 +1224,8 @@ class TestGetOrCreateUserStatus:mock_prisma.user.create.assert_not_called()posthog_sync.assert_called_once_with("user-hooked")track.assert_called_once_with(user_id="user-hooked", signup_method="email")+ # The email type and sign-in method MailerLite segments on.+ person.assert_called_once_with("user-hooked", "[email protected]", "email")@pytest.mark.asyncioasync def test_a_row_created_concurrently_is_read_back_not_an_error(self):diff --git a/autogpt_platform/backend/backend/notifications/audience_enrichment.py b/autogpt_platform/backend/backend/notifications/audience_enrichment.pyindex 44afb6a18d..efc40916df 100644--- a/autogpt_platform/backend/backend/notifications/audience_enrichment.py+++ b/autogpt_platform/backend/backend/notifications/audience_enrichment.py@@ -258,11 +258,11 @@ def checkout_fields(stripe_country: str | None = None,ip_country: str | None = None,status: str = SubscriptionStatus.SIGNED.value,- role: OnboardingRole | None = None,) -> Fields:- """Every field a checkout opener gets. Merge it with what MailerLite holds- (`merge_with_held`) before writing. Without a `role` picked yet, the role- MailerLite holds is left alone."""+ """Every field a checkout opener gets but the role, which the caller adds+ once there is one (`role_fields`); without it, the role MailerLite holds+ is left alone. Merge it with what MailerLite holds (`merge_with_held`)+ before writing."""tz_country = timezone_country(timezone)candidates = {"stripe": country_code(stripe_country),@@ -282,8 +282,6 @@ def checkout_fields(method = signin_method(signin_providers)if method:fields[SubscriberField.SIGNIN_METHOD] = method- if role:- fields.update(role_fields(role))for source in COUNTRY_SOURCES:code = candidates[source]if code:@@ -294,7 +292,7 @@ def checkout_fields(return fields-_COUNTRY_FIELDS = (+COUNTRY_FIELDS = (SubscriberField.COUNTRY,SubscriberField.COUNTRY_CODE,SubscriberField.COUNTRY_SOURCE,@@ -341,6 +339,6 @@ def merge_with_held(held_rank = _SOURCE_RANK.get(str(held.get(SubscriberField.COUNTRY_SOURCE.value)), 0)ours_rank = _SOURCE_RANK.get(str(merged.get(SubscriberField.COUNTRY_SOURCE)), 0)if held_rank > ours_rank:- for field in _COUNTRY_FIELDS:+ for field in COUNTRY_FIELDS:merged.pop(field, None)return mergeddiff --git a/autogpt_platform/backend/backend/notifications/audience_enrichment_test.py b/autogpt_platform/backend/backend/notifications/audience_enrichment_test.pyindex d863b8c265..45c994a892 100644--- a/autogpt_platform/backend/backend/notifications/audience_enrichment_test.py+++ b/autogpt_platform/backend/backend/notifications/audience_enrichment_test.py@@ -209,13 +209,9 @@ def test_country_comes_from_the_strongest_source(sources, country, source):assert fields[SubscriberField.COUNTRY_SOURCE] == source-def test_a_picked_role_rides_on_the_checkout():- fields = _fields(role=OnboardingRole(choice="HR/People"))- assert fields[SubscriberField.ROLE] == "HR / People"- assert fields[SubscriberField.ROLE_OTHER] is None---def test_without_a_pick_the_role_mailerlite_holds_is_left_alone():+def test_the_role_is_left_to_the_caller():+ """The checkout path adds it once there is a pick (`role_fields`), so+ without one the role MailerLite holds is left alone."""fields = _fields()assert SubscriberField.ROLE not in fieldsassert SubscriberField.ROLE_OTHER not in fieldsdiff --git a/autogpt_platform/backend/backend/notifications/checkout_audience_test.py b/autogpt_platform/backend/backend/notifications/checkout_audience_test.pyindex 523baa2e67..7914fc2aa8 100644--- a/autogpt_platform/backend/backend/notifications/checkout_audience_test.py+++ b/autogpt_platform/backend/backend/notifications/checkout_audience_test.py@@ -75,7 +75,9 @@ class _FakeMailerLite:return _response(200)-def _event(**kwargs):+def _event(role: OnboardingRole | None = None, **kwargs):+ """A checkout opener's change, with the role added as the checkout path+ adds it once there is a pick."""defaults = dict(email=EMAIL,created_at=CREATED,@@ -84,6 +86,8 @@ def _event(**kwargs):timezone="Asia/Kolkata",)fields = checkout_fields(**{**defaults, **kwargs})+ if role:+ fields.update(role_fields(role))return subscriber_fields.audience_event(AudienceAction.CHECKOUT_OPENED, EMAIL, "user-1", fields)diff --git a/autogpt_platform/backend/backend/notifications/posthog_audience.py b/autogpt_platform/backend/backend/notifications/posthog_audience.pynew file mode 100644index 0000000000..ba66f8b426--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/posthog_audience.py@@ -0,0 +1,161 @@+"""The checkout opener's segmentation on the PostHog person, as in MailerLite.++MailerLite holds what `audience_enrichment` works out for a checkout opener,+and GTM segments on it. PostHog gets the same values as person properties+(SECRT-2895), so funnels, retention and experiments split the same way:++- `email_type` and `signin_method`: every account, from signup;+- `checkout_opened_at`, `country`, `country_code`, `country_source` and+ `exclude_de_at`: every checkout opener. The open is an ISO-8601 UTC moment,+ like the lifecycle dates, where MailerLite holds the day.++The status and its dates are the lifecycle sync's+(`data/posthog_lifecycle_sync.py`), and the profile step sets the role, so+neither is sent from here.++PostHog is analytics, not marketing: nobody is left out for refusing marketing+or because a signal places them in Iran or Russia (`consent.py`).++MailerLite's merge (`audience_enrichment.merge_with_held`) reads the subscriber+before each write. PostHog can't be read on the way, so `$set_once`, which only+fills in what the person doesn't hold yet, keeps the same rules:++- the first checkout open stays;+- exclude_de_at "yes" is set and "no" only filled in, so it never goes back to+ "no";+- a Stripe billing country is set, so it always wins, and an IP or timezone+ country is only filled in, so it never replaces one. The one difference:+ PostHog keeps the first IP or timezone country it gets, where MailerLite+ moves on to a later IP country.+"""++import logging+from collections.abc import Iterable+from datetime import datetime++from posthog import Posthog+from pydantic import BaseModel, Field++from backend.data.notifications import SubscriberField+from backend.notifications.audience_enrichment import (+ COUNTRY_FIELDS,+ YES,+ email_type,+ signin_method,+)+from backend.notifications.subscriber_fields import Fields, utc_moment+from backend.util import posthog_client+from backend.util.posthog_events import PostHogEvent++logger = logging.getLogger(__name__)++CHECKOUT_OPENED_AT = "checkout_opened_at"++# Always `$set`: the account's current values, under MailerLite's own keys.+_ALWAYS_SET = (SubscriberField.EMAIL_TYPE, SubscriberField.SIGNIN_METHOD)++# Every MailerLite field is one or the other, which `posthog_audience_test`+# holds to the whole of `SubscriberField`, so a field added there can't skip+# PostHog unnoticed. The checkout open goes as CHECKOUT_OPENED_AT.+MIRRORED = frozenset(+ {+ *_ALWAYS_SET,+ SubscriberField.EXCLUDE_DE_AT,+ *COUNTRY_FIELDS,+ SubscriberField.CHECKOUT_OPENED,+ }+)+# The lifecycle sync sends the status and its dates, and the profile step the+# role.+NOT_MIRRORED = frozenset(+ {+ SubscriberField.STATUS,+ SubscriberField.SIGNUP,+ SubscriberField.TRIAL_STARTED,+ SubscriberField.SUBSCRIPTION_STARTED,+ SubscriberField.SUBSCRIPTION_CANCELED,+ SubscriberField.SUBSCRIPTION_ENDED,+ SubscriberField.ROLE,+ SubscriberField.ROLE_OTHER,+ }+)+++class PersonUpdate(BaseModel):+ """One `$set` event: values that replace the person's, and values that+ only fill in ones the person doesn't hold yet."""++ to_set: dict[str, str] = Field(default_factory=dict)+ to_set_once: dict[str, str] = Field(default_factory=dict)++ def properties(self) -> dict[str, dict[str, str]]:+ return {+ key: values+ for key, values in (("$set", self.to_set), ("$set_once", self.to_set_once))+ if values+ }+++def set_account_properties(user_id: str, email: str, signup_method: str | None) -> None:+ """A new account's email type and sign-in method. Never raises: analytics+ must not fail a signup."""+ try:+ providers = [signup_method] if signup_method else []+ send(user_id, account_update(email, providers))+ except Exception:+ logger.warning(f"Could not set the PostHog account properties of {user_id}")+++def set_checkout_properties(+ user_id: str, fields: Fields, opened_at: datetime | int | float | None+) -> None:+ """A checkout opener's properties (`checkout_update`). Never raises:+ analytics must not cost the opener their MailerLite change."""+ try:+ send(user_id, checkout_update(fields, opened_at))+ except Exception:+ logger.warning(f"Could not set the PostHog checkout properties of {user_id}")+++def account_update(email: str, signin_providers: Iterable[str]) -> PersonUpdate:+ update = PersonUpdate(to_set={SubscriberField.EMAIL_TYPE.value: email_type(email)})+ if method := signin_method(signin_providers):+ update.to_set[SubscriberField.SIGNIN_METHOD.value] = method+ return update+++def checkout_update(+ fields: Fields,+ opened_at: datetime | int | float | None,+ *,+ first_open: bool = False,+) -> PersonUpdate:+ """The person properties for a checkout opener's MailerLite `fields`+ (`audience_enrichment.checkout_fields`), opened at `opened_at`, or now when+ that is None. `first_open` is for the backfill: Stripe holds every+ checkout, so the open it names replaces the one PostHog holds."""+ update = PersonUpdate()+ for field in _ALWAYS_SET:+ if value := fields.get(field):+ update.to_set[field.value] = value+ if exclude := fields.get(SubscriberField.EXCLUDE_DE_AT):+ target = update.to_set if exclude == YES else update.to_set_once+ target[SubscriberField.EXCLUDE_DE_AT.value] = exclude+ country = {f.value: value for f in COUNTRY_FIELDS if (value := fields.get(f))}+ billed = fields.get(SubscriberField.COUNTRY_SOURCE) == "stripe"+ (update.to_set if billed else update.to_set_once).update(country)+ opened = utc_moment(opened_at).isoformat(timespec="seconds")+ (update.to_set if first_open else update.to_set_once)[CHECKOUT_OPENED_AT] = opened+ return update+++def send(user_id: str, update: PersonUpdate, *, client: Posthog | None = None) -> bool:+ """Queue `update` as one `$set` event, on `client` or else the shared+ client. Never raises. False when the client didn't queue it, analytics+ being off included; an update with nothing to set is not sent and counts+ as done."""+ if not (properties := update.properties()):+ return True+ return posthog_client.capture(+ user_id, PostHogEvent.SET_PERSON_PROPERTIES, properties, client=client+ )diff --git a/autogpt_platform/backend/backend/notifications/posthog_audience_backfill.py b/autogpt_platform/backend/backend/notifications/posthog_audience_backfill.pynew file mode 100644index 0000000000..a8043419ed--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/posthog_audience_backfill.py@@ -0,0 +1,140 @@+"""Give accounts from before SECRT-2895 the PostHog person properties the live+code sends since (`posthog_audience`): every account its email type and+sign-in method, and every checkout opener its first open, country and+exclude_de_at.++An opener is an account whose Stripe customer has a Checkout Session, as for+`mailerlite-checkout-backfill`, and is worked out the same way+(`audience_enrichment.checkout_fields`), from the Stripe billing country or+else the browser's timezone. The live code's own rules keep a run from making+PostHog worse: the IP country a live checkout saw was never stored, so a+timezone country is only filled in and never replaces it, and neither does an+exclude_de_at "no". The first open is the exception and is set outright: Stripe+holds every Checkout Session, so its first one is the first open, even where+the live code saw a later one first.++Nobody is left out: PostHog is analytics, not marketing. A repeated run sends+the same values, so it is harmless.+"""++from collections import Counter+from collections.abc import Mapping+from datetime import datetime+from functools import partial++from posthog import Posthog+from pydantic import BaseModel++from backend.data.notifications import SubscriberField+from backend.notifications import posthog_audience+from backend.notifications.audience_enrichment import YES, checkout_fields+from backend.notifications.posthog_audience import CHECKOUT_OPENED_AT, PersonUpdate+from backend.util.posthog_client import send_each++NONE = "none"+++class Account(BaseModel):+ user_id: str+ email: str+ created_at: datetime+ timezone: str | None = None+ stripe_customer_id: str | None = None+++class AccountUpdate(BaseModel):+ user_id: str+ update: PersonUpdate+++class AudiencePlan(BaseModel):+ updates: list[AccountUpdate]+ # Accounts whose Stripe customer has a Checkout Session.+ openers: int+ # What the run sends, for the report. A `$set_once` value only lands where+ # the person holds none yet, so these aren't what PostHog will hold.+ email_types: dict[str, int]+ signin_methods: dict[str, int]+ # Openers only.+ country_sources: dict[str, int]+ exclude_de_at: int+++def plan(+ accounts: list[Account],+ providers: Mapping[str, list[str]],+ first_opens: Mapping[str, int],+ billing_countries: Mapping[str, str],+) -> AudiencePlan:+ """`providers` by account id; `first_opens` (Unix timestamps) and+ `billing_countries` by Stripe customer id."""+ updates = [+ AccountUpdate(+ user_id=account.user_id,+ update=_update(+ account,+ providers.get(account.user_id, []),+ first_opens.get(account.stripe_customer_id or ""),+ billing_countries.get(account.stripe_customer_id or ""),+ ),+ )+ for account in accounts+ ]+ return _tally(updates)+++def _update(+ account: Account,+ providers: list[str],+ first_open: int | None,+ billing_country: str | None,+) -> PersonUpdate:+ if first_open is None:+ return posthog_audience.account_update(account.email, providers)+ fields = checkout_fields(+ email=account.email,+ created_at=account.created_at,+ opened_at=first_open,+ signin_providers=providers,+ timezone=account.timezone,+ stripe_country=billing_country,+ )+ return posthog_audience.checkout_update(fields, first_open, first_open=True)+++def _tally(updates: list[AccountUpdate]) -> AudiencePlan:+ email_types: Counter[str] = Counter()+ signin_methods: Counter[str] = Counter()+ sources: Counter[str] = Counter()+ openers = exclude_de_at = 0+ for change in updates:+ values = {**change.update.to_set_once, **change.update.to_set}+ email_types[values.get(SubscriberField.EMAIL_TYPE.value, NONE)] += 1+ signin_methods[values.get(SubscriberField.SIGNIN_METHOD.value, NONE)] += 1+ if CHECKOUT_OPENED_AT not in values:+ continue+ openers += 1+ sources[values.get(SubscriberField.COUNTRY_SOURCE.value, NONE)] += 1+ if values.get(SubscriberField.EXCLUDE_DE_AT.value) == YES:+ exclude_de_at += 1+ return AudiencePlan(+ updates=updates,+ openers=openers,+ email_types=dict(email_types),+ signin_methods=dict(signin_methods),+ country_sources=dict(sources),+ exclude_de_at=exclude_de_at,+ )+++async def send(updates: list[AccountUpdate]) -> int:+ """One `$set` event per account. Returns how many accounts PostHog didn't+ take (`posthog_client.send_each`). The run sends the same values every+ time, so retrying is harmless."""+ return await send_each(+ (change.user_id, partial(_send_update, change)) for change in updates+ )+++def _send_update(change: AccountUpdate, client: Posthog) -> bool:+ return posthog_audience.send(change.user_id, change.update, client=client)diff --git a/autogpt_platform/backend/backend/notifications/posthog_audience_backfill_test.py b/autogpt_platform/backend/backend/notifications/posthog_audience_backfill_test.pynew file mode 100644index 0000000000..52d9a786c8--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/posthog_audience_backfill_test.py@@ -0,0 +1,169 @@+"""The PostHog backfill gives every account its email type and sign-in method,+and every checkout opener what the live checkout sends, by the same rules,+except that Stripe's first open is set outright."""++from collections.abc import Iterator+from datetime import UTC, datetime+from unittest.mock import Mock++import pytest++from backend.notifications import posthog_audience+from backend.notifications import posthog_audience_backfill as backfill+from backend.notifications.audience_enrichment import checkout_fields+from backend.notifications.posthog_audience import CHECKOUT_OPENED_AT, PersonUpdate+from backend.notifications.posthog_audience_backfill import Account, AccountUpdate+from backend.util import posthog_client++CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+OPENED = 1788305400+OPENED_ISO = "2026-09-01T23:30:00+00:00"++ACCOUNTS = [+ Account(+ user_id="billed",+ email="[email protected]",+ created_at=CREATED,+ timezone="America/Chicago",+ stripe_customer_id="cus_1",+ ),+ Account(+ user_id="opened-only",+ email="[email protected]",+ created_at=CREATED,+ timezone="Europe/Vienna",+ stripe_customer_id="cus_2",+ ),+ Account(+ user_id="customer-without-session",+ email="[email protected]",+ created_at=CREATED,+ stripe_customer_id="cus_3",+ ),+ Account(+ user_id="never-a-customer",+ email="[email protected]",+ created_at=CREATED,+ timezone="Europe/Moscow",+ ),+]+PROVIDERS = {"billed": ["google", "credential"], "opened-only": ["credential"]}+FIRST_OPENS = {"cus_1": OPENED, "cus_2": OPENED + 60}+BILLING = {"cus_1": "US", "cus_3": "FR"}+++def _plan():+ return backfill.plan(ACCOUNTS, PROVIDERS, FIRST_OPENS, BILLING)+++def test_every_account_is_planned_and_only_openers_get_the_checkout_fields():+ plan = _plan()+ updates = {change.user_id: change.update for change in plan.updates}+ assert len(updates) == 4+ assert plan.openers == 2++ billed = updates["billed"]+ assert billed.to_set["country_code"] == "US"+ assert billed.to_set["country_source"] == "stripe"+ assert billed.to_set[CHECKOUT_OPENED_AT] == OPENED_ISO+ assert billed.to_set_once == {"exclude_de_at": "no"}++ opened_only = updates["opened-only"]+ assert opened_only.to_set_once["country_code"] == "AT"+ assert opened_only.to_set_once["country_source"] == "timezone"+ assert opened_only.to_set["exclude_de_at"] == "yes"++ # A Stripe customer is not an opener without a Checkout Session.+ assert updates["customer-without-session"].to_set == {"email_type": "education"}+ assert updates["customer-without-session"].to_set_once == {}+ # A signal in Russia keeps an account out of MailerLite, not PostHog.+ assert updates["never-a-customer"].to_set == {"email_type": "business"}+++def test_the_plan_counts_what_the_accounts_end_up_with():+ plan = _plan()+ assert plan.email_types == {"business": 2, "personal": 1, "education": 1}+ assert plan.signin_methods == {"google": 1, "email": 1, "none": 2}+ assert plan.country_sources == {"stripe": 1, "timezone": 1}+ assert plan.exclude_de_at == 1+++def test_an_opener_gets_what_the_live_checkout_sends():+ """The same values by the same rules; only the first open is set+ outright, since Stripe holds every checkout."""+ account = ACCOUNTS[0]+ live = posthog_audience.checkout_update(+ checkout_fields(+ email=account.email,+ created_at=account.created_at,+ opened_at=OPENED,+ signin_providers=PROVIDERS["billed"],+ timezone=account.timezone,+ stripe_country="US",+ ),+ OPENED,+ )+ planned = next(c.update for c in _plan().updates if c.user_id == "billed")+ assert planned.to_set.pop(CHECKOUT_OPENED_AT) == OPENED_ISO+ assert live.to_set_once.pop(CHECKOUT_OPENED_AT) == OPENED_ISO+ assert planned == live+++def client(monkeypatch: pytest.MonkeyPatch) -> Iterator[Mock]:+ """The run's own PostHog client (`posthog_client.send_each`), never the+ shared one. `client.on_error` is the callback the run gave it."""+ client = Mock()+ shared = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: shared)++ def new_client(**options) -> Mock:+ client.on_error = options["on_error"]+ return client++ monkeypatch.setattr(posthog_client, "new_posthog_client", new_client)+ yield client+ shared.capture.assert_not_called()+++async def test_each_account_is_one_update_on_the_runs_own_client(client: Mock):+ updates = [+ AccountUpdate(user_id="1", update=PersonUpdate(to_set={"email_type": "x"})),+ AccountUpdate(user_id="2", update=PersonUpdate(to_set_once={"country": "y"})),+ ]+ assert await backfill.send(updates) == 0+ sent = [+ (c.kwargs["distinct_id"], c.kwargs["event"], c.kwargs["properties"])+ for c in client.capture.call_args_list+ ]+ assert [(distinct_id, event) for distinct_id, event, _ in sent] == [+ ("1", "$set"),+ ("2", "$set"),+ ]+ assert sent[0][2]["$set"] == {"email_type": "x"}+ assert sent[1][2]["$set_once"] == {"country": "y"}+ client.shutdown.assert_called_once()+++async def test_what_posthog_did_not_take_is_returned(client: Mock):+ """Refused when queued, or in an upload that failed for good."""+ client.capture.side_effect = lambda **event: (+ None if event["distinct_id"] == "0" else "event-uuid"+ )+ client.shutdown.side_effect = lambda: client.on_error(+ RuntimeError("503"), [{"distinct_id": "2"}]+ )+ updates = [+ AccountUpdate(user_id=str(i), update=PersonUpdate(to_set={"email_type": "x"}))+ for i in range(3)+ ]+ assert await backfill.send(updates) == 2+++async def test_sending_without_posthog_fails_before_sending_anything(monkeypatch):+ monkeypatch.setattr(posthog_client, "new_posthog_client", lambda **_: None)+ with pytest.raises(RuntimeError, match="PostHog is not configured"):+ await backfill.send(_plan().updates)diff --git a/autogpt_platform/backend/backend/notifications/posthog_audience_test.py b/autogpt_platform/backend/backend/notifications/posthog_audience_test.pynew file mode 100644index 0000000000..206cd08c5e--- /dev/null+++ b/autogpt_platform/backend/backend/notifications/posthog_audience_test.py@@ -0,0 +1,317 @@+"""A checkout opener's MailerLite segmentation reaches the PostHog person with+the same values, and `$set`/`$set_once` keep MailerLite's merge rules without+reading the person first."""++from datetime import UTC, datetime, timedelta, timezone+from unittest.mock import Mock++import pytest++from backend.data.notifications import SubscriberField+from backend.data.onboarding_role import OnboardingRole+from backend.notifications import posthog_audience+from backend.notifications.audience_enrichment import (+ checkout_fields,+ merge_with_held,+ role_fields,+)+from backend.notifications.posthog_audience import (+ CHECKOUT_OPENED_AT,+ MIRRORED,+ NOT_MIRRORED,+ PersonUpdate,+)+from backend.util import posthog_client++CREATED = datetime(2026, 7, 14, 9, 0, tzinfo=UTC)+OPENED = 1788305400+OPENED_ISO = "2026-09-01T23:30:00+00:00"+COUNTRY_KEYS = {"country", "country_code", "country_source"}+# Mirrored under MailerLite's own key and value; the open goes as a moment.+SHARED = MIRRORED - {SubscriberField.CHECKOUT_OPENED}+++def capture(monkeypatch: pytest.MonkeyPatch) -> Mock:+ client = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)+ return client.capture+++def _fields(**overrides):+ return checkout_fields(+ **{+ "email": "[email protected]",+ "created_at": CREATED,+ "opened_at": OPENED,+ "signin_providers": ["credential"],+ "timezone": "America/Chicago",+ **overrides,+ }+ )+++def _sent(update: PersonUpdate) -> dict[str, str]:+ return {**update.to_set_once, **update.to_set}+++def _person(*updates: PersonUpdate) -> dict[str, str]:+ """What PostHog holds after `updates`, in order: `$set` replaces, and+ `$set_once` only fills in what the person doesn't hold yet."""+ person: dict[str, str] = {}+ for update in updates:+ for key, value in update.to_set_once.items():+ person.setdefault(key, value)+ person.update(update.to_set)+ return person+++# ── every account ────────────────────────────────────────────────────────────+++ "email, providers, expected",+ [+ (+ "[email protected]",+ ["credential"],+ {"email_type": "business", "signin_method": "email"},+ ),+ (+ "[email protected]",+ ["credential", "google"],+ {"email_type": "personal", "signin_method": "google"},+ ),+ ("[email protected]", [], {"email_type": "education"}),+ ],+)+def test_an_account_gets_its_email_type_and_sign_in_method(email, providers, expected):+ update = posthog_audience.account_update(email, providers)+ assert update.to_set == expected+ assert update.to_set_once == {}+++ "signup_method, expected", [("email", "email"), ("google", "google"), (None, None)]+)+def test_a_new_account_is_sent_from_its_signup_method(+ capture: Mock, signup_method, expected+):+ posthog_audience.set_account_properties("user-1", "[email protected]", signup_method)+ kwargs = capture.call_args.kwargs+ assert kwargs["distinct_id"] == "user-1"+ assert kwargs["event"] == "$set"+ person = kwargs["properties"]["$set"]+ assert person["email_type"] == "business"+ assert person.get("signin_method") == expected+++# ── checkout openers ─────────────────────────────────────────────────────────+++def test_every_mailerlite_field_is_mirrored_or_left_to_another_sender():+ """A field added to SubscriberField fails this until it is placed, so it+ can't skip PostHog unnoticed."""+ assert MIRRORED | NOT_MIRRORED == set(SubscriberField)+ assert not MIRRORED & NOT_MIRRORED+++def test_an_opener_gets_every_mirrored_field_and_nothing_else():+ """With every signal a checkout can carry, and the role on top: the+ mirrored fields arrive with MailerLite's values under its keys, the open as+ a moment rather than MailerLite's day, and nothing another sender owns."""+ fields = {+ **_fields(stripe_country="AT", ip_country="US", signin_providers=["google"]),+ **role_fields(OnboardingRole(choice="Other", other="Dentist")),+ }+ person = _person(posthog_audience.checkout_update(fields, OPENED))+ for field in SHARED:+ assert person[field.value] == fields[field]+ assert person[CHECKOUT_OPENED_AT] == OPENED_ISO+ assert person.keys() == {f.value for f in SHARED} | {CHECKOUT_OPENED_AT}+++ "countries, source, replaces",+ [+ (dict(stripe_country="DE", ip_country="US"), "stripe", True),+ (dict(ip_country="DE"), "ip", False),+ ({}, "timezone", False),+ ],+)+def test_only_a_billing_country_replaces_the_one_held(countries, source, replaces):+ update = posthog_audience.checkout_update(_fields(**countries), OPENED)+ held, other = (+ (update.to_set, update.to_set_once)+ if replaces+ else (update.to_set_once, update.to_set)+ )+ assert held["country_source"] == source+ assert COUNTRY_KEYS <= held.keys()+ assert not COUNTRY_KEYS & other.keys()+++def test_an_opener_with_no_country_signal_gets_no_country():+ sent = _sent(posthog_audience.checkout_update(_fields(timezone="UTC"), OPENED))+ assert not COUNTRY_KEYS & sent.keys()+++ "countries, value, replaces",+ [(dict(ip_country="AT"), "yes", True), (dict(ip_country="US"), "no", False)],+)+def test_exclude_de_at_yes_replaces_and_no_only_fills_in(countries, value, replaces):+ update = posthog_audience.checkout_update(_fields(**countries), OPENED)+ held = update.to_set if replaces else update.to_set_once+ assert held["exclude_de_at"] == value+++ "opened_at",+ [+ OPENED,+ datetime(2026, 9, 1, 23, 30),+ datetime(2026, 9, 2, 1, 30, tzinfo=timezone(timedelta(hours=2))),+ ],+ ids=["stripe-timestamp", "naive-utc", "other-offset"],+)+def test_the_open_is_an_iso_utc_moment_that_only_fills_in(opened_at):+ update = posthog_audience.checkout_update(_fields(), opened_at)+ assert update.to_set_once[CHECKOUT_OPENED_AT] == OPENED_ISO+ assert CHECKOUT_OPENED_AT not in update.to_set+++def test_an_open_without_a_timestamp_of_its_own_is_now():+ before = datetime.now(UTC).replace(microsecond=0)+ update = posthog_audience.checkout_update(_fields(opened_at=None), None)+ opened = datetime.fromisoformat(update.to_set_once[CHECKOUT_OPENED_AT])+ assert before <= opened <= datetime.now(UTC)+++def test_the_backfills_first_open_replaces_the_one_held():+ """Stripe holds every checkout, so its first is the first open, even+ where the live code saw a later one first."""+ live = posthog_audience.checkout_update(_fields(), OPENED + 86400)+ backfill = posthog_audience.checkout_update(_fields(), OPENED, first_open=True)+ assert backfill.to_set[CHECKOUT_OPENED_AT] == OPENED_ISO+ assert _person(live, backfill)[CHECKOUT_OPENED_AT] == OPENED_ISO+++def test_a_run_of_checkouts_ends_where_mailerlite_does():+ """Opened from Austria, completed billed to the US, opened again from+ France: MailerLite keeps the billing country, the Austrian signal and the+ first open, and so does PostHog, without reading the person."""+ checkouts = [+ (OPENED, dict(ip_country="AT")),+ (OPENED + 600, dict(stripe_country="US")),+ (OPENED + 86400, dict(ip_country="FR")),+ ]+ held: dict[str, str | None] = {}+ updates = []+ for opened_at, countries in checkouts:+ fields = _fields(opened_at=opened_at, **countries)+ merged = merge_with_held(fields, held)+ held.update({field.value: value for field, value in merged.items()})+ updates.append(posthog_audience.checkout_update(fields, opened_at))+ person = _person(*updates)+ for field in SHARED:+ assert person[field.value] == held[field.value]+ assert person["country_source"] == "stripe"+ assert person["exclude_de_at"] == "yes"+ assert person[CHECKOUT_OPENED_AT] == OPENED_ISO+++def test_posthog_keeps_the_first_ip_country_where_mailerlite_moves_on():+ """The one documented difference: PostHog can't read what it holds, so a+ later IP country doesn't replace an earlier IP or timezone one."""+ first = posthog_audience.checkout_update(_fields(ip_country="US"), OPENED)+ later = posthog_audience.checkout_update(_fields(ip_country="CA"), OPENED + 60)+ assert _person(first, later)["country_code"] == "US"+++# ── sending ──────────────────────────────────────────────────────────────────+++def test_an_update_is_one_set_event(capture: Mock):+ assert posthog_audience.send(+ "user-1",+ PersonUpdate(+ to_set={"email_type": "business"}, to_set_once={"exclude_de_at": "no"}+ ),+ )+ capture.assert_called_once()+ kwargs = capture.call_args.kwargs+ assert kwargs["distinct_id"] == "user-1"+ assert kwargs["event"] == "$set"+ assert kwargs["properties"]["$set"] == {"email_type": "business"}+ assert kwargs["properties"]["$set_once"] == {"exclude_de_at": "no"}+++def test_only_the_parts_with_values_are_sent(capture: Mock):+ posthog_audience.send("user-1", PersonUpdate(to_set={"email_type": "business"}))+ assert "$set_once" not in capture.call_args.kwargs["properties"]+++def test_an_empty_update_sends_nothing_and_counts_as_done(capture: Mock):+ assert posthog_audience.send("user-1", PersonUpdate())+ capture.assert_not_called()+++ "returned, side_effect",+ [(None, None), (None, RuntimeError("posthog down"))],+ ids=["dropped", "raised"],+)+def test_an_update_the_client_did_not_queue_is_reported_not_raised(+ capture: Mock, returned, side_effect+):+ capture.return_value = returned+ capture.side_effect = side_effect+ update = PersonUpdate(to_set={"email_type": "business"})+ assert posthog_audience.send("user-1", update) is False+++def test_an_update_goes_to_the_client_it_is_given(capture: Mock):+ own = Mock()+ update = PersonUpdate(to_set={"email_type": "business"})+ assert posthog_audience.send("user-1", update, client=own)+ own.capture.assert_called_once()+ capture.assert_not_called()+++def test_a_checkout_opener_is_sent_their_checkout_update(capture: Mock):+ fields = _fields(ip_country="AT")+ posthog_audience.set_checkout_properties("user-1", fields, OPENED)+ properties = capture.call_args.kwargs["properties"]+ expected = posthog_audience.checkout_update(fields, OPENED)+ assert properties["$set"] == expected.to_set+ assert properties["$set_once"] == expected.to_set_once+++ "broken, call",+ [+ (+ "account_update",+ lambda: posthog_audience.set_account_properties(+ "user-1", "[email protected]", "email"+ ),+ ),+ (+ "checkout_update",+ lambda: posthog_audience.set_checkout_properties(+ "user-1", _fields(), OPENED+ ),+ ),+ ],+ ids=["signup", "checkout"],+)+def test_a_failure_is_logged_and_never_reaches_the_caller(+ capture: Mock, monkeypatch, caplog, broken, call+):+ """Analytics must not fail a signup or cost an opener their MailerLite+ change."""+ monkeypatch.setattr(posthog_audience, broken, Mock(side_effect=RuntimeError()))+ call()+ capture.assert_not_called()+ assert "user-1" in caplog.textdiff --git a/autogpt_platform/backend/backend/notifications/role_backfill.py b/autogpt_platform/backend/backend/notifications/role_backfill.pyindex e4668b18d4..30d42612fb 100644--- a/autogpt_platform/backend/backend/notifications/role_backfill.py+++ b/autogpt_platform/backend/backend/notifications/role_backfill.py@@ -25,7 +25,9 @@ import loggingfrom collections import Counterfrom collections.abc import Callablefrom datetime import datetime+from functools import partial+from posthog import Posthogfrom pydantic import BaseModelfrom backend.data.notifications import SubscriberField@@ -44,7 +46,7 @@ from backend.notifications.mailerlite import ()from backend.notifications.mailerlite_backfill import BatchAnswer, _refusalfrom backend.notifications.mailerlite_field_backfill import Current-from backend.util.posthog_client import get_posthog_client+from backend.util.posthog_client import send_eachfrom backend.util.product_analytics import set_onboarding_rolelogger = logging.getLogger(__name__)@@ -52,8 +54,6 @@ logger = logging.getLogger(__name__)# One person every two seconds, a read and a write each: half MailerLite's# 120 calls a minute, so the live consumer keeps the rest.WRITE_INTERVAL_SECONDS = 2.0-# The PostHog client drops events once its in-memory queue is full.-POSTHOG_FLUSH_EVERY = 500class RoleRecord(BaseModel):@@ -166,18 +166,22 @@ def _plan_mailerlite(result.mailerlite.append(assignment)-async def send_to_posthog(assignments: list[RoleAssignment]) -> None:- """A `$set_once` per person, flushed as it goes."""- client = get_posthog_client()- if client is None:- raise RuntimeError("PostHog is not configured")- for index, assignment in enumerate(assignments, start=1):- set_onboarding_role(- user_id=assignment.user_id, role=assignment.role, keep_existing=True- )- if index % POSTHOG_FLUSH_EVERY == 0:- await asyncio.to_thread(client.flush)- await asyncio.to_thread(client.flush)+async def send_to_posthog(assignments: list[RoleAssignment]) -> int:+ """A `$set_once` per person. Returns how many PostHog didn't take+ (`posthog_client.send_each`); a rerun sends them again."""+ return await send_each(+ (assignment.user_id, partial(_send_role, assignment))+ for assignment in assignments+ )+++def _send_role(assignment: RoleAssignment, client: Posthog) -> bool:+ return set_onboarding_role(+ user_id=assignment.user_id,+ role=assignment.role,+ keep_existing=True,+ client=client,+ )async def apply(diff --git a/autogpt_platform/backend/backend/notifications/role_backfill_test.py b/autogpt_platform/backend/backend/notifications/role_backfill_test.pyindex 3a3e9e32ff..e483253ef4 100644--- a/autogpt_platform/backend/backend/notifications/role_backfill_test.py+++ b/autogpt_platform/backend/backend/notifications/role_backfill_test.py@@ -12,6 +12,7 @@ from backend.data.onboarding_role import OnboardingRolefrom backend.notifications import role_backfillfrom backend.notifications.mailerlite import pseudonymfrom backend.notifications.role_backfill import RoleAssignment, RoleRecord+from backend.util import posthog_clientOPTED_OUT = datetime(2026, 10, 2, tzinfo=UTC)@@ -154,24 +155,69 @@ async def test_a_failure_is_counted_by_pseudonym_and_the_run_goes_on(assert "[email protected]" not in caplog.text-async def test_posthog_gets_a_set_once_per_person_and_a_flush(monkeypatch):+def posthog_run(monkeypatch):+ """The run's own PostHog client (`posthog_client.send_each`), never the+ shared one. `client.on_error` is the callback the run gave it."""client = MagicMock()- monkeypatch.setattr(role_backfill, "get_posthog_client", lambda: client)- sent = MagicMock()- monkeypatch.setattr(role_backfill, "set_onboarding_role", sent)- monkeypatch.setattr(role_backfill, "POSTHOG_FLUSH_EVERY", 2)+ shared = MagicMock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: shared)++ def new_client(**options):+ client.on_error = options["on_error"]+ return client++ monkeypatch.setattr(posthog_client, "new_posthog_client", new_client)+ yield client+ shared.capture.assert_not_called()+++async def test_posthog_gets_a_set_once_per_person_on_the_runs_own_client(+ posthog_run,+):+ assigned = [+ RoleAssignment(user_id=f"u-{n}", email=f"{n}@x.io", role=role)+ for n, role in enumerate(+ [OnboardingRole(choice="Marketing"), OnboardingRole(choice="Engineering")]+ )+ ]- await role_backfill.send_to_posthog([_assignment() for _ in range(3)])+ assert await role_backfill.send_to_posthog(assigned) == 0++ sent = [c.kwargs for c in posthog_run.capture.call_args_list]+ assert [s["distinct_id"] for s in sent] == ["u-0", "u-1"]+ assert [s["properties"]["$set_once"]["onboarding_role"] for s in sent] == [+ "Marketing",+ "Engineering",+ ]+ assert all("$set" not in s["properties"] for s in sent)+ posthog_run.shutdown.assert_called_once()+++async def test_what_posthog_did_not_take_is_returned(posthog_run):+ """Refused when queued, or in an upload that failed for good, so the CLI+ can fail the run instead of reporting everyone as sent."""+ posthog_run.capture.side_effect = lambda **event: (+ None if event["distinct_id"] == "u-0" else "event-uuid"+ )+ posthog_run.shutdown.side_effect = lambda: posthog_run.on_error(+ RuntimeError("503"), [{"distinct_id": "u-2"}]+ )+ assigned = [+ RoleAssignment(+ user_id=f"u-{n}", email=f"{n}@x.io", role=OnboardingRole(choice="Marketing")+ )+ for n in range(3)+ ]- assert sent.call_count == 3- assert all(call.kwargs["keep_existing"] for call in sent.call_args_list)- assert client.flush.call_count == 2+ assert await role_backfill.send_to_posthog(assigned) == 2@pytest.mark.asyncioasync def test_posthog_must_be_configured(monkeypatch):- monkeypatch.setattr(role_backfill, "get_posthog_client", lambda: None)+ monkeypatch.setattr(posthog_client, "new_posthog_client", lambda **_: None)with pytest.raises(RuntimeError, match="PostHog is not configured"):await role_backfill.send_to_posthog([_assignment()])diff --git a/autogpt_platform/backend/backend/notifications/subscriber_fields.py b/autogpt_platform/backend/backend/notifications/subscriber_fields.pyindex 840683bbf5..49d98c6410 100644--- a/autogpt_platform/backend/backend/notifications/subscriber_fields.py+++ b/autogpt_platform/backend/backend/notifications/subscriber_fields.py@@ -33,15 +33,20 @@ Fields = dict[SubscriberField, str | None]def mailerlite_date(value: datetime | int | float | None) -> str:- """A Stripe timestamp or a datetime as a MailerLite date, in UTC. Only an- event without a timestamp of its own falls back to now."""+ """A Stripe timestamp or a datetime as a MailerLite date, in UTC."""+ return utc_moment(value).strftime("%Y-%m-%d")+++def utc_moment(value: datetime | int | float | None) -> datetime:+ """A Stripe timestamp or a datetime, in UTC. Only an event without a+ timestamp of its own falls back to now."""if value is None:moment = datetime.now(UTC)elif isinstance(value, datetime):moment = value if value.tzinfo else value.replace(tzinfo=UTC)else:moment = datetime.fromtimestamp(value, UTC)- return moment.astimezone(UTC).strftime("%Y-%m-%d")+ return moment.astimezone(UTC)def signed(created_at: datetime) -> Fields:diff --git a/autogpt_platform/backend/backend/util/posthog_client.py b/autogpt_platform/backend/backend/util/posthog_client.pyindex acc59c3713..4cebbaf15e 100644--- a/autogpt_platform/backend/backend/util/posthog_client.py+++ b/autogpt_platform/backend/backend/util/posthog_client.py@@ -8,9 +8,10 @@ treats a missing client as "analytics disabled" and never lets trackingraise into the request or execution that produced the event."""+import asyncioimport atexitimport logging-from collections.abc import Mapping+from collections.abc import Callable, Iterable, Mappingfrom functools import cachefrom threading import Lockfrom typing import Any@@ -37,13 +38,8 @@ def get_posthog_client() -> Posthog | None:return _clienttry:- settings = Settings()- if settings.secrets.posthog_api_key:- _client = Posthog(- settings.secrets.posthog_api_key,- host=settings.secrets.posthog_host,- )- else:+ _client = new_posthog_client()+ if _client is None:logger.debug("PostHog API key not configured, analytics disabled")except Exception:logger.warning("Failed to initialize PostHog analytics", exc_info=True)@@ -52,6 +48,53 @@ def get_posthog_client() -> Posthog | None:return _client+def new_posthog_client(**options: Any) -> Posthog | None:+ """A client of the caller's own, for a one-off run that needs `options`+ the shared one doesn't have, such as ``on_error`` to learn which uploads+ failed. None when analytics is off. The caller shuts it down."""+ settings = Settings()+ if not settings.secrets.posthog_api_key:+ return None+ return Posthog(+ settings.secrets.posthog_api_key, host=settings.secrets.posthog_host, **options+ )+++# The client drops events once its in-memory queue is full.+FLUSH_EVERY = 500++# One event to send: the distinct id it is for, and the call that queues it on+# the client it is given, saying whether the client took it.+Send = tuple[str, Callable[[Posthog], bool]]+++async def send_each(sends: Iterable[Send]) -> int:+ """Queue `sends` on a client of the run's own, flushing as it goes and+ shutting it down at the end, for a one-off run such as a backfill. Returns+ how many distinct ids PostHog didn't take: refused when queued, or in a+ batch whose upload still failed after the client's retries.++ Raises RuntimeError when analytics is off."""+ failed: set[str] = set()++ def upload_failed(_error: Exception, batch: list[dict]) -> None:+ failed.update(str(event.get("distinct_id")) for event in batch)++ client = new_posthog_client(on_error=upload_failed)+ if client is None:+ raise RuntimeError("PostHog is not configured")+ try:+ for index, (distinct_id, send) in enumerate(sends, start=1):+ if not send(client):+ failed.add(distinct_id)+ if index % FLUSH_EVERY == 0:+ await asyncio.to_thread(client.flush)+ finally:+ # Flushes what is left; failed uploads reach `upload_failed` first.+ await asyncio.to_thread(client.shutdown)+ return len(failed)++@cachedef _environment() -> str:return Settings().config.app_env.value@@ -64,8 +107,11 @@ def capture(*,source: str = "platform",dedup_key: str | None = None,-) -> None:- """Send one event for *distinct_id* with the base properties.+ client: Posthog | None = None,+) -> bool:+ """Send one event for *distinct_id* with the base properties, on+ ``client`` (from `new_posthog_client`) or else the shared client. True+ when the client queued it, which is not yet delivery.No user means no event: a synthetic distinct id would create a personnobody can merge. The base properties are applied last, so a caller can@@ -77,13 +123,14 @@ def capture(which the client otherwise randomises per call."""if not distinct_id:- return+ return Falseevent_name = "<unknown>"try:event_name = event.value- client = get_posthog_client()if client is None:- return+ client = get_posthog_client()+ if client is None:+ return Falsepayload: dict[str, Any] = {**(properties or {}),"environment": _environment(),@@ -95,12 +142,15 @@ def capture(event_uuid = str(uuid5(NAMESPACE_URL, f"{distinct_id}:{event_name}:{dedup_key}"))- client.capture(+ # The client returns None for an event it dropped, such as on a full+ # queue.+ queued = client.capture(distinct_id=distinct_id,event=event_name,properties=payload,uuid=event_uuid,)+ return queued is not Noneexcept Exception:logger.warning("Failed to send PostHog event %s for %s",@@ -108,6 +158,7 @@ def capture(distinct_id,exc_info=True,)+ return Falsedef _shutdown() -> None:diff --git a/autogpt_platform/backend/backend/util/posthog_client_test.py b/autogpt_platform/backend/backend/util/posthog_client_test.pyindex 75c9544dbe..38b7b22969 100644--- a/autogpt_platform/backend/backend/util/posthog_client_test.py+++ b/autogpt_platform/backend/backend/util/posthog_client_test.py@@ -181,6 +181,158 @@ def test_capture_never_raises_on_a_bad_event(monkeypatch: pytest.MonkeyPatch) ->client.capture.assert_not_called()+ "returned, side_effect, queued",+ [+ ("event-uuid", None, True),+ # The client returns None for an event it dropped, such as on a full+ # queue.+ (None, None, False),+ (None, RuntimeError("posthog down"), False),+ ],+ ids=["queued", "dropped", "raised"],+)+def test_capture_says_whether_the_client_queued_the_event(+ monkeypatch: pytest.MonkeyPatch, returned, side_effect, queued: bool+) -> None:+ client = Mock()+ client.capture.return_value = returned+ client.capture.side_effect = side_effect+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: client)++ assert posthog_client.capture("user-1", PostHogEvent.CHAT_TOOL_CALLED) is queued+++def test_capture_queues_nothing_without_a_user_or_a_client(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: None)++ assert posthog_client.capture("user-1", PostHogEvent.CHAT_TOOL_CALLED) is False+ assert posthog_client.capture(None, PostHogEvent.CHAT_TOOL_CALLED) is False+++def test_capture_on_a_client_of_its_own_leaves_the_shared_one_alone(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ shared = Mock()+ own = Mock()+ monkeypatch.setattr(posthog_client, "get_posthog_client", lambda: shared)++ assert posthog_client.capture("user-1", PostHogEvent.CHAT_TOOL_CALLED, client=own)++ own.capture.assert_called_once()+ shared.capture.assert_not_called()+++def test_a_client_of_its_own_gets_the_options_the_shared_one_lacks(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ settings = Mock()+ settings.secrets.posthog_api_key = "test-key"+ settings.secrets.posthog_host = "https://example.com"+ factory = Mock()+ on_error = Mock()+ monkeypatch.setattr(posthog_client, "Settings", Mock(return_value=settings))+ monkeypatch.setattr(posthog_client, "Posthog", factory)++ assert posthog_client.new_posthog_client(on_error=on_error) is factory.return_value+ factory.assert_called_once_with(+ "test-key", host="https://example.com", on_error=on_error+ )+ # The shared client is not created or replaced on the way.+ assert posthog_client._client is None+++def test_no_client_of_its_own_when_analytics_is_off(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ settings = Mock()+ settings.secrets.posthog_api_key = ""+ factory = Mock()+ monkeypatch.setattr(posthog_client, "Settings", Mock(return_value=settings))+ monkeypatch.setattr(posthog_client, "Posthog", factory)++ assert posthog_client.new_posthog_client(on_error=Mock()) is None+ factory.assert_not_called()+++def own_client(monkeypatch: pytest.MonkeyPatch) -> Mock:+ """The run's own client. `own_client.on_error` is the callback the run+ gave it for uploads that failed for good."""+ client = Mock()++ def new_client(**options: object) -> Mock:+ client.on_error = options["on_error"]+ return client++ monkeypatch.setattr(posthog_client, "new_posthog_client", new_client)+ return client+++def _sends(+ count: int, refused: frozenset[str] = frozenset()+) -> list[posthog_client.Send]:+ """One send per distinct id "0".."count-1"; those in `refused` report the+ client didn't take them."""+ return [+ (str(i), lambda client, i=str(i): client.capture(i) and i not in refused)+ for i in range(count)+ ]+++async def test_send_each_sends_all_flushing_as_it_goes_then_shuts_down(+ own_client: Mock, monkeypatch: pytest.MonkeyPatch+) -> None:+ monkeypatch.setattr(posthog_client, "FLUSH_EVERY", 2)++ assert await posthog_client.send_each(_sends(5)) == 0++ assert [c.args[0] for c in own_client.capture.call_args_list] == list("01234")+ # After the second and the fourth; shutting down flushes the rest.+ assert own_client.flush.call_count == 2+ own_client.shutdown.assert_called_once()+++async def test_send_each_counts_each_distinct_id_posthog_did_not_take_once(+ own_client: Mock,+) -> None:+ """Refused when queued, or in an upload that still failed after the+ client's retries, which reaches `on_error` by the end of the shutdown."""+ own_client.shutdown.side_effect = lambda: own_client.on_error(+ RuntimeError("503"), [{"distinct_id": "1"}, {"distinct_id": "3"}]+ )++ assert await posthog_client.send_each(_sends(4, frozenset({"1", "2"}))) == 3+++async def test_send_each_shuts_the_client_down_even_when_a_send_raises(+ own_client: Mock,+) -> None:+ def broken(_client: object) -> bool:+ raise RuntimeError("bug")++ with pytest.raises(RuntimeError, match="bug"):+ await posthog_client.send_each([("0", broken)])+ own_client.shutdown.assert_called_once()+++async def test_send_each_without_posthog_raises_before_sending(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(posthog_client, "new_posthog_client", lambda **_: None)+ send = Mock()++ with pytest.raises(RuntimeError, match="PostHog is not configured"):+ await posthog_client.send_each([("0", send)])+ send.assert_not_called()++def test_capture_dedup_key_is_insert_id_and_a_stable_uuid(monkeypatch: pytest.MonkeyPatch,) -> None:diff --git a/autogpt_platform/backend/backend/util/posthog_events.py b/autogpt_platform/backend/backend/util/posthog_events.pyindex df43b29c84..01ab56099c 100644--- a/autogpt_platform/backend/backend/util/posthog_events.py+++ b/autogpt_platform/backend/backend/util/posthog_events.py@@ -62,8 +62,10 @@ class PostHogEvent(StrEnum):TRIAL_CONVERTED = "trial_converted"PAYMENT_FAILED = "payment_failed"- # Person properties: backend/data/posthog_lifecycle_sync.py. PostHog's own- # person-update event, not a user action; keep it out of funnels.+ # Person properties: backend/data/posthog_lifecycle_sync.py,+ # product_analytics.set_onboarding_role and+ # backend/notifications/posthog_audience.py. PostHog's own person-update+ # event, not a user action; keep it out of funnels.SET_PERSON_PROPERTIES = "$set"# Key moments (SECRT-2723): backend/util/product_analytics.pydiff --git a/autogpt_platform/backend/backend/util/product_analytics.py b/autogpt_platform/backend/backend/util/product_analytics.pyindex c41cfa20dd..f5ff2df680 100644--- a/autogpt_platform/backend/backend/util/product_analytics.py+++ b/autogpt_platform/backend/backend/util/product_analytics.py@@ -59,6 +59,8 @@ from backend.util import posthog_clientfrom backend.util.posthog_events import PostHogEventif TYPE_CHECKING:+ from posthog import Posthog+from backend.data.execution import GraphExecutionEntry, GraphExecutionMetafrom backend.data.model import GraphExecutionStats@@ -351,17 +353,23 @@ def track_onboarding_completed(*, user_id: str) -> None:def set_onboarding_role(- *, user_id: str, role: OnboardingRole, keep_existing: bool = False-) -> None:+ *,+ user_id: str,+ role: OnboardingRole,+ keep_existing: bool = False,+ client: "Posthog | None" = None,+) -> bool:"""The role picked in onboarding as person properties, so funnels andretention can be split by it. Other's text is cleared for any other pick.- `keep_existing` is for the backfill: a role PostHog already holds is the- newer pick, so it is left alone."""+ `keep_existing` and `client` are for the backfill: a role PostHog already+ holds is the newer pick, so it is left alone, and the run sends on a client+ of its own (`posthog_client.send_each`). False when it wasn't queued."""person = {"onboarding_role": role.label, "onboarding_role_other": role.other}- posthog_client.capture(+ return posthog_client.capture(user_id,PostHogEvent.SET_PERSON_PROPERTIES,{"$set_once" if keep_existing else "$set": person},+ client=client,)diff --git a/docs/engineering/tracking-plan.md b/docs/engineering/tracking-plan.mdindex d6752626d7..50ded17b63 100644--- a/docs/engineering/tracking-plan.md+++ b/docs/engineering/tracking-plan.md@@ -348,6 +348,38 @@ exact picks only: any other stored value may be `Other`'s text or a rewrite,so it is left unset. MailerLite gets the same values as `role` and`role_other`.+The fields GTM segments checkout openers on in MailerLite are on the person+too (SECRT-2895, `backend/notifications/posthog_audience.py`). They are worked+out by the same code (`audience_enrichment.checkout_fields`) and named as in+MailerLite, except the checkout open, which is a moment here and a day there.+Every account gets the first two at signup. Every checkout opener gets all of+them from the checkout routes and the completed-checkout webhook. Unlike+MailerLite, PostHog also gets accounts that opted out of marketing or that a+signal places in Iran or Russia: it is analytics, not marketing.++| Property | Value |+| --- | --- |+| `email_type` | `business`, `personal` (a webmail provider) or `education`. |+| `signin_method` | `google` or `email`; `google` wins for an account that has both. |+| `checkout_opened_at` | When the account first opened Stripe checkout. |+| `country` | The country's English name, from the strongest source seen (see `country_source`). |+| `country_code` | The same country as an ISO 3166-1 alpha-2 code. |+| `country_source` | `stripe` (the billing address), `ip` (the visitor's IP country at checkout) or `timezone` (the browser's). |+| `exclude_de_at` | `yes` when any signal points at Germany or Austria, else `no`. |++MailerLite reads the subscriber before each write so it never makes a value+worse; PostHog can't be read on the way, so `$set_once`, which only fills in+what the person doesn't hold yet, keeps the same rules. The first open stays.+`exclude_de_at` `yes` is set and `no` only filled in, so it never goes back to+`no`. A billing country is set, so it always wins, and an IP or timezone+country is only filled in, so it never replaces one. The one difference:+PostHog keeps the first IP or timezone country it gets, where MailerLite moves+on to a later IP country. `poetry run cli+posthog-audience-backfill` sends them for earlier accounts, by the same rules,+from Stripe's first Checkout Session (set outright, since Stripe holds every+open) and its billing country or else the browser's timezone. An IP country is+never backfilled, since it was never stored.+## RemovedNo longer sent (SECRT-2722, SECRT-2723). The names stay reserved: the pin tests fail if