Skip to content

Commit 098b5c5

Browse files
committed
add tags to all events
1 parent 933e20e commit 098b5c5

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@
104104
print(posthog.get_remote_config_payload("encrypted_payload_flag_key"))
105105

106106

107-
# You can add tags to a context, and these are automatically added to exceptions captured within that context.
107+
# You can add tags to a context, and these are automatically added to exceptions and other events captured
108+
# within that context.
109+
108110
# You can enter a new context using a with statement. Any exceptions thrown in the context will be captured,
109-
# and tagged with the context tags. By default, it inherits tags from the parent context.
111+
# and tagged with the context tags. Other events captured will also be tagged with the context tags. By default,
112+
# the new context inherits tags from the parent context.
110113
with posthog.new_context():
111-
posthog.tag("user_id", 123)
112114
posthog.tag("transaction_id", "abc123")
113115
posthog.tag("some_arbitrary_value", {"tags": "can be dicts"})
116+
117+
# This event will be captured with the tags set above
118+
posthog.capture("order_processed")
114119
# This exception will be captured with the tags set above
115120
raise Exception("Order processing failed")
116121

posthog/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ def capture(
467467
require("properties", properties, dict)
468468
require("event", event, string_types)
469469

470+
# Grab current context tags, if any exist
471+
context_tags = get_tags()
472+
if context_tags:
473+
properties.update(context_tags)
474+
470475
msg = {
471476
"properties": properties,
472477
"timestamp": timestamp,
@@ -668,11 +673,6 @@ def capture_exception(
668673
self.log.debug("Exception already captured, skipping")
669674
return
670675

671-
# Grab current context tags, if any exist
672-
context_tags = get_tags()
673-
if context_tags:
674-
properties.update(context_tags)
675-
676676
# if there's no distinct_id, we'll generate one and set personless mode
677677
# via $process_person_profile = false
678678
if distinct_id is None:

posthog/scopes.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,29 @@ def new_context(fresh=False):
1414
# TODO - we could extend this context idea to also apply to other event types eventually,
1515
# but right now it only applies to exceptions...
1616
"""
17-
Create a new context scope that will be active for the duration of the with block.
18-
Any tags set within this scope will be isolated to this context. Any exceptions raised
19-
within the context will be captured and tagged with the context tags.
20-
21-
Args:
22-
fresh: Whether to start with a fresh context (default: False).
23-
If False, inherits tags from parent context.
24-
If True, starts with no tags.
25-
26-
Examples:
27-
# Inherit parent context tags
28-
with posthog.new_context():
29-
posthog.tag("user_id", "123")
30-
raise ValueError("Something went wrong")
31-
32-
# Start with fresh context (no inherited tags)
33-
with posthog.new_context(fresh=True):
34-
posthog.tag("user_id", "123")
35-
raise ValueError("Something went wrong")
17+
Create a new context scope that will be active for the duration of the with block.
18+
Any tags set within this scope will be isolated to this context. Any exceptions raised
19+
or events captured within the context will be tagged with the context tags.
20+
21+
Args:
22+
fresh: Whether to start with a fresh context (default: False).
23+
If False, inherits tags from parent context.
24+
If True, starts with no tags.
25+
26+
Examples:
27+
# Inherit parent context tags
28+
with posthog.new_context():
29+
posthog.tag("request_id", "123")
30+
# Both this event and the exception will be tagged with the context tags
31+
posthog.capture("event_name", {"property": "value"})
32+
raise ValueError("Something went wrong")
33+
34+
# Start with fresh context (no inherited tags)
35+
with posthog.new_context(fresh=True):
36+
posthog.tag("request_id", "123")
37+
# Both this event and the exception will be tagged with the context tags
38+
posthog.capture("event_name", {"property": "value"})
39+
raise ValueError("Something went wrong")
3640
3741
"""
3842
import posthog
@@ -86,9 +90,8 @@ def clear_tags() -> None:
8690

8791
def scoped(fresh=False):
8892
"""
89-
Decorator that creates a new context for the function, wraps the function in a
90-
try/except block, and if an exception occurs, captures it with the current context
91-
tags before re-raising it.
93+
Decorator that creates a new context for the function. Simply wraps
94+
the function in a with posthog.new_context(): block.
9295
9396
Args:
9497
fresh: Whether to start with a fresh context (default: False)
@@ -98,8 +101,12 @@ def scoped(fresh=False):
98101
def process_payment(payment_id):
99102
posthog.tag("payment_id", payment_id)
100103
posthog.tag("payment_method", "credit_card")
104+
105+
# This event will be captured with tags
106+
posthog.capture("payment_started")
101107
# If this raises an exception, it will be captured with tags
102108
# and then re-raised
109+
some_risky_function()
103110
"""
104111

105112
def decorator(func: F) -> F:

0 commit comments

Comments
 (0)