From febe8f41e97aeaa67f63cd01fc038b3ddacfc574 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Thu, 16 Nov 2023 16:59:55 -0800 Subject: [PATCH 1/9] Adding flag in decide response to allow returning elements chain as a string; related PR: https://github.com/PostHog/posthog-js/pull/823 --- posthog/api/decide.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/posthog/api/decide.py b/posthog/api/decide.py index 4880c77e8f86d..62c7cf153f460 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -224,7 +224,10 @@ def get_decide(request: HttpRequest): if settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS and str(team.id) in settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS: if random() < settings.NEW_ANALYTICS_CAPTURE_SAMPLING_RATE: - response["analytics"] = {"endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT} + response["analytics"] = { + "endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT, + "elementsChainAsString": True, + } if team.session_recording_opt_in and ( on_permitted_recording_domain(team, request) or not team.recording_domains From 89060c67e36bb8d97fc4941c5754a34f9837a5ec Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 13:38:16 -0800 Subject: [PATCH 2/9] Created a separate env var to control this behavior in https://github.com/PostHog/posthog-cloud-infra/pull/2314 --- posthog/api/decide.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/posthog/api/decide.py b/posthog/api/decide.py index 62c7cf153f460..3b9c89170cb91 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -226,9 +226,13 @@ def get_decide(request: HttpRequest): if random() < settings.NEW_ANALYTICS_CAPTURE_SAMPLING_RATE: response["analytics"] = { "endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT, - "elementsChainAsString": True, } + if settings.ELEMENT_CHAIN_AS_STRING_TEAMS and str(team.id) in settings.ELEMENT_CHAIN_AS_STRING_TEAMS: + response["analytics"] = { + "elementsChainAsString": True, + } + if team.session_recording_opt_in and ( on_permitted_recording_domain(team, request) or not team.recording_domains ): From a559f973125437f3444f0473c9a87e20adfd260d Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 17 Nov 2023 21:49:53 +0000 Subject: [PATCH 3/9] Update query snapshots --- .../test/__snapshots__/test_session_recordings.ambr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr index 0afe5e0ac247d..dc6d8faaf2c19 100644 --- a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr +++ b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr @@ -633,7 +633,7 @@ FROM "posthog_persondistinctid" INNER JOIN "posthog_person" ON ("posthog_persondistinctid"."person_id" = "posthog_person"."id") WHERE ("posthog_persondistinctid"."distinct_id" IN ('user2', - 'user_one_2') + 'user_one_1') AND "posthog_persondistinctid"."team_id" = 2) /*controller='project_session_recordings-list',route='api/projects/%28%3FP%3Cparent_lookup_team_id%3E%5B%5E/.%5D%2B%29/session_recordings/%3F%24'*/ ' --- From a38a62ea1c60125d5326627c3b09fbffd13b3df0 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 13:52:19 -0800 Subject: [PATCH 4/9] Add env var --- posthog/settings/ingestion.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/posthog/settings/ingestion.py b/posthog/settings/ingestion.py index bab3d7f2506ae..eee79759517a8 100644 --- a/posthog/settings/ingestion.py +++ b/posthog/settings/ingestion.py @@ -37,3 +37,5 @@ NEW_ANALYTICS_CAPTURE_ENDPOINT = os.getenv("NEW_CAPTURE_ENDPOINT", "/i/v0/e/") NEW_ANALYTICS_CAPTURE_TEAM_IDS = get_set(os.getenv("NEW_ANALYTICS_CAPTURE_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", "")) From 70b913f6c9b4d2c2b873db1f518d0c8e36a92dde Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 17 Nov 2023 22:01:29 +0000 Subject: [PATCH 5/9] Update query snapshots --- .../test/__snapshots__/test_session_recordings.ambr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr index dc6d8faaf2c19..0afe5e0ac247d 100644 --- a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr +++ b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr @@ -633,7 +633,7 @@ FROM "posthog_persondistinctid" INNER JOIN "posthog_person" ON ("posthog_persondistinctid"."person_id" = "posthog_person"."id") WHERE ("posthog_persondistinctid"."distinct_id" IN ('user2', - 'user_one_1') + 'user_one_2') AND "posthog_persondistinctid"."team_id" = 2) /*controller='project_session_recordings-list',route='api/projects/%28%3FP%3Cparent_lookup_team_id%3E%5B%5E/.%5D%2B%29/session_recordings/%3F%24'*/ ' --- From 313106488147d9d51a8b0853032d1fc24e088b20 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 14:14:12 -0800 Subject: [PATCH 6/9] Formatting --- posthog/api/decide.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/posthog/api/decide.py b/posthog/api/decide.py index 3b9c89170cb91..d23e952d8e20a 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -224,9 +224,7 @@ def get_decide(request: HttpRequest): if settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS and str(team.id) in settings.NEW_ANALYTICS_CAPTURE_TEAM_IDS: if random() < settings.NEW_ANALYTICS_CAPTURE_SAMPLING_RATE: - response["analytics"] = { - "endpoint": settings.NEW_ANALYTICS_CAPTURE_ENDPOINT, - } + 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["analytics"] = { From c568295dcbbdae75f6061c6c5bfe56ae148ebcdf Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 14:14:52 -0800 Subject: [PATCH 7/9] Just use a boolean --- posthog/api/decide.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/posthog/api/decide.py b/posthog/api/decide.py index d23e952d8e20a..da4fa95e62a5b 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -227,9 +227,7 @@ def get_decide(request: HttpRequest): 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["analytics"] = { - "elementsChainAsString": True, - } + response["elementsChainAsString"] = True if team.session_recording_opt_in and ( on_permitted_recording_domain(team, request) or not team.recording_domains From f554c0c1167cfa0b394ea86da17895b230770612 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 15:50:41 -0800 Subject: [PATCH 8/9] Add test for when ELEMENT_CHAIN_AS_STRING_TEAMS is set and not set --- posthog/api/test/test_decide.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/posthog/api/test/test_decide.py b/posthog/api/test/test_decide.py index 9579c2447d4ae..4893baa3d89fb 100644 --- a/posthog/api/test/test_decide.py +++ b/posthog/api/test/test_decide.py @@ -2991,6 +2991,20 @@ def test_decide_new_capture_activation(self, *args): self.assertEqual(response.status_code, 200) self.assertFalse("analytics" in response.json()) + @patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10) + 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.assertEqual(response["elementsChainAsString"], True) + + 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): """ From 78eb95583110640053cacad99ada43082e5ab140 Mon Sep 17 00:00:00 2001 From: Dave Murphy Date: Fri, 17 Nov 2023 16:36:10 -0800 Subject: [PATCH 9/9] Fix test --- posthog/api/test/test_decide.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/api/test/test_decide.py b/posthog/api/test/test_decide.py index 4893baa3d89fb..144bf1324469f 100644 --- a/posthog/api/test/test_decide.py +++ b/posthog/api/test/test_decide.py @@ -2998,7 +2998,7 @@ def test_decide_element_chain_as_string(self, *args): response = self._post_decide(api_version=3) self.assertEqual(response.status_code, 200) self.assertTrue("elementsChainAsString" in response.json()) - self.assertEqual(response["elementsChainAsString"], True) + self.assertTrue(response.json()["elementsChainAsString"]) with self.settings(ELEMENT_CHAIN_AS_STRING_TEAMS={"0"}): response = self._post_decide(api_version=3)