diff --git a/posthog/api/decide.py b/posthog/api/decide.py index b5117599b5833..4880c77e8f86d 100644 --- a/posthog/api/decide.py +++ b/posthog/api/decide.py @@ -14,6 +14,7 @@ from posthog.api.geoip import get_geoip_properties from posthog.api.utils import get_project_id, get_token +from posthog.api.survey import SURVEY_TARGETING_FLAG_PREFIX from posthog.database_healthcheck import DATABASE_FOR_FLAG_MATCHING from posthog.exceptions import RequestParsingError, generate_exception_response from posthog.logging.timing import timed @@ -268,10 +269,12 @@ def get_decide(request: HttpRequest): if feature_flags: # Billing analytics for decide requests with feature flags - # Sample no. of decide requests with feature flags - if settings.DECIDE_BILLING_SAMPLING_RATE and random() < settings.DECIDE_BILLING_SAMPLING_RATE: - count = int(1 / settings.DECIDE_BILLING_SAMPLING_RATE) - increment_request_count(team.pk, count) + # Don't count if all requests are for survey targeting flags only. + if not all(flag.startswith(SURVEY_TARGETING_FLAG_PREFIX) for flag in feature_flags.keys()): + # Sample no. of decide requests with feature flags + if settings.DECIDE_BILLING_SAMPLING_RATE and random() < settings.DECIDE_BILLING_SAMPLING_RATE: + count = int(1 / settings.DECIDE_BILLING_SAMPLING_RATE) + increment_request_count(team.pk, count) else: # no auth provided diff --git a/posthog/api/test/test_decide.py b/posthog/api/test/test_decide.py index b8bd4e3b331f2..9579c2447d4ae 100644 --- a/posthog/api/test/test_decide.py +++ b/posthog/api/test/test_decide.py @@ -2843,7 +2843,7 @@ def test_decide_analytics_samples_dont_break_with_zero_sampling(self, *args): self.assertEqual(client.hgetall(f"posthog:decide_requests:{self.team.pk}"), {}) @patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10) - def test_decide_analytics_fires_with_survey_linked_and_targeting_flags(self, *args): + def test_decide_analytics_only_fires_with_non_survey_targeting_flags(self, *args): ff = FeatureFlag.objects.create( team=self.team, rollout_percentage=50, @@ -2905,7 +2905,7 @@ def test_decide_analytics_fires_with_survey_linked_and_targeting_flags(self, *ar ) @patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10) - def test_decide_analytics_fire_for_survey_targeting_flags(self, *args): + def test_decide_analytics_does_not_fire_for_survey_targeting_flags(self, *args): FeatureFlag.objects.create( team=self.team, rollout_percentage=50, @@ -2960,10 +2960,7 @@ def test_decide_analytics_fire_for_survey_targeting_flags(self, *args): client = redis.get_client() # check that single increment made it to redis - self.assertEqual( - client.hgetall(f"posthog:decide_requests:{self.team.pk}"), - {b"165192618": b"1"}, - ) + self.assertEqual(client.hgetall(f"posthog:decide_requests:{self.team.pk}"), {}) @patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10) def test_decide_new_capture_activation(self, *args):