diff --git a/.gitignore b/.gitignore index f3bbd97c..15f849be 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ posthog-analytics pyrightconfig.json .env .DS_Store +posthog-python-references.json diff --git a/bin/docs b/bin/docs new file mode 100755 index 00000000..9943b4c0 --- /dev/null +++ b/bin/docs @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +#/ Usage: bin/docs +#/ Description: Generate documentation for the PostHog Python SDK +source bin/helpers/_utils.sh +set_source_and_root_dir +ensure_virtual_env + +exec python3 "$(dirname "$0")/docs_scripts/generate_json_schemas.py" "$@" \ No newline at end of file diff --git a/bin/docs_scripts/doc_constant.py b/bin/docs_scripts/doc_constant.py new file mode 100644 index 00000000..c3179bb2 --- /dev/null +++ b/bin/docs_scripts/doc_constant.py @@ -0,0 +1,81 @@ +""" +Constants for PostHog Python SDK documentation generation. +""" + +from typing import Dict, Union + +# Types that are built-in to Python and don't need to be documented +NO_DOCS_TYPES = [ + "Client", + "any", + "int", + "float", + "bool", + "dict", + "list", + "str", + "tuple", + "set", + "frozenset", + "bytes", + "bytearray", + "memoryview", + "range", + "slice", + "complex", + "Union", + "Optional", + "Any", + "Callable", + "Type", + "TypeVar", + "Generic", + "Literal", + "ClassVar", + "Final", + "Annotated", + "NotRequired", + "Required", + "None", + "NoneType", + "object", + "Unpack", + "BaseException", + "Exception", +] + +# Documentation generation metadata +DOCUMENTATION_METADATA = { + "hogRef": "0.1", + "slugPrefix": "posthog-python", + "specUrl": "https://github.com/PostHog/posthog-python", +} + +# Docstring parsing patterns for new format +DOCSTRING_PATTERNS = { + "examples_section": r"Examples:\s*\n(.*?)(?=\n\s*\n\s*Category:|\Z)", + "args_section": r"Args:\s*\n(.*?)(?=\n\s*\n\s*Examples:|\n\s*\n\s*Details:|\n\s*\n\s*Category:|\Z)", + "details_section": r"Details:\s*\n(.*?)(?=\n\s*\n\s*Examples:|\n\s*\n\s*Category:|\Z)", + "category_section": r"Category:\s*\n\s*(.+?)\s*(?:\n|$)", + "code_block": r"```(?:python)?\n(.*?)```", + "param_description": r"^\s*{param_name}:\s*(.+?)(?=\n\s*\w+:|\Z)", + "args_marker": r"\n\s*Args:\s*\n", + "examples_marker": r"\n\s*Examples:\s*\n", + "details_marker": r"\n\s*Details:\s*\n", + "category_marker": r"\n\s*Category:\s*\n", +} + +# Output file configuration +OUTPUT_CONFIG: Dict[str, Union[str, int]] = { + "output_dir": ".", + "filename": "posthog-python-references.json", + "indent": 2, +} + +# Documentation structure defaults +DOC_DEFAULTS = { + "showDocs": True, + "releaseTag": "public", + "return_type_void": "None", + "max_optional_params": 3, +} diff --git a/bin/docs_scripts/generate_json_schemas.py b/bin/docs_scripts/generate_json_schemas.py new file mode 100644 index 00000000..448d123f --- /dev/null +++ b/bin/docs_scripts/generate_json_schemas.py @@ -0,0 +1,472 @@ +#!/usr/bin/env python3 +""" +Generate comprehensive SDK documentation JSON from PostHog Python SDK. +This script inspects the code and docstrings to create documentation in the specified format. +""" + +import json +import inspect +import re +from dataclasses import is_dataclass, fields +from typing import get_origin, get_args, Union +from textwrap import dedent +from doc_constant import ( + NO_DOCS_TYPES, + DOCUMENTATION_METADATA, + DOCSTRING_PATTERNS, + OUTPUT_CONFIG, + DOC_DEFAULTS, +) +import os + + +def extract_examples_from_docstring(docstring: str) -> list: + """Extract code examples from docstring.""" + if not docstring: + return [] + + examples = [] + + # Look for Examples section in the new format + examples_section_match = re.search( + DOCSTRING_PATTERNS["examples_section"], docstring, re.DOTALL + ) + if examples_section_match: + examples_content = examples_section_match.group(1).strip() + # Extract code blocks from the Examples section + code_blocks = re.findall( + DOCSTRING_PATTERNS["code_block"], examples_content, re.DOTALL + ) + for i, code_block in enumerate(code_blocks): + # Remove common leading whitespace while preserving relative indentation + code = dedent(code_block).strip() + + # Extract name from first comment line if present + lines = code.split("\n") + name = f"Example {i + 1}" # Default fallback + + if lines and lines[0].strip().startswith("#"): + # Extract name from first comment, keep the comment in the code + comment_text = lines[0].strip()[1:].strip() + if comment_text: + name = comment_text + + examples.append({"id": f"example_{i + 1}", "name": name, "code": code}) + + return examples + + +def extract_details_from_docstring(docstring: str) -> str: + """Extract details section from docstring.""" + if not docstring: + return "" + + # Look for Details section + details_match = re.search( + DOCSTRING_PATTERNS["details_section"], docstring, re.DOTALL + ) + if details_match: + details_content = details_match.group(1).strip() + # Clean up formatting + return details_content.replace("\n", " ") + + return "" + + +def parse_docstring_tags(docstring: str) -> dict: + """Parse tags from docstring Category section.""" + if not docstring: + return {} + + tags = {} + + # Extract Category section + category_match = re.search(DOCSTRING_PATTERNS["category_section"], docstring) + if category_match: + category_value = category_match.group(1).strip() + tags["category"] = category_value + + return tags + + +def extract_description_from_docstring(docstring: str) -> str: + """Extract main description from docstring.""" + if not docstring: + return "" + + # Clean up the docstring + cleaned = dedent(docstring).strip() + + # Find the end of the description by looking for first section marker + # Check for Args:, Examples:, Details:, or Category: sections + section_patterns = [ + DOCSTRING_PATTERNS["args_marker"], + DOCSTRING_PATTERNS["examples_marker"], + DOCSTRING_PATTERNS["details_marker"], + DOCSTRING_PATTERNS["category_marker"], + ] + + end_pos = len(cleaned) + for pattern in section_patterns: + match = re.search(pattern, cleaned) + if match: + end_pos = min(end_pos, match.start()) + + # Extract description up to the first section marker + description = cleaned[:end_pos].strip() + + # Remove one level of \n since it will be rendered as markdown + # and \n will be padded in later steps + description = description.replace("\n", " ") + + return description + + +def get_type_name(type_annotation) -> str: + """Convert type annotation to string name.""" + if type_annotation is None or type_annotation is type(None): + return "any" + + # Handle typing constructs + origin = get_origin(type_annotation) + if origin is not None: + # Handle Union types (including Optional) + if origin is Union: + args = get_args(type_annotation) + if len(args) == 2 and type(None) in args: + # This is Optional[Type] - get the non-None type + non_none_type = next(arg for arg in args if arg is not type(None)) + return f"Optional[{get_type_name(non_none_type)}]" + else: + # Regular Union - list all types + type_names = [get_type_name(arg) for arg in args] + return f"Union[{', '.join(type_names)}]" + + # Handle other generic types (List, Dict, etc.) + origin_name = getattr(origin, "__name__", str(origin)) + args = get_args(type_annotation) + if args: + arg_names = [get_type_name(arg) for arg in args] + return f"{origin_name}[{', '.join(arg_names)}]" + else: + return origin_name + + # Handle regular types + elif hasattr(type_annotation, "__name__"): + return type_annotation.__name__ + else: + return str(type_annotation) + + +def analyze_parameter(param: inspect.Parameter, docstring: str = "") -> dict: + """Analyze a function parameter and return its documentation.""" + # Determine if parameter is optional (has default value) + is_optional = param.default == inspect.Parameter.empty + + # Get the type annotation + type_annotation = param.annotation + param_type = "any" + + if type_annotation != inspect.Parameter.empty: + # Handle Union/Optional types first + origin = get_origin(type_annotation) + if origin is Union: + args = get_args(type_annotation) + if len(args) == 2 and type(None) in args: + # This is Optional[Type] + non_none_type = next(arg for arg in args if arg is not type(None)) + param_type = get_type_name(non_none_type) + is_optional = True + else: + # Other Union types, use first type + param_type = get_type_name(args[0]) if args else "any" + else: + param_type = get_type_name(type_annotation) + elif param.default != inspect.Parameter.empty: + # No type annotation, but has default value - infer type from default + param_type = get_type_name(type(param.default)) + + # Extract parameter description from Args section + param_description = f"Parameter: {param.name}" + if docstring: + # Look for Args section and extract description for this parameter + args_section_match = re.search( + DOCSTRING_PATTERNS["args_section"], docstring, re.DOTALL + ) + if args_section_match: + args_content = args_section_match.group(1) + # Look for the parameter description + param_pattern = DOCSTRING_PATTERNS["param_description"].format( + param_name=re.escape(param.name) + ) + param_match = re.search( + param_pattern, args_content, re.MULTILINE | re.DOTALL + ) + if param_match: + param_description = param_match.group(1).strip().replace("\n", " ") + + param_info = { + "name": param.name, + "description": param_description, + "isOptional": is_optional, + "type": param_type, + } + + return param_info + + +def analyze_function(func, name: str) -> dict: + """Analyze a function and return its documentation.""" + try: + sig = inspect.signature(func) + docstring = inspect.getdoc(func) or "" + + # Skip functions with empty docstrings + if not docstring.strip(): + return {} + + # Extract parameters (excluding 'self') + params = [] + for param_name, param in sig.parameters.items(): + if param_name != "self": + params.append(analyze_parameter(param, docstring)) + + # Special handling for constructor + display_name = name + if name == "__init__": + display_name = func.__qualname__.split(".")[0] + + # Parse tags from docstring + tags = parse_docstring_tags(docstring) + + category = tags.get("category", None) + + # Extract description + description = extract_description_from_docstring(docstring) + + # Skip if no meaningful description + if not description.strip(): + return {} + + # Extract details section (only if it exists) + details = extract_details_from_docstring(docstring) + + # Get examples from docstring, do not generate fallback examples + examples = extract_examples_from_docstring(docstring) + # If no examples, do not include the examples key or set to empty list + + result = { + "id": name, + "title": display_name, + "description": description, + "details": details, + "category": category, + "params": params, + "showDocs": DOC_DEFAULTS["showDocs"], + "releaseTag": DOC_DEFAULTS["releaseTag"], + "returnType": { + "id": "return_type", + "name": get_type_name(sig.return_annotation) + if sig.return_annotation != inspect.Signature.empty + else DOC_DEFAULTS["return_type_void"], + }, + } + if examples: + result["examples"] = examples + return result + except Exception as e: + print(f"Error analyzing function {name}: {e}") + return {} + + +def analyze_class(cls) -> dict: + """Analyze a class and return its documentation.""" + class_doc = inspect.getdoc(cls) or f"Class: {cls.__name__}" + + # Get all public methods and constructor + functions = [] + for method_name in dir(cls): + if method_name.startswith("_") and method_name != "__init__": + continue + + method = getattr(cls, method_name) + if callable(method): + func_info = analyze_function(method, method_name) + if func_info: # Only add if not None (empty docstring check) + functions.append(func_info) + + return { + "id": cls.__name__, + "title": cls.__name__, + "description": extract_description_from_docstring(class_doc), + "functions": functions, + } + + +def analyze_type(cls) -> dict: + """Analyze a type/dataclass and return its documentation.""" + type_info = { + "id": cls.__name__, + "name": cls.__name__, + "path": f"{cls.__module__}.{cls.__name__}", + "properties": [], + "example": "", + } + + if is_dataclass(cls): + # Handle dataclass + for field in fields(cls): + prop = { + "name": field.name, + "type": get_type_name(field.type), + "description": f"Field: {field.name}", + } + type_info["properties"].append(prop) + elif hasattr(cls, "__annotations__"): + # Handle TypedDict or annotated class + for field_name, field_type in cls.__annotations__.items(): + prop = { + "name": field_name, + "type": get_type_name(field_type), + "description": f"Field: {field_name}", + } + type_info["properties"].append(prop) + + return type_info + + +def generate_sdk_documentation(): + """Generate complete SDK documentation in the requested format.""" + + # Import PostHog components + import posthog + from posthog.client import Client + import posthog.types as types_module + import posthog.args as args_module + from posthog.version import VERSION + + # Main SDK info + sdk_info = { + "version": VERSION, + "id": "posthog-python", + "title": "PostHog Python SDK", + "description": "Integrate PostHog into any python application.", + "slugPrefix": DOCUMENTATION_METADATA["slugPrefix"], + "specUrl": DOCUMENTATION_METADATA["specUrl"], + } + + # Collect types + types_list = [] + + # Types from posthog.types + for name in dir(types_module): + obj = getattr(types_module, name) + if inspect.isclass(obj) and not name.startswith("_"): + try: + type_info = analyze_type(obj) + types_list.append(type_info) + except Exception as e: + print(f"Error analyzing type {name}: {e}") + + # Types from posthog.args + for name in dir(args_module): + obj = getattr(args_module, name) + if inspect.isclass(obj) and not name.startswith("_"): + try: + type_info = analyze_type(obj) + types_list.append(type_info) + except Exception as e: + print(f"Error analyzing type {name}: {e}") + + # Collect classes + classes_list = [] + + # Main PostHog class (renamed from Client) + client_class = analyze_class(Client) + client_class["id"] = "PostHog" + client_class["title"] = "PostHog" + classes_list.append(client_class) + + # Global module functions (functions callable as posthog.function_name) + global_functions = [] + for func_name in dir(posthog): + # Skip private functions and non-callables + if func_name.startswith("_") or not callable(getattr(posthog, func_name)): + continue + + func = getattr(posthog, func_name) + # Only include functions actually defined in the posthog module (not imported) + # and exclude class references + if ( + func_name not in ["Client", "Posthog"] + and hasattr(func, "__module__") + and func.__module__ == "posthog" + ): + try: + func_info = analyze_function(func, func_name) + if func_info: # Only add if not None (has proper docstring) + global_functions.append(func_info) + except Exception: + continue + + # Add global functions as a "class" + if global_functions: + classes_list.append( + { + "id": "PostHogModule", + "title": "PostHog Module Functions", + "description": "Global functions available in the PostHog module", + "functions": global_functions, + } + ) + + # Create the final structure + result = { + "id": "posthog-python", + "hogRef": DOCUMENTATION_METADATA["hogRef"], + "info": sdk_info, + "noDocsTypes": NO_DOCS_TYPES, + "types": types_list, + "classes": classes_list, + } + + return result + + +if __name__ == "__main__": + print("Generating PostHog Python SDK documentation...") + + try: + documentation = generate_sdk_documentation() + + # Write to file + output_file = os.path.join( + str(OUTPUT_CONFIG["output_dir"]), str(OUTPUT_CONFIG["filename"]) + ) + with open(output_file, "w") as f: + json.dump(documentation, f, indent=int(OUTPUT_CONFIG["indent"])) + + print(f"✓ Generated {output_file}") + + # Print summary + types_count = len(documentation["types"]) + classes_count = len(documentation["classes"]) + + total_functions = sum(len(cls["functions"]) for cls in documentation["classes"]) + + print("📊 Documentation Summary:") + print(f" • {types_count} types documented") + print(f" • {classes_count} classes documented") + print(f" • {total_functions} functions documented") + + no_docs = documentation["noDocsTypes"] + if no_docs: + print( + f" • {len(no_docs)} types without documentation: {', '.join(no_docs[:5])}{'...' if len(no_docs) > 5 else ''}" + ) + + except Exception as e: + print(f"❌ Error generating documentation: {e}") + import traceback + + traceback.print_exc() diff --git a/posthog/__init__.py b/posthog/__init__.py index 9fd6160b..a9e4a8ed 100644 --- a/posthog/__init__.py +++ b/posthog/__init__.py @@ -20,22 +20,105 @@ def new_context(fresh=False, capture_exceptions=True): + """ + 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) + + 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) 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 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 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) @@ -70,40 +153,62 @@ def tag(name: str, value: Any): # versions, without a breaking change, to get back the type information in function signatures def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]: """ - 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 - - `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. - - For example: - ```python - # Enter a new context (e.g. a request/response cycle, an instance of a background job, etc) - with posthog.new_context(): - # Associate this context with some user, by distinct_id - posthog.identify_context('some user') - - # Capture an event, associated with the context-level distinct ID ('some user') - posthog.capture('movie started') + Capture anything a user does within your system. - # Capture an event associated with some other user (overriding the context-level distinct ID) - posthog.capture('movie joined', distinct_id='some-other-user') - - # Capture an event with some properties - posthog.capture('movie played', properties={'movie_id': '123', 'category': 'romcom'}) - - # Capture an event with some properties - posthog.capture('purchase', properties={'product_id': '123', 'category': 'romcom'}) - # Capture an event with some associated group - posthog.capture('purchase', groups={'company': 'id:5'}) - - # Adding a tag to the current context will cause it to appear on all subsequent events - posthog.tag_context('some-tag', 'some-value') - - posthog.capture('another-event') # Will be captured with `'some-tag': 'some-value'` in the properties dict - ``` + 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 + groups: Dict of group types and IDs + 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) @@ -112,21 +217,25 @@ def capture(event: str, **kwargs: Unpack[OptionalCaptureArgs]) -> Optional[str]: def set(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: """ Set properties on a user record. - 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). - For example: - ```python - posthog.set(distinct_id='distinct id', properties={ - 'current_browser': 'Chrome', - }) - ``` + 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 capture + capture( + 'distinct_id', + event='event_name', + properties={ + '$set': {'name': 'Max Hedgehog'}, + '$set_once': {'initial_url': '/blog'} + } + ) + ``` + Category: + Identification """ return _proxy("set", **kwargs) @@ -135,10 +244,26 @@ def set(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: def set_once(**kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: """ Set properties on a user record, only if they do not yet exist. - This will not overwrite previous people property values, unlike `set`. - Otherwise, operates in an identical manner to `set`. - ``` + 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 capture + capture( + 'distinct_id', + event='event_name', + properties={ + '$set': {'name': 'Max Hedgehog'}, + '$set_once': {'initial_url': '/blog'} + } + ) + + ``` + Category: + Identification """ return _proxy("set_once", **kwargs) @@ -153,18 +278,27 @@ def group_identify( ): # type: (...) -> Optional[str] """ - Set properties on a group - - A `group_identify` call requires - - `group_type` type of your group - - `group_key` unique identifier of the group + Set properties on a group. - For example: - ```python - posthog.group_identify('company', 5, { - 'employees': 11, - }) - ``` + 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 + + 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( @@ -187,19 +321,26 @@ def alias( ): # type: (...) -> Optional[str] """ - 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. + Associate user behaviour before and after they e.g. register, login, or perform some other identifying action. - An `alias` call requires - - `previous distinct id` the unique ID of the user before - - `distinct id` the current unique id - - For example: - ```python - posthog.alias('anonymous session id', 'distinct id') - ``` + 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( @@ -217,26 +358,25 @@ def capture_exception( **kwargs: Unpack[OptionalCaptureArgs], ): """ - capture_exception allows you to capture exceptions that happen in your code. - - 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) - - A `capture_exception` call does not require any fields, but we recommend passing an exception of some kind: - - `exception` to specify the exception to capture. If not provided, the current exception is captured via `sys.exc_info()` - - 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 exceptions that happen in your code. - `capture_exception` takes the same set of optional arguments as `capture`. + Args: + exception: The exception to capture. If not provided, the current exception is captured via `sys.exc_info()` + + 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) @@ -256,15 +396,29 @@ def feature_enabled( """ Use feature flags to enable or disable features for users. - For example: - ```python - if posthog.feature_enabled('beta feature', 'distinct id'): - # do something - if posthog.feature_enabled('groups feature', 'distinct id', groups={"organization": "5"}): - # do something - ``` - - You can call `posthog.load_feature_flags()` before to make sure you're not doing unexpected requests. + 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 + + 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", @@ -291,25 +445,30 @@ def get_feature_flag( ) -> Optional[FeatureFlag]: """ Get feature flag variant for users. Used with experiments. - Example: - ```python - if posthog.get_feature_flag('beta-feature', 'distinct_id') == 'test-variant': - # do test variant code - if posthog.get_feature_flag('beta-feature', 'distinct_id') == 'control': - # do control code - ``` - - `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: - - ```python - group_properties={"organization": {"name": "PostHog", "employees": 11}} - ``` + 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 + + 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", @@ -334,12 +493,26 @@ def get_all_flags( ) -> Optional[dict[str, FeatureFlag]]: """ Get all flags for a given user. - Example: - ```python - flags = posthog.get_all_flags('distinct_id') - ``` - flags are key-value pairs where the key is the flag key and the value is the flag variant, or True, or False. + 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 + + 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", @@ -417,27 +590,85 @@ def get_all_flags_and_payloads( def feature_flag_definitions(): - """Returns loaded feature flags, if any. Helpful for debugging what flag information you have loaded.""" + """ + 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.""" + """ + Load feature flag definitions from PostHog. + + Examples: + ```python + from posthog import load_feature_flags + load_feature_flags() + ``` + + Category: + Feature flags + """ return _proxy("load_feature_flags") def flush(): - """Tell the client to flush.""" + """ + Tell the client to flush all queued events. + + Examples: + ```python + from posthog import flush + flush() + ``` + + Category: + Client management + """ _proxy("flush") def join(): - """Block program until the client clears the queue""" + """ + Block program until the client clears the queue. Used during program shutdown. You should use `shutdown()` directly in most cases. + + Examples: + ```python + from posthog import join + join() + ``` + + Category: + Client management + """ _proxy("join") def shutdown(): - """Flush all messages and cleanly shutdown the client""" + """ + Flush all messages and cleanly shutdown the client. + + Examples: + ```python + from posthog import shutdown + shutdown() + ``` + + Category: + Client management + """ _proxy("flush") _proxy("join") diff --git a/posthog/client.py b/posthog/client.py index 76134faf..3dbd5189 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -97,29 +97,20 @@ def add_context_tags(properties): class Client(object): - """Create a new PostHog client. + """ + This is the SDK reference for the PostHog Python SDK. + You can learn more about example usage in the [Python SDK documentation](/docs/libraries/python). + You can also follow [Flask](/docs/libraries/flask) and [Django](/docs/libraries/django) + guides to integrate PostHog into your project. Examples: - Basic usage: - >>> client = Client("your-api-key") - - With memory-based feature flag fallback cache: - >>> client = Client( - ... "your-api-key", - ... flag_fallback_cache_url="memory://local/?ttl=300&size=10000" - ... ) - - With Redis fallback cache for high-scale applications: - >>> client = Client( - ... "your-api-key", - ... flag_fallback_cache_url="redis://localhost:6379/0/?ttl=300" - ... ) - - With Redis authentication: - >>> client = Client( - ... "your-api-key", - ... flag_fallback_cache_url="redis://username:password@localhost:6379/0/?ttl=300" - ... ) + ```python + from posthog import Posthog + posthog = Posthog('', host='') + posthog.debug = True + if settings.TEST: + posthog.disabled = True + ``` """ log = logging.getLogger("posthog") @@ -153,6 +144,24 @@ def __init__( before_send=None, flag_fallback_cache_url=None, ): + """ + Initialize a new PostHog client instance. + + Args: + project_api_key: The project API key. + host: The host to use for the client. + debug: Whether to enable debug mode. + + Examples: + ```python + from posthog import Posthog + + posthog = Posthog('', host='') + ``` + + Category: + Initialization + """ self.queue = queue.Queue(max_queue_size) # api_key: This should be the Team API Key (token), public @@ -250,6 +259,23 @@ def __init__( consumer.start() def new_context(self, fresh=False, capture_exceptions=True): + """ + Create a new context for managing shared state. Learn more about [contexts](/docs/libraries/python#contexts). + + Args: + fresh: Whether to create a fresh context that doesn't inherit from parent. + capture_exceptions: Whether to automatically capture exceptions in this context. + + Examples: + ```python + with posthog.new_context(): + identify_context('') + posthog.capture('event_name') + ``` + + Category: + Contexts + """ return new_context( fresh=fresh, capture_exceptions=capture_exceptions, client=self ) @@ -285,7 +311,17 @@ def get_feature_variants( disable_geoip=None, ) -> dict[str, Union[bool, str]]: """ - Get feature flag variants for a distinct_id by calling decide. + Get feature flag variants for a user by calling decide. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + disable_geoip: Whether to disable GeoIP for this request. + + Category: + Feature Flags """ resp_data = self.get_flags_decision( distinct_id, groups, person_properties, group_properties, disable_geoip @@ -301,7 +337,22 @@ def get_feature_payloads( disable_geoip=None, ) -> dict[str, str]: """ - Get feature flag payloads for a distinct_id by calling decide. + Get feature flag payloads for a user by calling decide. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + payloads = posthog.get_feature_payloads('') + ``` + + Category: + Feature Flags """ resp_data = self.get_flags_decision( distinct_id, groups, person_properties, group_properties, disable_geoip @@ -317,7 +368,22 @@ def get_feature_flags_and_payloads( disable_geoip=None, ) -> FlagsAndPayloads: """ - Get feature flags and payloads for a distinct_id by calling decide. + Get feature flags and payloads for a user by calling decide. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + result = posthog.get_feature_flags_and_payloads('') + ``` + + Category: + Feature Flags """ resp = self.get_flags_decision( distinct_id, groups, person_properties, group_properties, disable_geoip @@ -333,7 +399,22 @@ def get_flags_decision( disable_geoip=None, ) -> FlagsResponse: """ - Get feature flags decision, using either flags() or decide() API based on rollout. + Get feature flags decision. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + decision = posthog.get_flags_decision('user123') + ``` + + Category: + Feature Flags """ if distinct_id is None: @@ -365,6 +446,52 @@ def get_flags_decision( def capture( self, event: str, **kwargs: Unpack[OptionalCaptureArgs] ) -> Optional[str]: + """ + Captures an event manually. [Learn about capture best practices](https://posthog.com/docs/product-analytics/capture-events) + + Args: + event: The event name to capture. + distinct_id: The distinct ID of the user. + properties: A dictionary of properties to include with the event. + timestamp: The timestamp of the event. + uuid: A unique identifier for the event. + groups: A dictionary of group information. + send_feature_flags: Whether to send feature flags with the event. + disable_geoip: Whether to disable GeoIP for this event. + + Examples: + ```python + # Anonymous event + posthog.capture('some-anon-event') + ``` + ```python + # Context usage + from posthog import identify_context, new_context + with new_context(): + identify_context('distinct_id_of_the_user') + posthog.capture('user_signed_up') + posthog.capture('user_logged_in') + posthog.capture('some-custom-action', distinct_id='distinct_id_of_the_user') + ``` + ```python + # Set event properties + posthog.capture( + "user_signed_up", + distinct_id="distinct_id_of_the_user", + properties={ + "login_type": "email", + "is_free_trial": "true" + } + ) + ``` + ```python + # Page view event + posthog.capture('$pageview', distinct_id="distinct_id_of_the_user", properties={'$current_url': 'https://example.com'}) + ``` + + Category: + Capture + """ distinct_id = kwargs.get("distinct_id", None) properties = kwargs.get("properties", None) timestamp = kwargs.get("timestamp", None) @@ -431,6 +558,39 @@ def capture( return self._enqueue(msg, disable_geoip) def set(self, **kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: + """ + Set properties on a person profile. + + Args: + distinct_id: The distinct ID of the user. + properties: A dictionary of properties to set. + timestamp: The timestamp of the event. + uuid: A unique identifier for the event. + disable_geoip: Whether to disable GeoIP for this event. + + Examples: + ```python + # Set with distinct id + posthog.capture( + 'event_name', + distinct_id='user-distinct-id', + properties={ + '$set': {'name': 'Max Hedgehog'}, + '$set_once': {'initial_url': '/blog'} + } + ) + ``` + ```python + # Set using context + from posthog import new_context, identify_context + with new_context(): + identify_context('user-distinct-id') + posthog.capture('event_name') + ``` + + Category: + Identification + """ distinct_id = kwargs.get("distinct_id", None) properties = kwargs.get("properties", None) timestamp = kwargs.get("timestamp", None) @@ -457,6 +617,24 @@ def set(self, **kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: return self._enqueue(msg, disable_geoip) def set_once(self, **kwargs: Unpack[OptionalSetArgs]) -> Optional[str]: + """ + Set properties on a person profile only if they haven't been set before. + + Args: + distinct_id: The distinct ID of the user. + properties: A dictionary of properties to set once. + timestamp: The timestamp of the event. + uuid: A unique identifier for the event. + disable_geoip: Whether to disable GeoIP for this event. + + Examples: + ```python + posthog.set_once(distinct_id='user123', properties={'initial_signup_date': '2024-01-01'}) + ``` + + Category: + Identification + """ distinct_id = kwargs.get("distinct_id", None) properties = kwargs.get("properties", None) timestamp = kwargs.get("timestamp", None) @@ -485,18 +663,41 @@ def group_identify( self, group_type: str, group_key: str, - properties=None, - timestamp=None, - uuid=None, - disable_geoip=None, - distinct_id=None, - ): + properties: Optional[Dict[str, Any]] = None, + timestamp: Optional[Union[datetime, str]] = None, + uuid: Optional[str] = None, + disable_geoip: Optional[bool] = None, + distinct_id: Optional[ID_TYPES] = None, + ) -> Optional[str]: + """ + Identify a group and set its properties. + + Args: + group_type: The type of group (e.g., 'company', 'team'). + group_key: The unique identifier for the group. + properties: A dictionary of properties to set on the group. + timestamp: The timestamp of the event. + uuid: A unique identifier for the event. + disable_geoip: Whether to disable GeoIP for this event. + distinct_id: The distinct ID of the user performing the action. + + Examples: + ```python + posthog.group_identify('company', 'company_id_in_your_db', { + 'name': 'Awesome Inc.', + 'employees': 11 + }) + ``` + + Category: + Identification + """ properties = properties or {} # group_identify is purposefully always personful distinct_id = get_identity_state(distinct_id)[0] - msg = { + msg: Dict[str, Any] = { "event": "$groupidentify", "properties": { "$group_type": group_type, @@ -510,7 +711,7 @@ def group_identify( # NOTE - group_identify doesn't generally use context properties - should it? if get_context_session_id(): - msg["properties"]["$session_id"] = get_context_session_id() + msg["properties"]["$session_id"] = str(get_context_session_id()) return self._enqueue(msg, disable_geoip) @@ -522,6 +723,24 @@ def alias( uuid=None, disable_geoip=None, ): + """ + Create an alias between two distinct IDs. + + Args: + previous_id: The previous distinct ID. + distinct_id: The new distinct ID to alias to. + timestamp: The timestamp of the event. + uuid: A unique identifier for the event. + disable_geoip: Whether to disable GeoIP for this event. + + Examples: + ```python + posthog.alias(previous_id='distinct_id', distinct_id='alias_id') + ``` + + Category: + Identification + """ (distinct_id, personless) = get_identity_state(distinct_id) if personless: @@ -539,7 +758,7 @@ def alias( } if get_context_session_id(): - msg["properties"]["$session_id"] = get_context_session_id() + msg["properties"]["$session_id"] = str(get_context_session_id()) return self._enqueue(msg, disable_geoip) @@ -548,6 +767,28 @@ def capture_exception( exception: Optional[ExceptionArg], **kwargs: Unpack[OptionalCaptureArgs], ): + """ + Capture an exception for error tracking. + + Args: + exception: The exception to capture. + distinct_id: The distinct ID of the user. + properties: A dictionary of additional properties. + send_feature_flags: Whether to send feature flags with the exception. + disable_geoip: Whether to disable GeoIP for this event. + + Examples: + ```python + try: + # Some code that might fail + pass + except Exception as e: + posthog.capture_exception(e, 'user_distinct_id', properties=additional_properties) + ``` + + Category: + Error Tracking + """ distinct_id = kwargs.get("distinct_id", None) properties = kwargs.get("properties", None) send_feature_flags = kwargs.get("send_feature_flags", False) @@ -703,7 +944,15 @@ def _enqueue(self, msg, disable_geoip): return None def flush(self): - """Forces a flush from the internal queue to the server""" + """ + Force a flush from the internal queue to the server. Do not use directly, call `shutdown()` instead. + + Examples: + ```python + posthog.capture('event_name') + posthog.flush() # Ensures the event is sent immediately + ``` + """ queue = self.queue size = queue.qsize() queue.join() @@ -711,8 +960,13 @@ def flush(self): self.log.debug("successfully flushed about %s items.", size) def join(self): - """Ends the consumer thread once the queue is empty. - Blocks execution until finished + """ + End the consumer thread once the queue is empty. Do not use directly, call `shutdown()` instead. + + Examples: + ```python + posthog.join() + ``` """ for consumer in self.consumers: consumer.pause() @@ -726,7 +980,14 @@ def join(self): self.poller.stop() def shutdown(self): - """Flush all messages and cleanly shutdown the client""" + """ + Flush all messages and cleanly shutdown the client. Call this before the process ends in serverless environments to avoid data loss. + + Examples: + ```python + posthog.shutdown() + ``` + """ self.flush() self.join() @@ -799,6 +1060,17 @@ def _load_feature_flags(self): self._last_feature_flag_poll = datetime.now(tz=tzutc()) def load_feature_flags(self): + """ + Load feature flags for local evaluation. + + Examples: + ```python + posthog.load_feature_flags() + ``` + + Category: + Feature Flags + """ if not self.personal_api_key: self.log.warning( "[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags." @@ -876,6 +1148,31 @@ def feature_enabled( send_feature_flag_events=True, disable_geoip=None, ): + """ + Check if a feature flag is enabled for a user. + + Args: + key: The feature flag key. + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + send_feature_flag_events: Whether to send feature flag events. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + is_my_flag_enabled = posthog.feature_enabled('flag-key', 'distinct_id_of_your_user') + if is_my_flag_enabled: + # Do something differently for this user + # Optional: fetch the payload + matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user') + ``` + + Category: + Feature Flags + """ response = self.get_feature_flag( key, distinct_id, @@ -1005,8 +1302,29 @@ def get_feature_flag_result( """ Get a FeatureFlagResult object which contains the flag result and payload for a key by evaluating locally or remotely depending on whether local evaluation is enabled and the flag can be locally evaluated. + This also captures the `$feature_flag_called` event unless `send_feature_flag_events` is `False`. + + Examples: + ```python + flag_result = posthog.get_feature_flag_result('flag-key', 'distinct_id_of_your_user') + if flag_result and flag_result.get_value() == 'variant-key': + # Do something differently for this user + # Optional: fetch the payload + matched_flag_payload = flag_result.payload + ``` - This also captures the $feature_flag_called event unless send_feature_flag_events is False. + Args: + key: The feature flag key. + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + send_feature_flag_events: Whether to send feature flag events. + disable_geoip: Whether to disable GeoIP for this request. + + Returns: + Optional[FeatureFlagResult]: The feature flag result or None if disabled/not found. """ return self._get_feature_flag_result( key, @@ -1032,11 +1350,29 @@ def get_feature_flag( disable_geoip=None, ) -> Optional[FlagValue]: """ - Get a feature flag value for a key by evaluating locally or remotely - depending on whether local evaluation is enabled and the flag can be - locally evaluated. + Get multivariate feature flag value for a user. - This also captures the $feature_flag_called event unless send_feature_flag_events is False. + Args: + key: The feature flag key. + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + send_feature_flag_events: Whether to send feature flag events. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + enabled_variant = posthog.get_feature_flag('flag-key', 'distinct_id_of_your_user') + if enabled_variant == 'variant-key': # replace 'variant-key' with the key of your variant + # Do something differently for this user + # Optional: fetch the payload + matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user') + ``` + + Category: + Feature Flags """ feature_flag_result = self.get_feature_flag_result( key, @@ -1101,6 +1437,33 @@ def get_feature_flag_payload( send_feature_flag_events=True, disable_geoip=None, ): + """ + Get the payload for a feature flag. + + Args: + key: The feature flag key. + distinct_id: The distinct ID of the user. + match_value: The specific flag value to get payload for. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + send_feature_flag_events: Whether to send feature flag events. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + is_my_flag_enabled = posthog.feature_enabled('flag-key', 'distinct_id_of_your_user') + + if is_my_flag_enabled: + # Do something differently for this user + # Optional: fetch the payload + matched_flag_payload = posthog.get_feature_flag_payload('flag-key', 'distinct_id_of_your_user') + ``` + + Category: + Feature Flags + """ feature_flag_result = self._get_feature_flag_result( key, distinct_id, @@ -1243,6 +1606,25 @@ def get_all_flags( only_evaluate_locally=False, disable_geoip=None, ) -> Optional[dict[str, Union[bool, str]]]: + """ + Get all feature flags for a user. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + posthog.get_all_flags('distinct_id_of_your_user') + ``` + + Category: + Feature Flags + """ response = self.get_all_flags_and_payloads( distinct_id, groups=groups, @@ -1264,6 +1646,25 @@ def get_all_flags_and_payloads( only_evaluate_locally=False, disable_geoip=None, ) -> FlagsAndPayloads: + """ + Get all feature flags and their payloads for a user. + + Args: + distinct_id: The distinct ID of the user. + groups: A dictionary of group information. + person_properties: A dictionary of person properties. + group_properties: A dictionary of group properties. + only_evaluate_locally: Whether to only evaluate locally. + disable_geoip: Whether to disable GeoIP for this request. + + Examples: + ```python + posthog.get_all_flags_and_payloads('distinct_id_of_your_user') + ``` + + Category: + Feature Flags + """ if self.disabled: return {"featureFlags": None, "featureFlagPayloads": None} diff --git a/posthog/contexts.py b/posthog/contexts.py index db62c17c..0b63ac8b 100644 --- a/posthog/contexts.py +++ b/posthog/contexts.py @@ -71,7 +71,9 @@ def _get_current_context() -> Optional[ContextScope]: @contextmanager def new_context( - fresh=False, capture_exceptions=True, client: Optional["Client"] = None + 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. @@ -94,20 +96,25 @@ def new_context( the global one, in the case of `posthog.capture`) Examples: + ```python # Inherit parent context tags with posthog.new_context(): posthog.tag("request_id", "123") # Both this event and the exception will be tagged with the context tags posthog.capture("event_name", {"property": "value"}) raise ValueError("Something went wrong") - + ``` + ```python # Start with fresh context (no inherited tags) with posthog.new_context(fresh=True): posthog.tag("request_id", "123") # Both this event and the exception will be tagged with the context tags posthog.capture("event_name", {"property": "value"}) raise ValueError("Something went wrong") + ``` + Category: + Contexts """ from posthog import capture_exception @@ -138,7 +145,12 @@ def tag(key: str, value: Any) -> None: value: The tag value Example: + ```python posthog.tag("user_id", "123") + ``` + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -152,6 +164,9 @@ def get_tags() -> Dict[str, Any]: Returns: Dict of all tags in the current context + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -170,6 +185,9 @@ def identify_context(distinct_id: str) -> None: Args: distinct_id: The distinct ID to associate with the current context and its children. + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -184,6 +202,9 @@ def set_context_session(session_id: str) -> None: Args: session_id: The session ID to associate with the current context and its children. See https://posthog.com/docs/data/sessions + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -196,6 +217,9 @@ def get_context_session_id() -> Optional[str]: Returns: The session ID if set, None otherwise + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -209,6 +233,9 @@ def get_context_distinct_id() -> Optional[str]: Returns: The distinct ID if set, None otherwise + + Category: + Contexts """ current_context = _get_current_context() if current_context: @@ -219,7 +246,7 @@ def get_context_distinct_id() -> Optional[str]: F = TypeVar("F", bound=Callable[..., Any]) -def scoped(fresh=False, capture_exceptions=True): +def scoped(fresh: bool = False, capture_exceptions: bool = True): """ Decorator that creates a new context for the function. Simply wraps the function in a with posthog.new_context(): block. @@ -239,6 +266,9 @@ def process_payment(payment_id): # If this raises an exception, it will be captured with tags # and then re-raised some_risky_function() + + Category: + Contexts """ def decorator(func: F) -> F: