PostHog Connector
เชื่อมต่อ PostHog กับ Brevo ผ่าน Tajo เพื่อซิงค์ข้อมูล product analytics เหตุการณ์พฤติกรรมผู้ใช้ และการเป็นสมาชิก cohort สำหรับแคมเปญการตลาดที่ขับเคลื่อนด้วยข้อมูลและการมีส่วนร่วมกับลูกค้าแบบส่วนตัว
ภาพรวม
| คุณสมบัติ | ค่า |
|---|---|
| แพลตฟอร์ม | PostHog |
| หมวดหมู่ | Product Analytics (แบบกำหนดเอง) |
| ความซับซ้อนในการตั้งค่า | ปานกลาง |
| การผสานรวมอย่างเป็นทางการ | ไม่ |
| ข้อมูลที่ซิงค์ | เหตุการณ์ Persons Feature Flags Cohorts |
| วิธีการยืนยันตัวตน | Personal API Key / Project Token |
ฟีเจอร์
- การซิงค์เหตุการณ์ - ส่งต่อเหตุการณ์ analytics ของ PostHog ไปยัง Brevo สำหรับการกำหนดเป้าหมายพฤติกรรม
- การซิงค์โปรไฟล์ Person - ซิงค์คุณสมบัติ person ของ PostHog กับแอตทริบิวต์ผู้ติดต่อ Brevo
- การแบ่งกลุ่มตาม Cohort - แมป PostHog cohorts กับรายการผู้ติดต่อ Brevo
- การซิงค์ feature flag - แบ่งกลุ่มผู้ติดต่อตาม feature flags ที่เปิดใช้งาน
- ข้อมูล Funnel - ใช้ข้อมูล conversion funnel สำหรับ re-engagement ที่กำหนดเป้าหมาย
- Metadata ของ session replay - เพิ่มความสมบูรณ์ให้ผู้ติดต่อด้วยตัวชี้วัดการมีส่วนร่วม session
ข้อกำหนดเบื้องต้น
ก่อนเริ่มต้น ตรวจสอบให้แน่ใจว่าคุณมี:
- บัญชี PostHog (Cloud หรือ self-hosted)
- Personal API Key จาก PostHog Settings
- Project API Key (token) จาก Project Settings
- บัญชี Brevo ที่มีสิทธิ์เข้าถึง API
- บัญชี Tajo ที่มีสิทธิ์ connector
การยืนยันตัวตน
Personal API Key (Private Endpoints)
# Generate at https://app.posthog.com/settings/user-api-keysexport POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_keyexport POSTHOG_PROJECT_TOKEN=phc_your_project_tokenexport POSTHOG_HOST=https://us.posthog.com # or https://eu.posthog.comexport TAJO_API_KEY=your_tajo_api_keyexport BREVO_API_KEY=your_brevo_api_key// Private API endpoints use Bearer authenticationconst headers = { 'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}`, 'Content-Type': 'application/json'};
// Public endpoints use the project tokenconst publicHeaders = { 'Content-Type': 'application/json'};// Token is passed in the request body for public endpointsความปลอดภัย API Key
Personal API keys ให้สิทธิ์เข้าถึงบัญชีทั้งหมด อย่าเปิดเผยในโค้ดฝั่ง client ใช้ Project API Key (token) สำหรับ endpoints สาธารณะเช่นการจับ event และการประเมิน feature flag
การกำหนดค่า
การตั้งค่าพื้นฐาน
connectors: posthog: enabled: true host: "${POSTHOG_HOST}" personal_api_key: "${POSTHOG_PERSONAL_API_KEY}" project_token: "${POSTHOG_PROJECT_TOKEN}" project_id: "12345"
sync: persons: true events: true cohorts: true feature_flags: true schedule: "0 */3 * * *" # Every 3 hours
event_filters: - "$pageview" - "purchase_completed" - "signup_completed" - "feature_used"
lists: all_users: 25 active_users: 26 power_users: 27การแมปฟิลด์
field_mapping: email: email $name: FIRSTNAME $browser: BROWSER $os: OS $initial_referrer: REFERRAL_SOURCE total_events: EVENT_COUNT last_seen: LAST_ACTIVE_DATE signup_date: SIGNUP_DATE plan: SUBSCRIPTION_PLAN company: COMPANY cohort_names: POSTHOG_COHORTSAPI Endpoints
| Endpoint | เมธอด | คำอธิบาย |
|---|---|---|
{host}/api/projects/{id}/persons/ | GET | แสดงรายการ persons |
{host}/api/projects/{id}/events/ | GET | แสดงรายการเหตุการณ์ |
{host}/api/projects/{id}/cohorts/ | GET | แสดงรายการ cohorts |
{host}/api/projects/{id}/feature_flags/ | GET | แสดงรายการ feature flags |
{host}/api/projects/{id}/feature_flags/evaluation/ | POST | ประเมิน flags |
{host}/api/projects/{id}/insights/ | GET | แสดงรายการ insights ที่บันทึกไว้ |
{host}/api/projects/{id}/query/ | POST | รัน HogQL queries |
{host}/i/v0/e | POST | จับ events (สาธารณะ) |
{host}/decide/?v=3 | POST | Feature flag decisions (สาธารณะ) |
ตัวอย่างโค้ด
เริ่มต้น Connector
import { TajoClient } from '@tajo/sdk';
const tajo = new TajoClient({ apiKey: process.env.TAJO_API_KEY, brevoApiKey: process.env.BREVO_API_KEY});
await tajo.connectors.connect('posthog', { host: process.env.POSTHOG_HOST, personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY, projectToken: process.env.POSTHOG_PROJECT_TOKEN, projectId: '12345'});ซิงค์ Persons กับ Brevo
// Paginate through PostHog personslet nextUrl = `${posthogHost}/api/projects/${projectId}/persons/?` + new URLSearchParams({ limit: '100' });
while (nextUrl) { const response = await fetch(nextUrl, { headers: { 'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}` } });
const data = await response.json();
for (const person of data.results) { const email = person.properties.$email || person.properties.email; if (!email) continue;
await tajo.contacts.sync({ email, attributes: { FIRSTNAME: person.properties.$name || person.properties.name, LAST_ACTIVE_DATE: person.properties.$last_seen, SIGNUP_DATE: person.created_at, EVENT_COUNT: person.properties.$event_count, BROWSER: person.properties.$browser, OS: person.properties.$os, REFERRAL_SOURCE: person.properties.$initial_referrer }, listIds: [25] }); }
nextUrl = data.next;}ซิงค์ Cohorts เป็นรายการ Brevo
// Get PostHog cohorts and sync members to Brevo listsconst cohortsResponse = await fetch( `${posthogHost}/api/projects/${projectId}/cohorts/`, { headers: { 'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}` } });
const { results: cohorts } = await cohortsResponse.json();
for (const cohort of cohorts) { // Get persons in this cohort const personsResponse = await fetch( `${posthogHost}/api/projects/${projectId}/cohorts/${cohort.id}/persons/`, { headers: { 'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}` } } );
const { results: persons } = await personsResponse.json();
for (const person of persons) { const email = person.properties.$email || person.properties.email; if (email) { await tajo.contacts.update(email, { attributes: { POSTHOG_COHORTS: cohort.name } }); } }}รัน HogQL Queries สำหรับ Analytics
// Use HogQL to query analytics dataconst queryResponse = await fetch( `${posthogHost}/api/projects/${projectId}/query/`, { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ query: { kind: 'HogQLQuery', query: ` SELECT properties.$email AS email, count() AS event_count, max(timestamp) AS last_event FROM events WHERE event = 'purchase_completed' AND timestamp > now() - interval 30 day GROUP BY email HAVING event_count > 3 ORDER BY event_count DESC LIMIT 1000 ` } }) });
const queryResult = await queryResponse.json();
for (const row of queryResult.results) { await tajo.contacts.update(row[0], { attributes: { PURCHASE_COUNT_30D: row[1], LAST_PURCHASE: row[2] } });}ขีดจำกัดอัตรา
| หมวดหมู่ Endpoint | ขีดจำกัด | หมายเหตุ |
|---|---|---|
| Analytics endpoints | 240/นาที, 1,200/ชั่วโมง | GET persons, events, insights |
| Query endpoint | 2,400/ชั่วโมง | HogQL และ custom queries |
| Feature flag evaluation | 600/นาที | Local evaluation endpoint |
| CRUD endpoints | 480/นาที, 4,800/ชั่วโมง | สร้าง อัปเดต ลบการดำเนินการ |
| Public endpoints (capture) | ไม่จำกัด | การจับ event, flag decisions |
Batch Exports
สำหรับการส่งออกข้อมูลเหตุการณ์ขนาดใหญ่ ใช้ฟีเจอร์ batch exports ของ PostHog แทน API Batch exports รองรับ S3, BigQuery, Snowflake และปลายทางอื่นๆ
การแก้ไขปัญหา
| ปัญหา | สาเหตุ | วิธีแก้ |
|---|---|---|
| 401 Unauthorized | API key ไม่ถูกต้อง | ตรวจสอบ Personal API Key ในการตั้งค่า |
| 400 Invalid project | Project ID ไม่ถูกต้อง | ตรวจสอบ project ID ใน PostHog URL |
| รายการ persons ว่างเปล่า | ไม่มีผู้ใช้ที่ระบุตัวตน | ตรวจสอบให้แน่ใจว่า posthog.identify() ถูกเรียก |
| Properties หายไป | Properties ไม่ได้ตั้งค่า | ตรวจสอบการเรียก $set ใน client SDK |
| ขีดจำกัดอัตรา 429 | คำขอมากเกินไป | ใช้ backoff ตรวจสอบ rate limit headers |
โหมด Debug
connectors: posthog: debug: true log_level: verbose log_queries: true log_sync: trueแนวทางปฏิบัติที่ดีที่สุด
- ระบุตัวตนผู้ใช้ - เรียก
posthog.identify()กับอีเมลเสมอเพื่อเปิดใช้งานการซิงค์ person - ใช้ cohorts สำหรับการแบ่งกลุ่ม - ใช้ behavioral cohorts ของ PostHog สำหรับรายการ Brevo
- Batch คำขอ API - ใช้ pagination และการประมวลผลแบบ batch สำหรับ datasets ขนาดใหญ่
- ใช้ HogQL สำหรับ queries ที่ซับซ้อน - ดึง analytics แบบกำหนดเองด้วย queries คล้าย SQL
- ตั้งค่า batch exports - สำหรับปริมาณข้อมูลขนาดใหญ่ ใช้ batch exports แทนการ polling API
- กรองเหตุการณ์ที่เกี่ยวข้อง - ซิงค์เฉพาะเหตุการณ์ที่เกี่ยวข้องกับการตลาดเพื่อลด noise
ความปลอดภัย
- Personal API Key - การยืนยันตัวตน Bearer token แบบกำหนดขอบเขต
- Project token - Token สาธารณะสำหรับการดำเนินการฝั่ง client เท่านั้น
- HTTPS เท่านั้น - Endpoints ทั้งหมดต้องการการเข้ารหัส TLS
- IP allowlisting - มีสำหรับ instances self-hosted
- การกำหนดขอบเขต key - สร้าง API keys ด้วย scope สิทธิ์เฉพาะ
- การสแกนความลับ GitHub - PostHog ร่วมมือกับ GitHub สำหรับการตรวจจับ key ที่รั่วไหล