Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(replay): convert trigger group flag objects for every project (#127637)

Kim Svatos Dugand0f709c0489b
diff --git a/posthog/models/remote_config.py b/posthog/models/remote_config.py
index 72343f38fa56..fd3542e3323f 100644
--- a/posthog/models/remote_config.py
+++ b/posthog/models/remote_config.py
@@ -21,9 +21,7 @@ from posthog.models.team.extensions import get_or_create_team_extension
from posthog.models.team.js_snippet_config import TeamJsSnippetConfig
from posthog.models.team.team import Team
from posthog.models.utils import UUIDTModel, execute_with_timeout
-from posthog.ph_client import feature_enabled_or_false
from posthog.storage.hypercache import HyperCache, HyperCacheStoreMissing
-from posthog.utils import get_instance_region
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
from products.cdp.backend.models.plugin import PluginConfig
@@ -69,27 +67,6 @@ def indent_js(js_content: str, indent: int = 4) -> str:
return joined
-# Converting a trigger group's flag object starts recordings that the group never made before.
-# This PostHog flag targets the project group by id and region.
-# PostHog turns it on for a team only after it notifies that team.
-TRIGGER_GROUP_OBJECT_FLAGS_ROLLOUT = "replay-trigger-group-object-flags"
-
-
-def _converts_trigger_group_object_flags(team_id: int) -> bool:
- # Local evaluation keeps a config build from waiting on a remote call. A flag that cannot be evaluated reads as off.
- # Each region numbers its teams separately. EU evaluates a mirror of the US flag.
- # Every condition must filter on region, or it also matches the project with the same id in the other region.
- region = get_instance_region() or "DEV"
- return feature_enabled_or_false(
- TRIGGER_GROUP_OBJECT_FLAGS_ROLLOUT,
- f"team-{team_id}",
- groups={"project": f"{region}:{team_id}"},
- group_properties={"project": {"id": str(team_id), "region": region}},
- only_evaluate_locally=True,
- send_feature_flag_events=False,
- )
-
-
def _flag_for_sdk(flag: dict[str, Any]) -> str | dict[str, Any] | None:
# posthog-js reads a flag gate as a bare key, or as {"flag": key, "variant": variant} to gate on one variant.
key = flag.get("key")
@@ -234,12 +211,7 @@ class RemoteConfig(UUIDTModel):
# Converting a missing or empty key would send an empty flag.
# The SDK reads an empty flag as no flag condition.
# A group with no other condition then records every session.
- if (
- isinstance(flag, dict)
- and isinstance(key := flag.get("key"), str)
- and key
- and _converts_trigger_group_object_flags(team.id)
- ):
+ if isinstance(flag, dict) and isinstance(key := flag.get("key"), str) and key:
conditions = {**conditions, "flag": _flag_for_sdk(flag)}
normalized_groups.append({**group, "conditions": conditions})
diff --git a/posthog/models/test/test_remote_config.py b/posthog/models/test/test_remote_config.py
index b36e4fd980d5..4ac13a805df7 100644
--- a/posthog/models/test/test_remote_config.py
+++ b/posthog/models/test/test_remote_config.py
@@ -17,7 +17,6 @@ from posthog.models.integration import Integration
from posthog.models.project import Project
from posthog.models.remote_config import (
REMOTE_CONFIG_CACHE_EXPIRY_SORTED_SET,
- TRIGGER_GROUP_OBJECT_FLAGS_ROLLOUT,
RemoteConfig,
RemoteConfigTokenChangeSyncError,
UnreadableTriggerGroupsError,
@@ -418,13 +417,7 @@ class TestRemoteConfig(_RemoteConfigBase):
expect_reported: bool,
) -> None:
self.team.session_recording_trigger_groups = stored
- with (
- patch("posthog.models.remote_config.capture_exception") as mock_capture_exception,
- patch(
- "posthoganalytics.feature_enabled",
- side_effect=lambda key, *args, **kwargs: key == TRIGGER_GROUP_OBJECT_FLAGS_ROLLOUT,
- ),
- ):
+ with patch("posthog.models.remote_config.capture_exception") as mock_capture_exception:
self.team.save()
self.sync_remote_config()
reported = any(
@@ -438,35 +431,6 @@ class TestRemoteConfig(_RemoteConfigBase):
assert session_recording["version"] == 2
assert session_recording["triggerGroups"] == expected_trigger_groups
- @parameterized.expand(
- [
- ("rollout_region", "US", {"flag": "k", "variant": "test"}),
- ("same_project_id_other_region", "EU", {"id": 7, "key": "k", "variant": "test"}),
- ]
- )
- def test_session_recording_trigger_group_flag_rollout_targets_one_region(
- self, _name: str, region: str, expected_flag: dict[str, Any]
- ) -> None:
- def rollout_for_us_project(key: str, *args: Any, group_properties: Any = None, **kwargs: Any) -> bool:
- project = (group_properties or {}).get("project", {})
- return (
- key == TRIGGER_GROUP_OBJECT_FLAGS_ROLLOUT
- and project.get("id") == str(self.team.id)
- and project.get("region") == "US"
- )
-
- flag = {"id": 7, "key": "k", "variant": "test"}
- self.team.session_recording_trigger_groups = {"version": 2, "groups": [_flag_group(flag, events=["purchase"])]}
- with (
- override_settings(CLOUD_DEPLOYMENT=region),
- patch("posthoganalytics.feature_enabled", side_effect=rollout_for_us_project),
- ):
- self.team.save()
- self.sync_remote_config()
- assert self.remote_config.config["sessionRecording"]["triggerGroups"] == [
- _flag_group(expected_flag, events=[{"name": "purchase"}])
- ]
-
def test_extra_settings_recorder_script(self):
self.team.session_recording_opt_in = True
self.team.extra_settings = {"recorder_script": "custom-recorder"}