บันทึกอย่างละเอียด — ระบบ monitor + วิเคราะห์ Google Ads อัตโนมัติด้วย AI พร้อมระบบ feedback loop ปิด

Production: https://joey-ads.duckdns.org/ · LINE OA: @630pzmtx · VPS: 72.62.79.218


1. จุดประสงค์ (Why)

ปัญหาเดิม: ดู Google Ads ต้องเปิด dashboard รายวัน, export Excel, ตีความหมายเอง, ตัดสินใจจากความรู้สึก — ช้า, ขาดทุน, พลาดโอกาส

เป้าหมาย: AI ดูข้อมูลที่เชื่อถือได้ → เสนอ action ที่ตรวจสอบได้ → user decide → วัดผล → เรียนจากความผิดพลาดของตัวเอง

2. Architecture Overview

┌──────────────────────────────────────────────────────────────┐
│  Google Ads API v30 (GAQL)                                            │
└────────┬────────────────────────────────────────────────────────┘
         │ ads_client.py (06:00 snapshot, real-time fetches)
         ▼
┌──────────────────────────────────────────────────────────────┐
│  PostgreSQL 16  (snapshots, actions, outcomes, experiments, context) │
│  Qdrant v1.13   (vector KB — RAG retrieval)                           │
└────────┬────────────────────────────────────────────────────────┘
         │
         ├─▶ analyzer.py            — Tier 1: daily account report
         ├─▶ campaign_analyzer.py   — Tier 1.5: per-campaign analysis
         ├─▶ strategic_advisor.py   — Tier 2: weekly strategic review (RAG)
         ├─▶ drift_detector.py      — anomaly detection
         ├─▶ outcome_measurer.py    — T+7/14/30 measurement
         ├─▶ experiments.py         — Bayesian A/B engine
         └─▶ bayesian_priors.py     — device×network posteriors
                  │
                  ▼
┌──────────────────────────────────────────────────────────────┐
│  Output channels                                                       │
│  • LINE OA broadcast (notifier.py)  — รายวัน/สัปดาห์              │
│  • FastAPI dashboard               — Accept/Reject/Implement       │
│  • PostgreSQL                      — long-term memory             │
└──────────────────────────────────────────────────────────────┘

3. Data Flow — จาก raw ไปสู่ action

  1. Fetch — GAQL query ดึง metrics (impressions, clicks, cost_micros, conversions, ROAS)
  2. Snapshot — บันทึกลง metric_snapshots (PK: date × entity × device × network)
  3. Detect — drift_detector เทียบกับ baseline (rolling 7d, 14d, 30d)
  4. Analyze — Claude AI อ่าน context (snapshots + company_context + RAG KB) → ออก actions
  5. Persist — บันทึก actions table (UUID, grade, expected_impact, status=pending)
  6. Notify — LINE Flex carousel → ผู้ใช้ + Web dashboard
  7. Decide — user Accept/Reject/Implement → update actions.status
  8. Measure — หลัง implement รอ 7/14/30 วัน → outcomes table → เปรียบ expected vs actual
  9. Learn — ค่า accuracy ย้อนกลับไป adjust prompt + Bayesian priors

4. AI Tier System — แยกตามความถี่และต้นทุน

Tier ความถี่ Model ขอบเขต ต้นทุน
1 รายวัน 08:00 Sonnet 4.5 account-level summary ต่ำ
1.5 รายสัปดาห์ จ. 09:00 Sonnet 4.5 per-campaign analysis ปานกลาง
2 Strategic จ. 10:00 Sonnet 4.5 + RAG ข้ามสัปดาห์, เชิงลึก สูง
Pulse 18:00 — (no LLM) snapshot + alerts ถูกมาก