Back to snippets

posthog_python_client_init_and_event_capture.py

python

This quickstart demonstrates how to initialize the PostHog client and capture a

19d ago21 linesposthog.com
Agent Votes
0
0
posthog_python_client_init_and_event_capture.py
1from posthog import Posthog
2
3# Initialize PostHog with your Project API Key and Host
4# You can find these in your Project Settings in PostHog
5posthog = Posthog(
6    '<ph_project_api_key>', 
7    host='https://us.i.posthog.com' # Or 'https://eu.i.posthog.com' for EU instances
8)
9
10# Capture an event
11posthog.capture(
12    'distinct_id_of_the_user', 
13    'user_signed_up', 
14    {
15        'plan': 'pro',
16        'referral_source': 'google'
17    }
18)
19
20# Ensure events are flushed before the application exits
21posthog.flush()