Skip to content

Commit

Permalink
feat: add super properties
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin committed Oct 3, 2024
1 parent 28c4802 commit d273875
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
poll_interval = 30 # type: int
disable_geoip = True # type: bool
feature_flags_request_timeout_seconds = 3 # type: int
super_properties = None # type: Optional[Dict]
# Currently alpha, use at your own risk
enable_exception_autocapture = False # type: bool
exception_autocapture_integrations = [] # type: List[Integrations]
Expand Down Expand Up @@ -504,6 +505,7 @@ def _proxy(method, *args, **kwargs):
disabled=disabled,
disable_geoip=disable_geoip,
feature_flags_request_timeout_seconds=feature_flags_request_timeout_seconds,
super_properties=super_properties,
# TODO: Currently this monitoring begins only when the Client is initialised (which happens when you do something with the SDK)
# This kind of initialisation is very annoying for exception capture. We need to figure out a way around this,
# 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)
Expand Down
5 changes: 5 additions & 0 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(
disable_geoip=True,
historical_migration=False,
feature_flags_request_timeout_seconds=3,
super_properties=None,
enable_exception_autocapture=False,
exception_autocapture_integrations=None,
project_root=None,
Expand Down Expand Up @@ -86,6 +87,7 @@ def __init__(
self.disabled = disabled
self.disable_geoip = disable_geoip
self.historical_migration = historical_migration
self.super_properties = super_properties
self.enable_exception_autocapture = enable_exception_autocapture
self.exception_autocapture_integrations = exception_autocapture_integrations
self.exception_capture = None
Expand Down Expand Up @@ -211,6 +213,9 @@ def capture(
require("properties", properties, dict)
require("event", event, string_types)

if self.super_properties:
properties = {**properties, **self.super_properties}

msg = {
"properties": properties,
"timestamp": timestamp,
Expand Down
10 changes: 10 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def test_basic_capture_with_project_api_key(self):
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
self.assertEqual(msg["properties"]["$lib_version"], VERSION)

def test_basic_capture_with_super_properties(self):
client = Client(FAKE_TEST_API_KEY, super_properties={'source': 'repo-name', 'environment': 'test'})

_, msg = client.capture("distinct_id", "python test event")
client.flush()

self.assertEqual(msg["event"], "python test event")
self.assertEqual(msg["properties"]["source"], "repo-name")
self.assertEqual(msg["properties"]["environment"], "test")

def test_basic_capture_exception(self):

with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
Expand Down

0 comments on commit d273875

Please sign in to comment.