From 77fecfb2d24b2e36ecee20482c5f006b09f54310 Mon Sep 17 00:00:00 2001 From: Rafael Loh Date: Fri, 16 Aug 2024 01:53:14 +0800 Subject: [PATCH] Create aws-kafka.md (#624) * Create aws-kafka.md * Rename aws-kafka.md to aws-kafka.md * Update _meta.json * Update integrations.mdx * Update aws-kafka.md --------- Co-authored-by: ishamehramixpanel <117322225+ishamehramixpanel@users.noreply.github.com> Co-authored-by: myronkaifung <97630035+myronkaifung@users.noreply.github.com> --- pages/docs/tracking-methods/integrations.mdx | 1 + .../tracking-methods/integrations/_meta.json | 1 + .../integrations/aws-kafka.md | 58 +++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 pages/docs/tracking-methods/integrations/aws-kafka.md diff --git a/pages/docs/tracking-methods/integrations.mdx b/pages/docs/tracking-methods/integrations.mdx index 301cb356af..a9fabb4e45 100644 --- a/pages/docs/tracking-methods/integrations.mdx +++ b/pages/docs/tracking-methods/integrations.mdx @@ -16,6 +16,7 @@ you're looking for [reach out to us](https://mixpanel.com/get-support). + diff --git a/pages/docs/tracking-methods/integrations/_meta.json b/pages/docs/tracking-methods/integrations/_meta.json index caabd92ea3..08e8d4f28b 100644 --- a/pages/docs/tracking-methods/integrations/_meta.json +++ b/pages/docs/tracking-methods/integrations/_meta.json @@ -1,5 +1,6 @@ { "amazon-s3": "Amazon S3", + "aws-kafka": "Amazon Kafka", "cms-ecommerce": "CMS & E-Commerce", "customer-io": { "title": "Customer.io ↗", diff --git a/pages/docs/tracking-methods/integrations/aws-kafka.md b/pages/docs/tracking-methods/integrations/aws-kafka.md new file mode 100644 index 0000000000..3fc5f79714 --- /dev/null +++ b/pages/docs/tracking-methods/integrations/aws-kafka.md @@ -0,0 +1,58 @@ +# Kafka + +This guide demonstrates how to plug Mixpanel into an event collection pipeline by AWS Kafka. Once set up, your events will route to Mixpanel and be available in real-time for analytics. This approach is serverless and open-source, and takes ~5 minutes to set up. + +To read messages from a Kafka topic and send them to Mixpanel, you'll need to use the confluent-kafka library for Kafka and the mixpanel library for Mixpanel. + +### Here's a sample Python code snippet: + +```python main.py +from confluent_kafka import Consumer, KafkaError +from mixpanel import Mixpanel + +# Kafka setup +kafka_config = { + 'bootstrap.servers': 'localhost:9092', + 'group.id': 'my-group', + 'auto.offset.reset': 'earliest' +} +consumer = Consumer(kafka_config) +consumer.subscribe(['my_topic']) + +# Mixpanel setup +mp = Mixpanel('YOUR_MIXPANEL_TOKEN') + +try: + while True: + msg = consumer.poll(1) + + if msg is None: + continue + if msg.error(): + if msg.error().code() == KafkaError._PARTITION_EOF: + continue + else: + print(msg.error()) + break + + # Send to Mixpanel + # The event_data contains the entire event. Consider extracting parsing the event for properties and explicitly assigning them instead. + event_data = msg.value().decode('utf-8') + mp.track('some_user_id', 'My Event', {'message': event_data}) + +finally: + consumer.close() + +``` + + +To run this code: + +1. Install Kafka and start a Kafka broker. +2. Install necessary Python packages: + - pip install confluent-kafka + - pip install mixpanel +3. Replace 'YOUR_MIXPANEL_TOKEN' with your actual Mixpanel token. +4. Run the code. + +Google has a [great reference](https://cloud.google.com/pubsub/docs/handling-failures) on best practices for handling both retryable and non-retryable errors.