Skip to content

Commit 854b6f7

Browse files
fix: pass before_send through module-level setup
1 parent c3f9cfc commit 854b6f7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

posthog/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from posthog.types import (
5959
FeatureFlag,
6060
FlagsAndPayloads,
61+
BeforeSendCallback
6162
)
6263
from posthog.types import (
6364
FeatureFlagResult as FeatureFlagResult,
@@ -250,6 +251,7 @@ def get_tags() -> Dict[str, Any]:
250251
project_root = None # type: Optional[str]
251252
# Used for our AI observability feature to not capture any prompt or output just usage + metadata
252253
privacy_mode = False # type: bool
254+
before_send = None # type: Optional[BeforeSendCallback]
253255
# Whether to enable feature flag polling for local evaluation by default. Defaults to True.
254256
# We recommend setting this to False if you are only using the personalApiKey for evaluating remote config payloads via `get_remote_config_payload` and not using local evaluation.
255257
enable_local_evaluation = True # type: bool
@@ -875,6 +877,7 @@ def setup() -> Client:
875877
# or deprecate this proxy option fully (it's already in the process of deprecation, no new clients should be using this method since like 5-6 months)
876878
enable_exception_autocapture=enable_exception_autocapture,
877879
log_captured_exceptions=log_captured_exceptions,
880+
before_send=before_send,
878881
enable_local_evaluation=enable_local_evaluation,
879882
flag_definition_cache_provider=flag_definition_cache_provider,
880883
capture_exception_code_variables=capture_exception_code_variables,

posthog/test/test_before_send.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import importlib
12
import unittest
23

34
import mock
5+
import posthog
46

57
from posthog.client import Client
68
from posthog.test.test_utils import FAKE_TEST_API_KEY
@@ -216,3 +218,33 @@ def scrub_pii(event):
216218
self.assertEqual(enqueued_msg["properties"]["email"], "***@example.com")
217219
self.assertNotIn("credit_card", enqueued_msg["properties"])
218220
self.assertEqual(enqueued_msg["properties"]["form_name"], "contact")
221+
222+
223+
class TestModuleLevelBeforeSend(unittest.TestCase):
224+
def setUp(self):
225+
importlib.reload(posthog)
226+
227+
def tearDown(self):
228+
if posthog.default_client:
229+
posthog.shutdown()
230+
importlib.reload(posthog)
231+
232+
def test_before_send_callback_used_during_module_level_setup(self):
233+
def my_before_send(event):
234+
event["properties"]["module_level_before_send"] = True
235+
return event
236+
237+
with mock.patch("posthog.client.batch_post") as mock_post:
238+
posthog.api_key = FAKE_TEST_API_KEY
239+
posthog.before_send = my_before_send
240+
posthog.sync_mode = True
241+
242+
msg_uuid = posthog.capture("test_event", distinct_id="user1")
243+
244+
self.assertIsNotNone(msg_uuid)
245+
self.assertIs(posthog.default_client.before_send, my_before_send)
246+
247+
mock_post.assert_called_once()
248+
batch_data = mock_post.call_args[1]["batch"]
249+
enqueued_msg = batch_data[0]
250+
self.assertTrue(enqueued_msg["properties"]["module_level_before_send"])

0 commit comments

Comments
 (0)