---
title: "PostHog Connector"
description: "คู่มือการผสานรวม PostHog ฉบับสมบูรณ์สำหรับการซิงค์ product analytics feature flags และข้อมูลพฤติกรรมผู้ใช้กับ Brevo ผ่าน Tajo"
url: https://tajo.io/th/docs/connectors/custom/posthog/
markdown: https://tajo.io/th/docs/connectors/custom/posthog.md
lang: th-TH
robots: noindex
---

ขอสิทธิ์ใช้งานล่วงหน้า

กรอกชื่อและที่อยู่อีเมลที่ทำงานของคุณ เราจะส่งลิงก์เพื่อยืนยันคำขอเข้าใช้งาน Tajo ทางอีเมล

# PostHog Connector

สถานะตัวเชื่อม: วางแผนไว้ ยังไม่ได้สร้าง ปัจจุบัน Tajo ยังไม่ได้ใช้งานตัวเชื่อมนี้

| ฟิลด์ต้นทาง | ฟิลด์ปลายทาง | ค่าตัวอย่าง |
| --- | --- | --- |
| `record.id` | `source_id` | `record_1042` |
| `record.updated_at` | `updated_at` | `2026-10-18T08:00:00Z` |
| `record.value` | `value` | `sample_value` |

เชื่อมต่อ 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

## ข้อกำหนดเบื้องต้น

ก่อนเริ่มต้น ตรวจสอบให้แน่ใจว่าคุณมี:

1. บัญชี PostHog (Cloud หรือ self-hosted)
2. Personal API Key จาก [PostHog Settings](https://app.posthog.com/settings/user-api-keys)
3. Project API Key (token) จาก [Project Settings](https://app.posthog.com/project/settings)
4. บัญชี Brevo ที่มีสิทธิ์เข้าถึง API
5. บัญชี Tajo ที่มีสิทธิ์ connector

## การยืนยันตัวตน

### Personal API Key (Private Endpoints)

```bash
# Generate at https://app.posthog.com/settings/user-api-keys
export POSTHOG_PERSONAL_API_KEY=phx_your_personal_api_key
export POSTHOG_PROJECT_TOKEN=phc_your_project_token
export POSTHOG_HOST=https://us.posthog.com  # or https://eu.posthog.com
export TAJO_API_KEY=your_tajo_api_key
export BREVO_API_KEY=your_brevo_api_key
```

```javascript
// Private API endpoints use Bearer authentication
const headers = {
  'Authorization': `Bearer ${process.env.POSTHOG_PERSONAL_API_KEY}`,
  'Content-Type': 'application/json'
};

// Public endpoints use the project token
const 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

## การกำหนดค่า

### การตั้งค่าพื้นฐาน

```yaml
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
```

### การแมปฟิลด์

```yaml
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_COHORTS
```

## API 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

```javascript
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

```javascript
// Paginate through PostHog persons
let 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

```javascript
// Get PostHog cohorts and sync members to Brevo lists
const 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

```javascript
// Use HogQL to query analytics data
const 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

```yaml
connectors:
  posthog:
    debug: true
    log_level: verbose
    log_queries: true
    log_sync: true
```

## แนวทางปฏิบัติที่ดีที่สุด

1. **ระบุตัวตนผู้ใช้** - เรียก `posthog.identify()` กับอีเมลเสมอเพื่อเปิดใช้งานการซิงค์ person
2. **ใช้ cohorts สำหรับการแบ่งกลุ่ม** - ใช้ behavioral cohorts ของ PostHog สำหรับรายการ Brevo
3. **Batch คำขอ API** - ใช้ pagination และการประมวลผลแบบ batch สำหรับ datasets ขนาดใหญ่
4. **ใช้ HogQL สำหรับ queries ที่ซับซ้อน** - ดึง analytics แบบกำหนดเองด้วย queries คล้าย SQL
5. **ตั้งค่า batch exports** - สำหรับปริมาณข้อมูลขนาดใหญ่ ใช้ batch exports แทนการ polling API
6. **กรองเหตุการณ์ที่เกี่ยวข้อง** - ซิงค์เฉพาะเหตุการณ์ที่เกี่ยวข้องกับการตลาดเพื่อลด 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 ที่รั่วไหล

## แหล่งข้อมูลที่เกี่ยวข้อง

- [Optimizely Connector](https://tajo.io/th/docs/connectors/custom/optimizely)
- [Segment Connector](https://tajo.io/th/docs/connectors/custom/segment)
- [Snowflake Connector](https://tajo.io/th/docs/connectors/custom/snowflake)
- [PostHog API Documentation](https://posthog.com/docs/api)
