ข้ามไปยังเนื้อหา

SDK Integration

GrowthBook SDK ใช้โมเดล fetch-once, evaluate-locally เมื่อแอปของคุณเริ่มทำงาน SDK จะเชื่อมต่อกับ GrowthBook API และดาวน์โหลด JSON bundle ขนาดเล็กที่เรียกว่า feature definitions ซึ่งประกอบด้วย flag, rules และ experiment configurations ทั้งหมดของโปรเจกต์ หลังจากการดึงข้อมูลครั้งแรกนั้น การประเมิน flag ทุกครั้งจะเกิดขึ้นภายในแอปของคุณทั้งหมด โดยไม่มี network round-trip ต่อการเรียกแต่ละครั้ง

นี่คือ flow ตั้งแต่ต้นจนจบ:

  1. GrowthBook UI — คุณสร้าง flag, กำหนด targeting rules และ publish การเปลี่ยนแปลง
  2. GrowthBook CDN — จัดเก็บ compiled feature definitions สำหรับแต่ละ SDK key
  3. SDK init() — แอปของคุณดึงและ cache definitions เหล่านั้นตอน startup
  4. isOn() / getFeatureValue() — การประเมินทุกครั้งอ่านจาก local cache โดยไม่มี network call

สถาปัตยกรรมนี้ทำให้การประเมิน flag รวดเร็ว (ต่ำกว่า millisecond), ทำงานได้แบบ offline หลังจากการดึงข้อมูลครั้งแรก และรองรับปริมาณ request ใด ๆ โดยไม่เพิ่มภาระให้กับ GrowthBook

GrowthBook SDK พร้อมใช้งานสำหรับทุกภาษาและ platform หลัก:

  • JavaScript / TypeScript — browser และ Node.js
  • React — hooks และ context provider
  • React Native — mobile apps
  • Go — server-side
  • Python — server-side
  • Ruby — server-side
  • PHP — server-side
  • iOS (Swift) — native mobile
  • Android (Kotlin) — native mobile
  • Flutter — cross-platform mobile

โมดูลนี้ครอบคลุมบทเรียนต่อไปนี้:

บทเรียนสิ่งที่คุณจะได้เรียนรู้
JavaScript SDKการติดตั้ง vanilla SDK, การ initialise, การตั้ง attributes และการประเมิน flag ในทุก JS environment
React SDKGrowthBookProvider, hook useFeatureIsOn และ pattern การใช้ feature flag ใน React components
Node.js / Server-sideGrowthBook instances แบบ per-request และการประเมินแบบ safe ฝั่ง server
Streaming & Cachingการอัปเดต flag แบบ real-time ด้วย Server-Sent Events และตัวเลือก cache strategy

ค่า config ที่จำเป็นสองค่าคือ apiHost (CDN endpoint) และ clientKey (SDK key สำหรับ environment ของคุณ ซึ่งหาได้ใน GrowthBook ที่ SDK Connections)

import { GrowthBook } from '@growthbook/growthbook';

const gb = new GrowthBook({
  apiHost: 'https://cdn.growthbook.io',
  clientKey: 'sdk-abc123',
});

await gb.init({ timeout: 2000 });
// Definitions are now cached locally — evaluate away
console.log(gb.isOn('my-flag')); // true or false, no network call
GrowthBook SDK ประเมิน feature flag ที่ไหน?
SDK ต้องการค่าใดสองค่าในการดึง feature definitions?
ข้อใดไม่ใช่ platform ที่ GrowthBook SDK รองรับ?