Pro Setup
เอกสารแพ็กเกจ gamification สำหรับ Kajabi ที่ย้ายจาก repo มาไว้ใน Wize School เพื่อใช้เป็น productized package และใช้กับเว็บนี้ต่อได้เลย
GamifyKit Pro — setup
Pro turns on **cross-device sync**, **real course leaderboards**, **quiz-gated paths**, and
**creator analytics**. The backend is hosted by us (Wize); buyers only paste an API key.
Architecture
- Backend lives in the Wize Astro app: src/pages/api/gamify/* (Cloudflare Workers).
- State store: **Cloudflare D1** (SQL) — chosen over Sanity because leaderboards need
ORDER BY xp and state writes are frequent. Schema: src/lib/gamify/schema.sql.
- Auth: each buyer has an API key (Bearer) for theme→API calls; the creator dashboard uses
the site's better-auth session + an admin allowlist.
Endpoints
| Route | Auth | Purpose |
|---|---|---|
| GET/POST /api/gamify/state | Bearer apiKey | load / upsert a student's xp·level·badges·unlocks (monotonic merge) |
| GET /api/gamify/leaderboard | Bearer apiKey | top-N by XP for a product |
| POST /api/gamify/webhook/kajabi?buyer=<id> | HMAC sig | quiz pass → unlock gated step |
| GET /api/gamify/analytics | session + admin | aggregate stats + event counts |
Provision (one-time)
GamifyKit reuses the site's existing D1 database (binding DB, wize-school-db) — no new
database to create. Just apply the gamify tables (prefixed gamify_):
npx wrangler d1 execute wize-school-db --remote --file=src/lib/gamify/schema.sql
seed a buyer:
npx wrangler d1 execute wize-school-db --remote --command \
"INSERT INTO gamify_buyer (id, api_key, plan, kajabi_site, webhook_secret, created_at) \
VALUES ('wize', 'gk_live_xxx', 'pro', 'wize.school', 'whsec_xxx', strftime('%s','now')*1000)"
Set GAMIFY_ADMIN_EMAILS (comma-separated) for dashboard access.
Buyer setup (in Kajabi theme settings)
1. Settings → Gamification → Mode = **Pro**.
2. Paste **Pro API base URL** (https://www.wize.school) and **Pro API key**.
3. (Quiz gating) Add a Kajabi automation on quiz pass → webhook to
…/api/gamify/webhook/kajabi?buyer=<id> signed with the buyer's secret.
Open items (see verification-notes.md)
- **studentKey**: theme resolves identity best-effort (member global → anonymous fallback).
Confirm a reliable Kajabi member identifier to make sync truly cross-device.
- **Quiz webhook payload**: field paths in webhook/kajabi.ts are provisional until a real
Kajabi quiz webhook is captured.