|
33 | 33 | get, |
34 | 34 | remote_config, |
35 | 35 | ) |
36 | | -from posthog.scopes import get_tags |
| 36 | +from posthog.scopes import ( |
| 37 | + _get_current_context, |
| 38 | + get_context_distinct_id, |
| 39 | + get_context_session_id, |
| 40 | +) |
37 | 41 | from posthog.types import ( |
38 | 42 | FeatureFlag, |
39 | 43 | FeatureFlagResult, |
@@ -285,10 +289,17 @@ def identify( |
285 | 289 | stacklevel=2, |
286 | 290 | ) |
287 | 291 |
|
| 292 | + if distinct_id is None: |
| 293 | + distinct_id = get_context_distinct_id() |
| 294 | + |
288 | 295 | properties = properties or {} |
| 296 | + |
289 | 297 | require("distinct_id", distinct_id, ID_TYPES) |
290 | 298 | require("properties", properties, dict) |
291 | 299 |
|
| 300 | + if "$session_id" not in properties and get_context_session_id(): |
| 301 | + properties["$session_id"] = get_context_session_id() |
| 302 | + |
292 | 303 | msg = { |
293 | 304 | "timestamp": timestamp, |
294 | 305 | "distinct_id": distinct_id, |
@@ -358,6 +369,9 @@ def get_flags_decision( |
358 | 369 | """ |
359 | 370 | Get feature flags decision, using either flags() or decide() API based on rollout. |
360 | 371 | """ |
| 372 | + |
| 373 | + if distinct_id is None: |
| 374 | + distinct_id = get_context_distinct_id() |
361 | 375 | require("distinct_id", distinct_id, ID_TYPES) |
362 | 376 |
|
363 | 377 | if disable_geoip is None: |
@@ -406,14 +420,22 @@ def capture( |
406 | 420 |
|
407 | 421 | properties = {**(properties or {}), **system_context()} |
408 | 422 |
|
| 423 | + if "$session_id" not in properties and get_context_session_id(): |
| 424 | + properties["$session_id"] = get_context_session_id() |
| 425 | + |
| 426 | + if distinct_id is None: |
| 427 | + distinct_id = get_context_distinct_id() |
| 428 | + |
409 | 429 | require("distinct_id", distinct_id, ID_TYPES) |
410 | 430 | require("properties", properties, dict) |
411 | 431 | require("event", event, string_types) |
412 | 432 |
|
413 | | - # Grab current context tags, if any exist |
414 | | - context_tags = get_tags() |
415 | | - if context_tags: |
416 | | - properties.update(context_tags) |
| 433 | + current_context = _get_current_context() |
| 434 | + if current_context: |
| 435 | + context_tags = current_context.collect_tags() |
| 436 | + # We want explicitly passed properties to override context tags |
| 437 | + context_tags.update(properties) |
| 438 | + properties = context_tags |
417 | 439 |
|
418 | 440 | msg = { |
419 | 441 | "properties": properties, |
@@ -480,6 +502,9 @@ def set( |
480 | 502 | stacklevel=2, |
481 | 503 | ) |
482 | 504 |
|
| 505 | + if distinct_id is None: |
| 506 | + distinct_id = get_context_distinct_id() |
| 507 | + |
483 | 508 | properties = properties or {} |
484 | 509 | require("distinct_id", distinct_id, ID_TYPES) |
485 | 510 | require("properties", properties, dict) |
@@ -510,6 +535,9 @@ def set_once( |
510 | 535 | stacklevel=2, |
511 | 536 | ) |
512 | 537 |
|
| 538 | + if distinct_id is None: |
| 539 | + distinct_id = get_context_distinct_id() |
| 540 | + |
513 | 541 | properties = properties or {} |
514 | 542 | require("distinct_id", distinct_id, ID_TYPES) |
515 | 543 | require("properties", properties, dict) |
@@ -581,6 +609,9 @@ def alias( |
581 | 609 | stacklevel=2, |
582 | 610 | ) |
583 | 611 |
|
| 612 | + if distinct_id is None: |
| 613 | + distinct_id = get_context_distinct_id() |
| 614 | + |
584 | 615 | require("previous_id", previous_id, ID_TYPES) |
585 | 616 | require("distinct_id", distinct_id, ID_TYPES) |
586 | 617 |
|
@@ -613,6 +644,9 @@ def page( |
613 | 644 | stacklevel=2, |
614 | 645 | ) |
615 | 646 |
|
| 647 | + if distinct_id is None: |
| 648 | + distinct_id = get_context_distinct_id() |
| 649 | + |
616 | 650 | properties = properties or {} |
617 | 651 | require("distinct_id", distinct_id, ID_TYPES) |
618 | 652 | require("properties", properties, dict) |
@@ -648,6 +682,9 @@ def capture_exception( |
648 | 682 | stacklevel=2, |
649 | 683 | ) |
650 | 684 |
|
| 685 | + if distinct_id is None: |
| 686 | + distinct_id = get_context_distinct_id() |
| 687 | + |
651 | 688 | # this function shouldn't ever throw an error, so it logs exceptions instead of raising them. |
652 | 689 | # this is important to ensure we don't unexpectedly re-raise exceptions in the user's code. |
653 | 690 | try: |
|
0 commit comments