Skip to content

before_send is completely ignored during module-level initialization in __init__.py #450

@sipa-echo-zaoa

Description

@sipa-echo-zaoa

Root Cause

The Client class constructor in posthog/client.py correctly accepts the before_send argument.

However, in posthog/__init__.py, the module-level state variables and the setup() function completely omit before_send. When setup() lazily instantiates the Client, it does not look for a global before_send variable, nor does it pass it into the constructor:

# From posthog/__init__.py -> setup()
default_client = Client(
    api_key,
    host=host,
    debug=debug,
    on_error=on_error,
    send=send,
    # ... before_send is completely missing here ...
    in_app_modules=in_app_modules,
)

Why this matters: The official Django integration documentation explicitly instructs developers to initialize PostHog globally inside apps.py:

# From the official Django docs:
from django.apps import AppConfig
import posthog

class YourAppConfig(AppConfig):
    name = "your_app_name"
    def ready(self):
        posthog.api_key = '<ph_project_api_key>'
        posthog.host = 'https://eu.i.posthog.com'
        # Developers naturally try to add `posthog.before_send = my_filter` here, but it silently fails.

Steps to Reproduce

Following the standard initialization pattern, before_send fails to attach:

import posthog

def my_filter(event):
    print("This will never fire")
    return event

posthog.api_key = 'my-key'
posthog.before_send = my_filter # This variable is ignored by setup()

# Triggering an event initializes the client via setup(), but without the hook
posthog.capture('user_123', 'test_event')

Environment

  • posthog-python version: 7.9.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions