references/posthog-python.md · 24000
key?(any) - The feature flag keydistinct_id?(any) - The user's distinct IDgroups(any) - Groups mappingperson_properties(any) - Person propertiesgroup_properties(any) - Group propertiesonly_evaluate_locally(bool) - Whether to evaluate only locallysend_feature_flag_events(bool) - Whether to send feature flag eventsdisable_geoip(any) - Whether to disable GeoIP lookup
Returns
None
Examples
# Boolean feature flag
from posthog import feature_enabled, get_feature_flag_payload
is_my_flag_enabled = feature_enabled('flag-key', 'distinct_id_of_your_user')
if is_my_flag_enabled:
matched_flag_payload = get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
feature_flag_definitions()
Release Tag: public
Returns loaded feature flags.
Notes:
Returns loaded feature flags, if any. Helpful for debugging what flag information you have loaded.
Returns
None
Examples
from posthog import feature_flag_definitions
definitions = feature_flag_definitions()
get_all_flags()
Release Tag: public
Get all flags for a given user.
Notes:
Flags are key-value pairs where the key is the flag key and the value is the flag variant, or True, or False.
Parameters
distinct_id?(any) - The user's distinct IDgroups(any) - Groups mappingperson_properties(any) - Person propertiesgroup_properties(any) - Group propertiesonly_evaluate_locally(bool) - Whether to evaluate only locallydisable_geoip(any) - Whether to disable GeoIP lookup
Returns
Optional[dict[str, FeatureFlag]]
Examples
# All flags for user
from posthog import get_all_flags
get_all_flags('distinct_id_of_your_user')
get_feature_flag()
Release Tag: public
Get feature flag variant for users. Used with experiments.
Notes:
groups are a mapping from group type to group key. So, if you have a group type of "organization" and a group key of "5", you would pass groups={"organization": "5"}. group_properties take the format: { group_type_name: { group_properties } }. So, for example, if you have the group type "organization" and the group key "5", with the properties name, and employee count, you'll send these as: group_properties={"organization": {"name": "PostHog", "employees": 11}}.
Parameters
key?(any) - The feature flag keydistinct_id?(any) - The user's distinct IDgroups(any) - Groups mapping from group type to group keyperson_properties(any) - Person propertiesgroup_properties(any) - Group properties in format { group_type_name: { group_properties } }only_evaluate_locally(bool) - Whether to evaluate only locallysend_feature_flag_events(bool) - Whether to send feature flag eventsdisable_geoip(any) - Whether to disable GeoIP lookup
Returns
Optional[FeatureFlag]
Examples
# Multivariate feature flag
from posthog import get_feature_flag, get_feature_flag_payload
enabled_variant = get_feature_flag('flag-key', 'distinct_id_of_your_user')
if enabled_variant == 'variant-key':
matched_flag_payload = get_feature_flag_payload('flag-key', 'distinct_id_of_your_user')
load_feature_flags()
Release Tag: public
Load feature flag definitions from PostHog.
Returns
None
Examples
from posthog import load_feature_flags
load_feature_flags()
Client management methods
flush()
Release Tag: public
Tell the client to flush all queued events.
Returns
None
Examples
from posthog import flush
flush()
join()
Release Tag: public
Block program until the client clears the queue. Used during program shutdown. You should use shutdown() directly in most cases.
Returns
None
Examples
from posthog import join
join()
shutdown()
Release Tag: public
Flush all messages and cleanly shutdown the client.
Returns
None
Examples
from posthog import shutdown
shutdown()
Other methods
get_feature_flag_result()
Release Tag: public
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}")
Parameters
key?(any)distinct_id?(any)groups(any)person_properties(any)group_properties(any)only_evaluate_locally(bool)send_feature_flag_events(bool)disable_geoip(any)
Returns
None
get_remote_config_payload()
Release Tag: public
Get the payload for a remote config feature flag.
Parameters
key?(any) - The key of the feature flag
Returns
None
set_capture_exception_code_variables_context()
Release Tag: public
Set whether code variables are captured for the current context.
Parameters
enabled?(bool)
Returns
None
set_code_variables_ignore_patterns_context()
Release Tag: public
Variable names matching these patterns will be ignored completely when capturing code variables.
Parameters
ignore_patterns?(list)
Returns
None
set_code_variables_mask_patterns_context()
Release Tag: public
Variable names matching these patterns will be masked with *** when capturing code variables.
Parameters
mask_patterns?(list)
Returns
None
Contexts methods
new_context()
Release Tag: public
Create a new context scope that will be active for the duration of the with block.
Parameters
fresh(bool) - Whether to start with a fresh context (default: False)capture_exceptions(bool) - Whether to capture exceptions raised within the context (default: True)client(any) - Optional Posthog client instance to use for this context (default: None)
Returns
None
Examples
from posthog import new_context, tag, capture
with new_context():
tag("request_id", "123")
capture("event_name", properties={"property": "value"})
scoped()
Release Tag: public
Decorator that creates a new context for the function.
Parameters
fresh(bool) - Whether to start with a fresh context (default: False)capture_exceptions(bool) - Whether to capture and track exceptions with posthog error tracking (default: True)
Returns
None
Examples
from posthog import scoped, tag, capture
@scoped()
def process_payment(payment_id):
tag("payment_id", payment_id)
capture("payment_started")
set_context_session()
Release Tag: public
Set the session ID for the current context.
Parameters
session_id?(str) - The session ID to associate with the current context and its children
Returns
None
Examples
from posthog import set_context_session
set_context_session("session_123")
tag()
Release Tag: public
Add a tag to the current context.
Parameters
name?(str) - The tag keyvalue?(Any) - The tag value
Returns
None
Examples
from posthog import tag
tag("user_id", "123")