跳到正文
Skillpedia
网站与界面(前端)41k · #6960

implementing-api-surveys

它能做什么

本技能向智能体展示如何使用应用自己的组件和布局,构建在你的产品中运行的 PostHog 问卷。适合希望完全掌控问卷外观的开发者。

适用场景

  • ✓当你想用自己的设计,在网站或应用中展示 PostHog 问卷时。
  • ✓当你正在构建自定义的问卷页面,而不是使用 PostHog 内置弹窗时。
  • ✓当你需要确认问卷答案能否正确到达 PostHog 的时候。

技能链接

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

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/PostHog/posthog/blob/master/products/surveys/skills/implementing-api-surveys/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“你能否把 PostHog 问卷构建到我的应用中,使其与我们的设计相符,并检查答案能否正确到达?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会读取你在 PostHog 中的问卷,查看你的代码和现有组件,编写问卷页面,并向 PostHog 发送正确的事件。然后,它会检查是否收到了测试回复。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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, 
常见问题

PostHog 会替我展示这份问卷吗?

不会。这类问卷在 PostHog 中只是一份定义。你的应用负责绘制问题,并将答案回传。

哪些应用可以使用这项技能?

只要智能体能够读取和编辑代码,浏览器应用、服务器应用和自定义移动页面都可以使用。

这项技能会在 PostHog 中创建问卷吗?

如果问卷尚不存在,智能体会被指示先使用单独的问卷技能创建它。

详情

作者的原始描述

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

热度

GitHub 星标
41k
排名
#6960
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能