-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy path__init__.py
More file actions
1134 lines (961 loc) · 40.3 KB
/
__init__.py
File metadata and controls
1134 lines (961 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime # noqa: F401
from typing import Any, Callable, Dict, Optional # noqa: F401
from typing_extensions import Unpack
from posthog.args import ExceptionArg, OptionalCaptureArgs, OptionalSetArgs
from posthog.client import Client
from posthog.contexts import (
identify_context as inner_identify_context,
)
from posthog.contexts import (
new_context as inner_new_context,
)
from posthog.contexts import (
scoped as inner_scoped,
)
from posthog.contexts import (
set_capture_exception_code_variables_context as inner_set_capture_exception_code_variables_context,
)
from posthog.contexts import (
set_code_variables_ignore_patterns_context as inner_set_code_variables_ignore_patterns_context,
)
from posthog.contexts import (
set_code_variables_mask_patterns_context as inner_set_code_variables_mask_patterns_context,
)
from posthog.contexts import (
set_context_device_id as inner_set_context_device_id,
)
from posthog.contexts import (
set_context_session as inner_set_context_session,
)
from posthog.contexts import (
tag as inner_tag,
)
from posthog.contexts import (
get_tags as inner_get_tags,
)
from posthog.exception_utils import (
DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS,
DEFAULT_CODE_VARIABLES_MASK_PATTERNS,
)
from posthog.feature_flag_evaluations import (
FeatureFlagEvaluations as FeatureFlagEvaluations,
)
from posthog.feature_flags import (
InconclusiveMatchError as InconclusiveMatchError,
)
from posthog.feature_flags import (
RequiresServerEvaluation as RequiresServerEvaluation,
)
from posthog.flag_definition_cache import (
FlagDefinitionCacheData as FlagDefinitionCacheData,
FlagDefinitionCacheProvider as FlagDefinitionCacheProvider,
)
from posthog.request import (
disable_connection_reuse as disable_connection_reuse,
enable_keep_alive as enable_keep_alive,
set_socket_options as set_socket_options,
SocketOptions as SocketOptions,
)
from posthog.types import (
BeforeSendCallback as BeforeSendCallback,
FeatureFlag,
FlagsAndPayloads,
)
from posthog.types import (
FeatureFlagResult as FeatureFlagResult,
)
from posthog.version import VERSION
__version__ = VERSION
"""Context management."""
def new_context(
fresh: bool = False,
capture_exceptions: bool = True,
client: Optional[Client] = None,
):
"""
Create a new context scope that will be active for the duration of the with block.
Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture exceptions raised within the context (default: True)
client: Optional Posthog client instance to use for this context (default: None)
Examples:
```python
from posthog import new_context, tag, capture
with new_context():
tag("request_id", "123")
capture("event_name", properties={"property": "value"})
```
Category:
Contexts
"""
return inner_new_context(
fresh=fresh, capture_exceptions=capture_exceptions, client=client
)
def scoped(fresh=False, capture_exceptions=True):
"""
Decorator that creates a new context for the function.
Args:
fresh: Whether to start with a fresh context (default: False)
capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)
Examples:
```python
from posthog import scoped, tag, capture
@scoped()
def process_payment(payment_id):
tag("payment_id", payment_id)
capture("payment_started")
```
Category:
Contexts
"""
return inner_scoped(fresh=fresh, capture_exceptions=capture_exceptions)
def set_context_session(session_id: str):
"""
Set the session ID for the current context.
Args:
session_id: The session ID to associate with the current context and its children
Examples:
```python
from posthog import set_context_session
set_context_session("session_123")
```
Category:
Contexts
"""
return inner_set_context_session(session_id)
def set_context_device_id(device_id: str):
"""
Set the device ID for the current context, associating all feature flag requests
in this or child contexts with the given device ID.
Args:
device_id: The device ID to associate with the current context and its children
Examples:
```python
from posthog import set_context_device_id
set_context_device_id("device_123")
```
Category:
Contexts
"""
return inner_set_context_device_id(device_id)
def identify_context(distinct_id: str):
"""
Identify the current context with a distinct ID.
Args:
distinct_id: The distinct ID to associate with the current context and its children
Examples:
```python
from posthog import identify_context
identify_context("user_123")
```
Category:
Identification
"""
return inner_identify_context(distinct_id)
def set_capture_exception_code_variables_context(enabled: bool):
"""
Override code-variable capture for exceptions in the current context.
Args:
enabled: Whether exceptions captured in this context should include local
variable values from stack frames.
Category:
Contexts
"""
return inner_set_capture_exception_code_variables_context(enabled)
def set_code_variables_mask_patterns_context(mask_patterns: list):
"""
Override code-variable mask patterns for exceptions in the current context.
Args:
mask_patterns: Variable-name patterns whose values should be replaced
with ``***`` when code variables are captured.
Category:
Contexts
"""
return inner_set_code_variables_mask_patterns_context(mask_patterns)
def set_code_variables_ignore_patterns_context(ignore_patterns: list):
"""
Override code-variable ignore patterns for exceptions in the current context.
Args:
ignore_patterns: Variable-name patterns that should be omitted entirely
when code variables are captured.
Category:
Contexts
"""
return inner_set_code_variables_ignore_patterns_context(ignore_patterns)
def tag(name: str, value: Any):
"""
Add a tag to the current context.
Args:
name: The tag key
value: The tag value
Examples:
```python
from posthog import tag
tag("user_id", "123")
```
Category:
Contexts
"""
return inner_tag(name, value)
def get_tags() -> Dict[str, Any]:
"""
Get all tags from the current context.
Returns:
Dict of all tags in the current context
Category:
Contexts
"""
return inner_get_tags()
"""Settings.
These module-level settings configure the legacy global PostHog client used by
functions such as ``posthog.capture()``. Set them before your first SDK call.
For new code, prefer creating an explicit ``Posthog``/``Client`` instance with
the corresponding constructor arguments.
Attributes:
api_key: Project API key/token used by the global client. Missing or blank
values create a disabled no-op global client.
host: PostHog ingestion host. Defaults to the US ingestion endpoint when not
set.
on_error: Optional callback invoked by background consumers when event upload
fails.
debug: Enable verbose SDK logging and re-raise errors from public APIs.
send: If False, queueing succeeds but events are not sent to PostHog.
sync_mode: If True, send events synchronously instead of using background
worker threads.
disabled: If True, disable captures and API requests. Useful in tests.
personal_api_key: Personal API key used for local feature flag evaluation
and remote config payloads.
poll_interval: Seconds between local feature flag definition refreshes.
disable_geoip: Whether to disable server-side GeoIP enrichment. Defaults to
True.
is_server: Whether events are emitted from a server-side runtime. Defaults to
True; set to False when using the SDK as a client/CLI so the device OS is
attributed to the person normally.
feature_flags_request_timeout_seconds: Timeout in seconds for feature flag
and remote config requests.
super_properties: Properties merged into every captured event.
enable_exception_autocapture: Automatically capture uncaught exceptions.
log_captured_exceptions: Also log exceptions captured by error tracking.
before_send: Optional callback that can modify or drop events before upload.
Return ``None`` to drop an event.
enable_local_evaluation: Whether to poll feature flag definitions for local
evaluation when a personal API key is configured.
flag_definition_cache_provider: Optional external cache provider for sharing
feature flag definitions across workers.
capture_exception_code_variables: Capture local variable values on exception
stack frames.
code_variables_mask_patterns: Variable-name patterns to mask when capturing
code variables.
code_variables_ignore_patterns: Variable-name patterns to omit when capturing
code variables.
in_app_modules: Module/package prefixes treated as in-app frames in captured
exceptions.
"""
api_key = None # type: Optional[str]
host = None # type: Optional[str]
on_error = None # type: Optional[Callable]
debug = False # type: bool
send = True # type: bool
sync_mode = False # type: bool
disabled = False # type: bool
personal_api_key = None # type: Optional[str]
project_api_key = None # type: Optional[str]
poll_interval = 30 # type: int
disable_geoip = True # type: bool
is_server = True # type: bool
feature_flags_request_timeout_seconds = 3 # type: int
super_properties = None # type: Optional[Dict]
enable_exception_autocapture = False # type: bool
log_captured_exceptions = False # type: bool
# Used to determine in app paths for exception autocapture. Defaults to the current working directory
project_root = None # type: Optional[str]
# Used for our AI observability feature to not capture any prompt or output just usage + metadata
privacy_mode = False # type: bool
before_send = None # type: Optional[BeforeSendCallback]
# Whether to enable feature flag polling for local evaluation by default. Defaults to True.
# 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.
enable_local_evaluation = True # type: bool
flag_definition_cache_provider = None # type: Optional[FlagDefinitionCacheProvider]
default_client = None # type: Optional[Client]
capture_exception_code_variables = False
code_variables_mask_patterns = DEFAULT_CODE_VARIABLES_MASK_PATTERNS
code_variables_ignore_patterns = DEFAULT_CODE_VARIABLES_IGNORE_PATTERNS
in_app_modules = None # type: Optional[list[str]]
# NOTE - this and following functions take unpacked kwargs because we needed to make
# it impossible to write `posthog.capture(distinct-id, event-name)` - basically, to enforce
# the breaking change made between 5.3.0 and 6.0.0. This decision can be unrolled in later
# versions, without a breaking change, to get back the type information in function signatures
def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]:
"""
Capture anything a user does within your system.
Args:
event: The event name to specify the event
**kwargs: Optional arguments including:
distinct_id: Unique identifier for the user
properties: Dict of event properties
timestamp: When the event occurred
uuid: Unique identifier for this event. If omitted, one is generated
and returned.
groups: Dict of group types and IDs
flags: A FeatureFlagEvaluations snapshot from evaluate_flags(). The
exact values from the snapshot are attached with no extra /flags
request.
send_feature_flags: Deprecated. Prefer flags=... from
evaluate_flags(). When truthy, evaluates flags during capture and
attaches them to the event.
disable_geoip: Whether to disable GeoIP lookup
Details:
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up. A capture call requires an event name to specify the event. We recommend using [verb] [noun], like `movie played` or `movie updated` to easily identify what your events mean later on. Capture takes a number of optional arguments, which are defined by the `OptionalCaptureArgs` type.
Examples:
```python
# Context and capture usage
from posthog import new_context, identify_context, tag_context, capture
# Enter a new context (e.g. a request/response cycle, an instance of a background job, etc)
with new_context():
# Associate this context with some user, by distinct_id
identify_context('some user')
# Capture an event, associated with the context-level distinct ID ('some user')
capture('movie started')
# Capture an event associated with some other user (overriding the context-level distinct ID)
capture('movie joined', distinct_id='some-other-user')
# Capture an event with some properties
capture('movie played', properties={'movie_id': '123', 'category': 'romcom'})
# Capture an event with some properties
capture('purchase', properties={'product_id': '123', 'category': 'romcom'})
# Capture an event with some associated group
capture('purchase', groups={'company': 'id:5'})
# Adding a tag to the current context will cause it to appear on all subsequent events
tag_context('some-tag', 'some-value')
capture('another-event') # Will be captured with `'some-tag': 'some-value'` in the properties dict
```
```python
# Set event properties
from posthog import capture
capture(
"user_signed_up",
distinct_id="distinct_id_of_the_user",
properties={
"login_type": "email",
"is_free_trial": "true"
}
)
```
Category:
Events
"""
return _proxy("capture", event, **kwargs)
def set(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]:
"""
Set properties on a user record.
Args:
**kwargs: Optional arguments including:
distinct_id: Unique identifier for the user. Falls back to the
context distinct ID; if none exists, this call does nothing.
properties: Dict of person properties to set.
timestamp: When the properties were set.
uuid: Unique identifier for this operation. If omitted, one is
generated and returned.
disable_geoip: Whether to disable GeoIP lookup.
Details:
This will overwrite previous people property values. Generally operates similar to `capture`, with distinct_id being an optional argument, defaulting to the current context's distinct ID. If there is no context-level distinct ID, and no override distinct_id is passed, this function will do nothing. Context tags are folded into $set properties, so tagging the current context and then calling `set` will cause those tags to be set on the user (unlike capture, which causes them to just be set on the event).
Examples:
```python
# Set person properties
from posthog import set
set(distinct_id='distinct_id', properties={'name': 'Max Hedgehog'})
```
Category:
Identification
"""
return _proxy("set", **kwargs)
def set_once(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]:
"""
Set properties on a user record, only if they do not yet exist.
Args:
**kwargs: Optional arguments including:
distinct_id: Unique identifier for the user. Falls back to the
context distinct ID; if none exists, this call does nothing.
properties: Dict of person properties to set only once.
timestamp: When the properties were set.
uuid: Unique identifier for this operation. If omitted, one is
generated and returned.
disable_geoip: Whether to disable GeoIP lookup.
Details:
This will not overwrite previous people property values, unlike `set`. Otherwise, operates in an identical manner to `set`.
Examples:
```python
# Set property once
from posthog import set_once
set_once(distinct_id='distinct_id', properties={'initial_url': '/blog'})
```
Category:
Identification
"""
return _proxy("set_once", **kwargs)
def group_identify(
group_type, # type: str
group_key, # type: str
properties=None, # type: Optional[Dict]
timestamp=None, # type: Optional[datetime.datetime]
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
distinct_id=None, # type: Optional[str]
):
# type: (...) -> Optional[str]
"""
Set properties on a group.
Args:
group_type: Type of your group
group_key: Unique identifier of the group
properties: Properties to set on the group
timestamp: Optional timestamp for the event
uuid: Optional UUID for the event
disable_geoip: Whether to disable GeoIP lookup
distinct_id: Optional distinct ID of the user performing the action
Examples:
```python
# Group identify
from posthog import group_identify
group_identify('company', 'company_id_in_your_db', {
'name': 'Awesome Inc.',
'employees': 11
})
```
Category:
Identification
"""
return _proxy(
"group_identify",
group_type=group_type,
group_key=group_key,
properties=properties,
timestamp=timestamp,
uuid=uuid,
disable_geoip=disable_geoip,
distinct_id=distinct_id,
)
def alias(
previous_id, # type: str
distinct_id, # type: str
timestamp=None, # type: Optional[datetime.datetime]
uuid=None, # type: Optional[str]
disable_geoip=None, # type: Optional[bool]
):
# type: (...) -> Optional[str]
"""
Associate user behaviour before and after they e.g. register, login, or perform some other identifying action.
Args:
previous_id: The unique ID of the user before
distinct_id: The current unique id
timestamp: Optional timestamp for the event
uuid: Optional UUID for the event
disable_geoip: Whether to disable GeoIP lookup
Details:
To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?". Particularly useful for associating user behaviour before and after they e.g. register, login, or perform some other identifying action.
Examples:
```python
# Alias user
from posthog import alias
alias(previous_id='distinct_id', distinct_id='alias_id')
```
Category:
Identification
"""
return _proxy(
"alias",
previous_id=previous_id,
distinct_id=distinct_id,
timestamp=timestamp,
uuid=uuid,
disable_geoip=disable_geoip,
)
def capture_exception(
exception: Optional[ExceptionArg] = None,
**kwargs: Unpack[OptionalCaptureArgs],
):
"""
Capture exceptions that happen in your code.
Args:
exception: The exception to capture. If not provided, the current exception is captured via `sys.exc_info()`
**kwargs: Optional capture arguments including distinct_id, properties,
timestamp, uuid, groups, flags, send_feature_flags, and disable_geoip.
Details:
Capture exception is idempotent - if it is called twice with the same exception instance, only a occurrence will be tracked in posthog. This is because, generally, contexts will cause exceptions to be captured automatically. However, to ensure you track an exception, if you catch and do not re-raise it, capturing it manually is recommended, unless you are certain it will have crossed a context boundary (e.g. by existing a `with posthog.new_context():` block already). If the passed exception was raised and caught, the captured stack trace will consist of every frame between where the exception was raised and the point at which it is captured (the "traceback"). If the passed exception was never raised, e.g. if you call `posthog.capture_exception(ValueError("Some Error"))`, the stack trace captured will be the full stack trace at the moment the exception was captured. Note that heavy use of contexts will lead to truncated stack traces, as the exception will be captured by the context entered most recently, which may not be the point you catch the exception for the final time in your code. It's recommended to use contexts sparingly, for this reason. `capture_exception` takes the same set of optional arguments as `capture`.
Examples:
```python
# Capture exception
from posthog import capture_exception
try:
risky_operation()
except Exception as e:
capture_exception(e)
```
Category:
Events
"""
return _proxy("capture_exception", exception=exception, **kwargs)
def feature_enabled(
key, # type: str
distinct_id, # type: str
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]
device_id=None, # type: Optional[str]
):
# type: (...) -> bool
"""
Use feature flags to enable or disable features for users.
Args:
key: The feature flag key
distinct_id: The user's distinct ID
groups: Groups mapping
person_properties: Person properties
group_properties: Group properties
only_evaluate_locally: Whether to evaluate only locally
send_feature_flag_events: Whether to send feature flag events
disable_geoip: Whether to disable GeoIP lookup
device_id: Optional device ID override for experience-continuity flags
Details:
You can call `posthog.load_feature_flags()` before to make sure you're not doing unexpected requests.
Examples:
```python
# 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')
```
Category:
Feature flags
"""
return _proxy(
"feature_enabled",
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,
device_id=device_id,
)
def get_feature_flag(
key, # type: str
distinct_id, # type: str
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]
device_id=None, # type: Optional[str]
) -> Optional[FeatureFlag]:
"""
Get feature flag variant for users. Used with experiments.
Args:
key: The feature flag key
distinct_id: The user's distinct ID
groups: Groups mapping from group type to group key
person_properties: Person properties
group_properties: Group properties in format { group_type_name: { group_properties } }
only_evaluate_locally: Whether to evaluate only locally
send_feature_flag_events: Whether to send feature flag events
disable_geoip: Whether to disable GeoIP lookup
device_id: Optional device ID override for experience-continuity flags
Details:
`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}}.
Examples:
```python
# 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')
```
Category:
Feature flags
"""
return _proxy(
"get_feature_flag",
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,
device_id=device_id,
)
def get_all_flags(
distinct_id, # type: str
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]
device_id=None, # type: Optional[str]
flag_keys_to_evaluate=None, # type: Optional[list[str]]
) -> Optional[dict[str, FeatureFlag]]:
"""
Get all flags for a given user.
Args:
distinct_id: The user's distinct ID
groups: Groups mapping
person_properties: Person properties
group_properties: Group properties
only_evaluate_locally: Whether to evaluate only locally
disable_geoip: Whether to disable GeoIP lookup
device_id: Optional device ID override for experience-continuity flags
flag_keys_to_evaluate: Optional list of flag keys to evaluate (evaluates all if None)
Details:
Flags are key-value pairs where the key is the flag key and the value is the flag variant, or True, or False.
Examples:
```python
# All flags for user
from posthog import get_all_flags
get_all_flags('distinct_id_of_your_user')
```
Category:
Feature flags
"""
return _proxy(
"get_all_flags",
distinct_id=distinct_id,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
disable_geoip=disable_geoip,
device_id=device_id,
flag_keys_to_evaluate=flag_keys_to_evaluate,
)
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]
device_id=None, # type: Optional[str]
):
# 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
Args:
key: The feature flag key.
distinct_id: The user's distinct ID.
groups: Mapping of group type to group key.
person_properties: Person properties to use for evaluation.
group_properties: Group properties keyed by group type.
only_evaluate_locally: Whether to evaluate only locally.
send_feature_flag_events: Whether to send a $feature_flag_called event.
disable_geoip: Whether to disable GeoIP lookup.
device_id: Optional device ID override for experience-continuity flags.
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,
device_id=device_id,
)
def get_feature_flag_payload(
key,
distinct_id,
match_value=None,
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]
device_id=None, # type: Optional[str]
) -> Optional[str]:
"""
Get the payload associated with a feature flag value.
Deprecated for new code. Prefer ``evaluate_flags()`` and
``flags.get_flag_payload(key)`` so flag evaluation happens once per request.
Args:
key: The feature flag key.
distinct_id: The user's distinct ID.
match_value: Optional flag value to use when selecting a payload.
groups: Mapping of group type to group key.
person_properties: Person properties to use for evaluation.
group_properties: Group properties keyed by group type.
only_evaluate_locally: Whether to evaluate only locally.
send_feature_flag_events: Whether to send a $feature_flag_called event.
disable_geoip: Whether to disable GeoIP lookup.
device_id: Optional device ID override for experience-continuity flags.
Category:
Feature flags
"""
return _proxy(
"get_feature_flag_payload",
key=key,
distinct_id=distinct_id,
match_value=match_value,
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,
device_id=device_id,
)
def get_remote_config_payload(
key, # type: str
):
"""Get the payload for a remote config feature flag.
Args:
key: The key of the feature flag
Returns:
The payload associated with the feature flag. If payload is encrypted, the return value will be decrypted
Note:
Requires personal_api_key to be set for authentication
"""
return _proxy(
"get_remote_config_payload",
key=key,
)
def get_all_flags_and_payloads(
distinct_id,
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]
device_id=None, # type: Optional[str]
flag_keys_to_evaluate=None, # type: Optional[list[str]]
) -> FlagsAndPayloads:
"""
Get all feature flag values and payloads for a user.
Args:
distinct_id: The user's distinct ID.
groups: Mapping of group type to group key.
person_properties: Person properties to use for evaluation.
group_properties: Group properties keyed by group type.
only_evaluate_locally: Whether to evaluate only locally.
disable_geoip: Whether to disable GeoIP lookup.
device_id: Optional device ID override for experience-continuity flags.
flag_keys_to_evaluate: Optional list of flag keys to evaluate. Evaluates
all flags when omitted.
Returns:
A dict with ``featureFlags`` and ``featureFlagPayloads`` entries.
Category:
Feature flags
"""
return _proxy(
"get_all_flags_and_payloads",
distinct_id=distinct_id,
groups=groups or {},
person_properties=person_properties or {},
group_properties=group_properties or {},
only_evaluate_locally=only_evaluate_locally,
disable_geoip=disable_geoip,
device_id=device_id,
flag_keys_to_evaluate=flag_keys_to_evaluate,
)
def evaluate_flags(
distinct_id=None, # type: Optional[str]
groups=None, # type: Optional[Dict[str, str]]
person_properties=None, # type: Optional[Dict[str, Any]]
group_properties=None, # type: Optional[Dict[str, Dict[str, Any]]]
only_evaluate_locally=False, # type: bool
disable_geoip=None, # type: Optional[bool]
flag_keys=None, # type: Optional[list]
device_id=None, # type: Optional[str]
) -> FeatureFlagEvaluations:
"""Evaluate all feature flags for a user in a single call and return a
:class:`FeatureFlagEvaluations` snapshot. Branch on ``.is_enabled()`` /
``.get_flag()`` and pass the same snapshot to ``capture()`` via the
``flags`` option so events carry the exact flag values the code branched on.
Prefer this over repeated ``get_feature_flag()`` calls and over
``capture(send_feature_flags=True)`` — it consolidates flag evaluation into
a single ``/flags`` request per incoming request.
Args:
distinct_id: The user's distinct ID. If ``None``, falls back to the context
distinct_id. If still unresolvable, returns an empty snapshot.
groups: Mapping of group type to group key.
person_properties: Person properties to use for evaluation.
group_properties: Group properties keyed by group type.
only_evaluate_locally: If ``True``, never fall back to remote evaluation.
disable_geoip: Whether to disable GeoIP lookup.
flag_keys: Optional list of flag keys. When provided, only these flags are
evaluated — the underlying ``/flags`` request asks the server for just
this subset, which makes the response smaller and the request cheaper.
Use this when you only need a handful of flags out of many.
device_id: Optional device ID override. If not provided, falls back to the
context device_id (which may be set via tracing headers). Used by
experience-continuity flags to match users across distinct_id changes.
Examples:
```python
from posthog import evaluate_flags, capture
flags = evaluate_flags("user_123", person_properties={"plan": "enterprise"})
if flags.is_enabled("new-dashboard"):
render_new_dashboard()
capture("page_viewed", distinct_id="user_123", flags=flags)
```
Category:
Feature flags
"""
return _proxy(
"evaluate_flags",
distinct_id=distinct_id,
groups=groups,
person_properties=person_properties,
group_properties=group_properties,
only_evaluate_locally=only_evaluate_locally,
disable_geoip=disable_geoip,
flag_keys=flag_keys,
device_id=device_id,
)
def feature_flag_definitions():
"""
Returns loaded feature flags.
Details:
Returns loaded feature flags, if any. Helpful for debugging what flag information you have loaded.
Examples:
```python
from posthog import feature_flag_definitions
definitions = feature_flag_definitions()
```
Category:
Feature flags
"""
return _proxy("feature_flag_definitions")
def load_feature_flags():
"""
Load feature flag definitions from PostHog.