Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add super properties #138

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
daibhin marked this conversation as resolved.
Show resolved Hide resolved
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")
daibhin marked this conversation as resolved.
Show resolved Hide resolved
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
Loading