Skip to content

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

feat(flags): evaluate the app's flags in the posthog-web-app context (#119860)

Paul D'Ambra686a156e06aa
diff --git a/frontend/src/loadPostHogJS.tsx b/frontend/src/loadPostHogJS.tsx
index c66942412b78..a2eeb64b9350 100644
--- a/frontend/src/loadPostHogJS.tsx
+++ b/frontend/src/loadPostHogJS.tsx
@@ -113,6 +113,9 @@ export function loadPostHogJS(): void {
: {},
// The first flags request must wait until the user and groups from the page are set.
advanced_disable_feature_flags_on_first_load: true,
+ // Flags tagged only with other contexts stay out of the app. The server bootstrap
+ // applies the same rule in `flags_for_app_evaluation_context` (posthog/utils.py).
+ evaluation_contexts: ['posthog-web-app'],
opt_in_site_apps: true,
disable_surveys: window.IMPERSONATED_SESSION,
disable_product_tours: true,
diff --git a/posthog/test/test_get_context_for_template.py b/posthog/test/test_get_context_for_template.py
index 6053aa04d053..bd2571b89721 100644
--- a/posthog/test/test_get_context_for_template.py
+++ b/posthog/test/test_get_context_for_template.py
@@ -17,7 +17,11 @@ from parameterized import parameterized
from posthog.api.tagged_item import set_tags_on_object
from posthog.models import UserHomeSettings
from posthog.stable_chunks import StableChunks
-from posthog.utils import get_context_for_template, get_persisted_feature_flags_for_app_context
+from posthog.utils import (
+ flags_for_app_evaluation_context,
+ get_context_for_template,
+ get_persisted_feature_flags_for_app_context,
+)
from products.conversations.backend.services.identity import IDENTITY_CLAIM_MAX_AGE_SECONDS
@@ -92,6 +96,31 @@ STABLE_CHUNKS = StableChunks(
)
+class TestFlagsForAppEvaluationContext(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("no_contexts_is_kept", None, True),
+ ("empty_contexts_is_kept", [], True),
+ ("app_context_is_kept", ["posthog-web-app", "posthog-website"], True),
+ ("other_context_is_dropped", ["posthog-website"], False),
+ ]
+ )
+ def test_keeps_flags_the_app_evaluates(self, _name, evaluation_contexts, kept):
+ definitions = [{"key": "the-flag", "evaluation_contexts": evaluation_contexts}]
+
+ actual = flags_for_app_evaluation_context({"the-flag": True, "untagged": True}, definitions)
+
+ assert actual == ({"the-flag": True, "untagged": True} if kept else {"untagged": True})
+
+ def test_keeps_every_flag_before_definitions_load(self):
+ assert flags_for_app_evaluation_context({"the-flag": True}, None) == {"the-flag": True}
+
+ def test_keeps_no_flags_when_analytics_is_disabled(self):
+ definitions = [{"key": "the-flag", "evaluation_contexts": ["posthog-website"]}]
+
+ assert flags_for_app_evaluation_context(None, definitions) is None
+
+
class TestGetContextForTemplate(APIBaseTest):
def test_get_context_for_template(self):
with (
diff --git a/posthog/utils.py b/posthog/utils.py
index e5ef2c4b40d6..dc3b6744ef25 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -486,6 +486,27 @@ def _read_preload_manifest(manifest_path: str, include_authenticated_shell: bool
return ("", (), "")
+APP_EVALUATION_CONTEXT = "posthog-web-app"
+
+
+def flags_for_app_evaluation_context(
+ flags: dict[str, Any] | None, definitions: list[dict[str, Any]] | None
+) -> dict[str, Any] | None:
+ """
+ Drops flags that /flags leaves out for the app, which sends `evaluation_contexts: ['posthog-web-app']`
+ from `loadPostHogJS.tsx`. The Python SDK evaluates locally without applying evaluation contexts.
+ """
+ # The SDK returns no flags at all when analytics is disabled, for example in tests or with OPT_OUT_CAPTURE.
+ if flags is None:
+ return None
+ excluded = {
+ definition["key"]
+ for definition in definitions or []
+ if definition.get("evaluation_contexts") and APP_EVALUATION_CONTEXT not in definition["evaluation_contexts"]
+ }
+ return {key: value for key, value in flags.items() if key not in excluded}
+
+
@frozen
class _FlagIdentity:
person_properties: dict[str, Any]
@@ -781,7 +802,9 @@ def _build_template_context(
group_properties=identity.group_properties,
)
# don't forcefully set distinctID, as this breaks the link for anonymous users coming from `posthog.com`.
- posthog_bootstrap["featureFlags"] = feature_flags
+ posthog_bootstrap["featureFlags"] = flags_for_app_evaluation_context(
+ feature_flags, posthoganalytics.feature_flag_definitions()
+ )
# This allows immediate flag availability on the frontend, atleast for flags
# that don't depend on any person properties. To get these flags, add person properties to the