Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)41k · #6960

implementing-api-surveys

Skill này làm gì

Skill này chỉ cho agent cách xây một khảo sát PostHog chạy bên trong sản phẩm của bạn, dùng các thành phần và bố cục riêng của ứng dụng. Skill dành cho lập trình viên muốn kiểm soát hoàn toàn cách khảo sát trông như thế nào.

Khi nào nên dùng

  • ✓Khi bạn muốn hiển thị một khảo sát PostHog trong website hoặc ứng dụng với thiết kế riêng của bạn.
  • ✓Khi bạn đang xây một màn hình khảo sát tùy chỉnh thay vì dùng cửa sổ bật lên có sẵn của PostHog.
  • ✓Khi bạn cần xác nhận rằng câu trả lời khảo sát đang được gửi đến PostHog đúng cách.

Liên kết skill

https://github.com/PostHog/posthog/blob/master/products/surveys/skills/implementing-api-surveys/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/PostHog/posthog/blob/master/products/surveys/skills/implementing-api-surveys/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Bạn có thể xây một khảo sát PostHog vào ứng dụng của tôi để nó khớp với thiết kế của chúng tôi và kiểm tra xem câu trả lời có đến nơi không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent đọc khảo sát của bạn trong PostHog, xem mã và các thành phần hiện có của bạn, viết màn hình khảo sát và gửi các sự kiện phù hợp đến PostHog. Sau đó nó kiểm tra xem một phản hồi thử nghiệm đã được nhận hay chưa.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Implementing API surveys

An api survey is only a definition in PostHog. PostHog does not render it. The application renders the questions and captures the survey events. If the events or their properties are wrong, responses are missing, double-counted, or unattributed.

1. Get the survey

Fetch it with posthog:survey-get by id. Note each question's id, type, question, choices, scale and optional, plus current_iteration. Never invent question ids.

If the survey does not exist yet, create it first with the creating-surveys skill (type api). Design guidance (short, rating or choice first, open text second) lives there too. Do not restate it here.

2. Read the user's codebase

Before writing UI, find:

  • The framework and the existing component library or design system. Build the survey from those components, not a generic modal.
  • Where PostHog is initialized, and the installed SDK and version (package.json or lockfile, or the server language's manifest).
  • Where the survey should appear. Let the app own the trigger: leave the survey's event, URL and selector conditions empty, and run the lookup below at the moment the survey should appear. getActiveMatchingSurveys only returns a survey with event conditions after that event fires, and nothing runs the lookup again at that point.

Pick the path:

  • Browser app with posthog-js: continue below.
  • Server-rendered pages, other SDKs (Python, Node, Go, Ruby, PHP), or custom mobile UI: read server-and-mobile.md, then return to step 5. Mobile SDKs render only popover surveys natively, so a custom mobile UI follows the server pattern.

3. Browser contract (posthog-js)

Require posthog-js 1.249.2 or newer. Older versions do not set the person properties and seen state on capture. Tell the user to upgrade rather than adding them by hand.

The lookup only works once both flags and the surveys bundle have loaded, because targeting is evaluated with flags and getActiveMatchingSurveys does nothing before surveys load. Register one retry for each at startup, and guard against rendering twice, since onFeatureFlags also runs on every flag reload (after identify, and after each response).

let triggered = false
let rendered = false

function tryShowSurvey() {
  if (!triggered || rendered) return
  posthog.getActiveMatchingSurveys((surveys) => {
    const survey = surveys.find((s) => s.id === SURVEY_ID)
    if (!survey || rendered) return
    rendered = true
    renderSurvey(survey)
  })
}

// Register once at startup, not per trigger.
posthog.onFeatureFlags(tryShowSurvey)
posthog.onSurveysLoaded(tryShowSurvey)

function showSurveyIfEligible() {
  triggered = true
  tryShowSurvey()
}

// Call when the survey closes, after the final send or a dismiss.
function onSurveyClosed() {
  triggered = false
  rendered = false
}

Call showSurveyIfEligible() from the app's trigger, such as after a checkout completes, or right after setup if the survey belongs on page load. Call onSurveyClosed() when the survey closes, so a survey that can repeat shows again on the next trigger. A survey that cannot repeat stays hidden, because posthog-js marks it as seen. In a single-page app, register the two listeners where the app initializes PostHog, and call the functions they return to unsubscribe if that code can unmount. SURVEY_ID is the id from step 1. renderSurvey mounts the component you build from the app's own UI, and that component calls the handlers below. getActiveMatchingSurveys applies the survey's targeting for this person, so render only what it returns.

Capture three events. answers maps each question id to its answer, and holds every answer given so far in this submission. Exact property names and value shapes are in event-contract.md.

function onShown(survey) {
  posthog.capture('survey shown', {
    $survey_id: survey.id,
    $survey_iteration: survey.current_iteration,
  })
  // posthog-js reads this key for the survey wait period; only its own renderer writes it.
  try {
    localStorage.setItem('lastSeenSurveyDate', new Date().toISOString())
  } catch {}
}

function onSubmit(survey, answers, submissionId, completed) {
  posthog.capture('survey sent', {
    $survey_id: survey.id,
    $survey_iteration: survey.current_iteration,
    $survey_submission_id: submissionId,
    $survey_completed: completed,
    $survey_questions: survey.questions.map(({ id, 
Câu hỏi thường gặp

PostHog có tự hiển thị khảo sát này cho tôi không?

Không. Loại khảo sát này chỉ là một định nghĩa trong PostHog. Ứng dụng của bạn vẽ các câu hỏi và gửi câu trả lời về.

Những ứng dụng nào có thể dùng skill này?

Ứng dụng trình duyệt, ứng dụng máy chủ và màn hình di động tùy chỉnh, miễn là agent có thể đọc và chỉnh sửa mã.

Skill này có tạo khảo sát trong PostHog không?

Nếu khảo sát chưa tồn tại, agent được hướng dẫn tạo nó trước bằng một skill khảo sát riêng.

Chi tiết

Mô tả gốc của tác giả

Implements a PostHog API (headless) survey inside the user's own codebase so it renders with their UI components and captures the exact events PostHog expects, then verifies the first response arrived. Use when asked to implement a survey, build a headless survey, build a custom survey UI, render a survey in my app, or wire up a survey of type `api` in a browser, server, or custom mobile UI. Covers the posthog-js event contract, server-side SDK capture, and post-implementation verification through MCP. To design or create the survey definition, use creating-surveys instead. For a survey that i

repo: PostHog/posthog

Độ phổ biến

Sao GitHub
41k
Vị trí xếp hạng
#6960
Giấy phép
—
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự