{"library":"segment-analytics-python","code":"import os\nimport segment.analytics as analytics\n\n# It's recommended to set the write_key via an environment variable for security\nWRITE_KEY = os.environ.get('SEGMENT_WRITE_KEY', 'YOUR_SEGMENT_WRITE_KEY')\n\nif not WRITE_KEY or WRITE_KEY == 'YOUR_SEGMENT_WRITE_KEY':\n    print(\"WARNING: SEGMENT_WRITE_KEY environment variable not set or using placeholder. Analytics calls will not be sent.\")\n    analytics.send = False # Disable sending if key is not set\nelse:\n    analytics.write_key = WRITE_KEY\n    print(f\"Segment write_key set: {analytics.write_key[:4]}...{analytics.write_key[-4:]}\")\n\n# Configure for development (optional, but recommended to see errors)\nanalytics.debug = True\ndef on_error(error, items):\n    print(f\"An error occurred with Segment: {error}. Items: {items}\")\nanalytics.on_error = on_error\n\n# Identify a user\nanalytics.identify('user-123', {\n    'name': 'John Doe',\n    'email': 'john.doe@example.com',\n    'plan': 'premium'\n})\n\n# Track an event\nanalytics.track('Sign Up Completed', {\n    'method': 'email',\n    'plan': 'premium'\n})\n\n# Page view (for server-side apps, typically not directly used for web page views)\nanalytics.page('App Home', {\n    'path': '/home',\n    'title': 'Homepage'\n})\n\n# Group (associate a user with a group, e.g., a company)\nanalytics.group('user-123', 'company-456', {\n    'name': 'Acme Corp',\n    'industry': 'Software'\n})\n\n# Flush any remaining events in the queue\nanalytics.flush()\nprint(\"Segment events sent (or queued if SEND_DISABLED is not set).\")","lang":"python","description":"This quickstart demonstrates how to initialize the Segment client, identify a user, track an event, record a page view, and associate a user with a group. It includes best practices for handling the `write_key` via environment variables and setting up error handling for development. Remember to replace `YOUR_SEGMENT_WRITE_KEY` or set the `SEGMENT_WRITE_KEY` environment variable.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}