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(self-capture): use dogfood-flags team token for `js_posthog_api_key` (#72444)

Rafael Audibert4dea63475b2c
diff --git a/posthog/test/test_get_context_for_template.py b/posthog/test/test_get_context_for_template.py
index 0af8e1f35a3a..45d8c2d5a7ab 100644
--- a/posthog/test/test_get_context_for_template.py
+++ b/posthog/test/test_get_context_for_template.py
@@ -23,14 +23,13 @@ class TestGetContextForTemplate(APIBaseTest):
MagicMock(),
)
- # the current team has an api_token
+ # Under self-capture, posthog-js evaluates PostHog's own flags with the dogfood-flags team's
+ # token (first team by PK), which in this test is self.team — not the PH Cloud key.
assert self.team.api_token != "sTMFPsFhdP1Ssg"
- # but we use the posthog cloud api_token for the context
assert actual == {
"git_rev": mock.ANY,
"js_capture_time_to_see_data": False,
- # NB: we default to the PH Cloud key
- "js_posthog_api_key": "sTMFPsFhdP1Ssg",
+ "js_posthog_api_key": self.team.api_token,
"js_posthog_host": "",
"js_url": "http://localhost:8234",
"opt_out_capture": False,
diff --git a/posthog/test/test_utils.py b/posthog/test/test_utils.py
index f6201925a396..fa761ded6de9 100644
--- a/posthog/test/test_utils.py
+++ b/posthog/test/test_utils.py
@@ -11,6 +11,7 @@ from freezegun import freeze_time
from posthog.test.base import BaseTest
from unittest.mock import call, patch
+from django.contrib.auth.models import AnonymousUser
from django.core.cache import cache
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpRequest
@@ -42,6 +43,7 @@ from posthog.utils import (
format_query_params_absolute_url,
get_available_timezones_with_offsets,
get_compare_period_dates,
+ get_context_for_template,
get_default_event_info,
get_default_event_name,
get_dogfood_flags_team_id,
@@ -1391,6 +1393,40 @@ class TestBuildFlagProvider(TestCase):
assert _build_flag_provider()._resolve_team_id() == 5
+class TestSelfCaptureBrowserFlagToken(TestCase):
+ PASSWORD = "testpassword12345"
+
+ def setUp(self):
+ super().setUp()
+ # The dogfood branch reads the whole teams table; clear ambient rows so the team we create
+ # is the first one. Cascade deletes roll back with the test transaction.
+ User.objects.all().delete()
+ Organization.objects.all().delete()
+
+ @override_settings(SELF_CAPTURE=True, E2E_TESTING=False)
+ def test_browser_token_uses_dogfood_flags_team_not_self_capture_team(self):
+ # js_posthog_api_key is the token posthog-js evaluates PostHog's own flags with, so it must
+ # be the dogfood-flags team (first team, where flags are synced) even when a more-recently
+ # active user's current_team points at another team that holds no internal flags. Sourcing
+ # it from the self-capture team instead made local flags load then vanish on reload.
+ organization = Organization.objects.create(name="Org")
+ first_team = Team.objects.create(organization=organization, name="First")
+ recent_team = Team.objects.create(organization=organization, name="Recent")
+
+ recent_user = User.objects.create_and_join(organization, "[email protected]", self.PASSWORD)
+ recent_user.current_team = recent_team
+ recent_user.last_login = datetime(2026, 1, 2, tzinfo=ZoneInfo("UTC"))
+ recent_user.save()
+
+ request = RequestFactory().get("/?no-preloaded-app-context=1")
+ request.user = AnonymousUser()
+
+ context = get_context_for_template("head.html", request)
+
+ assert context["js_posthog_api_key"] == first_team.api_token
+ assert first_team.api_token != recent_team.api_token
+
+
VALID_PRELOAD_MANIFEST = {
"css": "static/index-ABC123.css",
"font": "static/assets/Inter-DEF456.woff2",
diff --git a/posthog/utils.py b/posthog/utils.py
index 9cef011c7f33..cdde8fba6533 100644
--- a/posthog/utils.py
+++ b/posthog/utils.py
@@ -531,7 +531,17 @@ def _build_template_context(
context["js_posthog_ui_host"] = "https://us.posthog.com"
elif settings.SELF_CAPTURE:
- if posthoganalytics.api_key:
+ # posthog-js uses this token to evaluate PostHog's own gating flags, so it must point at the
+ # team those flags are synced to — the dogfood-flags team (first team by PK), the same team
+ # the server-side bootstrap evaluates against via _build_flag_provider(). Do NOT use the
+ # self-capture team here (posthoganalytics.api_key = most-recently-active user's current_team):
+ # it drifts onto demo teams that hold no internal flags, so flags load from the bootstrap and
+ # then vanish the moment posthog-js reloads them against that team.
+ dogfood_team = resolve_dogfood_flags_team()
+ if dogfood_team is not None:
+ context["js_posthog_api_key"] = dogfood_team.api_token
+ context["js_posthog_host"] = "" # Becomes location.origin in the frontend
+ elif posthoganalytics.api_key:
context["js_posthog_api_key"] = posthoganalytics.api_key
context["js_posthog_host"] = "" # Becomes location.origin in the frontend
else: