Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,19 @@
# get payload
print(posthog.get_feature_flag_payload("beta-feature", "distinct_id"))
print(posthog.get_all_flags_and_payloads("distinct_id"))
exit()
# # Alias a previous distinct id with a new one

# get feature flag result with all details (enabled, variant, payload, key, reason)
result = posthog.get_feature_flag_result("beta-feature", "distinct_id")
if result:
print(f"Flag key: {result.key}")
print(f"Flag enabled: {result.enabled}")
print(f"Variant: {result.variant}")
print(f"Payload: {result.payload}")
print(f"Reason: {result.reason}")
# get_value() returns the variant if it exists, otherwise the enabled value
print(f"Value (variant or enabled): {result.get_value()}")

# Alias a previous distinct id with a new one

posthog.alias("distinct_id", "new_distinct_id")

Expand Down
105 changes: 74 additions & 31 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
set_context_session as inner_set_context_session,
identify_context as inner_identify_context,
)
from posthog.types import FeatureFlag, FlagsAndPayloads
from posthog.types import FeatureFlag, FlagsAndPayloads, FeatureFlagResult
from posthog.version import VERSION

__version__ = VERSION
Expand Down Expand Up @@ -388,9 +388,9 @@ def capture_exception(
def feature_enabled(
key, # type: str
distinct_id, # type: str
groups={}, # type: dict
person_properties={}, # type: dict
group_properties={}, # type: dict
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False, # type: bool
send_feature_flag_events=True, # type: bool
disable_geoip=None, # type: Optional[bool]
Expand Down Expand Up @@ -427,9 +427,9 @@ def feature_enabled(
"feature_enabled",
key=key,
distinct_id=distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
send_feature_flag_events=send_feature_flag_events,
disable_geoip=disable_geoip,
Expand All @@ -439,9 +439,9 @@ def feature_enabled(
def get_feature_flag(
key, # type: str
distinct_id, # type: str
groups={}, # type: dict
person_properties={}, # type: dict
group_properties={}, # type: dict
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False, # type: bool
send_feature_flag_events=True, # type: bool
disable_geoip=None, # type: Optional[bool]
Expand Down Expand Up @@ -477,9 +477,9 @@ def get_feature_flag(
"get_feature_flag",
key=key,
distinct_id=distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
send_feature_flag_events=send_feature_flag_events,
disable_geoip=disable_geoip,
Expand All @@ -488,9 +488,9 @@ def get_feature_flag(

def get_all_flags(
distinct_id, # type: str
groups={}, # type: dict
person_properties={}, # type: dict
group_properties={}, # type: dict
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False, # type: bool
disable_geoip=None, # type: Optional[bool]
) -> Optional[dict[str, FeatureFlag]]:
Expand Down Expand Up @@ -520,21 +520,64 @@ def get_all_flags(
return _proxy(
"get_all_flags",
distinct_id=distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
disable_geoip=disable_geoip,
)


def get_feature_flag_result(
key,
distinct_id,
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False,
send_feature_flag_events=True,
disable_geoip=None, # type: Optional[bool]
):
# type: (...) -> Optional[FeatureFlagResult]
"""
Get a FeatureFlagResult object which contains the flag result and payload.

This method evaluates a feature flag and returns a FeatureFlagResult object containing:
- enabled: Whether the flag is enabled
- variant: The variant value if the flag has variants
- payload: The payload associated with the flag (automatically deserialized from JSON)
- key: The flag key
- reason: Why the flag was enabled/disabled

Example:
```python
result = posthog.get_feature_flag_result('beta-feature', 'distinct_id')
if result and result.enabled:
# Use the variant and payload
print(f"Variant: {result.variant}")
print(f"Payload: {result.payload}")
```
"""
return _proxy(
"get_feature_flag_result",
key=key,
distinct_id=distinct_id,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
send_feature_flag_events=send_feature_flag_events,
disable_geoip=disable_geoip,
)


def get_feature_flag_payload(
key,
distinct_id,
match_value=None,
groups={},
person_properties={},
group_properties={},
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False,
send_feature_flag_events=True,
disable_geoip=None, # type: Optional[bool]
Expand All @@ -544,9 +587,9 @@ def get_feature_flag_payload(
key=key,
distinct_id=distinct_id,
match_value=match_value,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
send_feature_flag_events=send_feature_flag_events,
disable_geoip=disable_geoip,
Expand Down Expand Up @@ -575,18 +618,18 @@ def get_remote_config_payload(

def get_all_flags_and_payloads(
distinct_id,
groups={},
person_properties={},
group_properties={},
groups=None, # type: Optional[dict]
person_properties=None, # type: Optional[dict]
group_properties=None, # type: Optional[dict]
only_evaluate_locally=False,
disable_geoip=None, # type: Optional[bool]
) -> FlagsAndPayloads:
return _proxy(
"get_all_flags_and_payloads",
distinct_id=distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
disable_geoip=disable_geoip,
)
Expand Down
Loading
Loading