From 73efda37a846eb20cb9e6a236e0ba51a93430181 Mon Sep 17 00:00:00 2001 From: David Murphy Date: Mon, 20 Nov 2023 10:26:43 -0800 Subject: [PATCH] feat(django): allow elements chain as string for certain teams (#18701) * Adding flag in decide response to allow returning elements chain as a string; related PR: https://github.com/PostHog/posthog-js/pull/823 * Created a separate env var to control this behavior in https://github.com/PostHog/posthog-cloud-infra/pull/2314 * Update query snapshots * Add env var * Update query snapshots * Formatting * Just use a boolean * Add test for when ELEMENT_CHAIN_AS_STRING_TEAMS is set and not set * Fix test --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- posthog/api/decide.py | 3 +++ posthog/api/test/test_decide.py | 13 +++++++++++++ posthog/settings/ingestion.py | 2 ++ 3 files changed, 18 insertions(+) diff --git a/posthog/api/decide.py b/posthog/api/decide.py index e3dfe08ae3ed0..ccb6670af4584 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -230,6 +230,9 @@ def get_decide(request: HttpRequest): if random() < settings.NEW_ANALYTICS_CAPTURE_SAMPLING_RATE: response["analytics"] = {"endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT} + if settings.ELEMENT_CHAIN_AS_STRING_TEAMS and str(team.id) in settings.ELEMENT_CHAIN_AS_STRING_TEAMS: + response["elementsChainAsString"] = True + if team.session_recording_opt_in and ( on_permitted_recording_domain(team, request) or not team.recording_domains ): diff --git a/posthog/api/test/test_decide.py b/posthog/api/test/test_decide.py index 6e12087fca2b1..8c670c3243529 100644 --- a/posthog/api/test/test_decide.py +++ b/posthog/api/test/test_decide.py @@ -3006,6 +3006,19 @@ def test_decide_new_capture_activation(self, *args): self.assertEqual(response.status_code, 200) self.assertFalse("analytics" in response.json()) + def test_decide_element_chain_as_string(self, *args): + self.client.logout() + with self.settings(ELEMENT_CHAIN_AS_STRING_TEAMS={str(self.team.id)}): + response = self._post_decide(api_version=3) + self.assertEqual(response.status_code, 200) + self.assertTrue("elementsChainAsString" in response.json()) + self.assertTrue(response.json()["elementsChainAsString"]) + + with self.settings(ELEMENT_CHAIN_AS_STRING_TEAMS={"0"}): + response = self._post_decide(api_version=3) + self.assertEqual(response.status_code, 200) + self.assertFalse("elementsChainAsString" in response.json()) + class TestDatabaseCheckForDecide(BaseTest, QueryMatchingTest): """ diff --git a/posthog/settings/ingestion.py b/posthog/settings/ingestion.py index 41080595deac1..b559f5726ca29 100644 --- a/posthog/settings/ingestion.py +++ b/posthog/settings/ingestion.py @@ -38,3 +38,5 @@ NEW_ANALYTICS_CAPTURE_TEAM_IDS = get_set(os.getenv("NEW_ANALYTICS_CAPTURE_TEAM_IDS", "")) NEW_ANALYTICS_CAPTURE_EXCLUDED_TEAM_IDS = get_set(os.getenv("NEW_ANALYTICS_CAPTURE_EXCLUDED_TEAM_IDS", "")) NEW_ANALYTICS_CAPTURE_SAMPLING_RATE = get_from_env("NEW_ANALYTICS_CAPTURE_SAMPLING_RATE", type_cast=float, default=1.0) + +ELEMENT_CHAIN_AS_STRING_TEAMS = get_set(os.getenv("ELEMENT_CHAIN_AS_STRING_TEAMS", ""))