querying-canvas-data
Skill này làm gì
Skill này cho thấy canvas nên tải dữ liệu từ PostHog an toàn và chính xác như thế nào. Nó dành cho người xây dựng muốn các con số trên canvas khớp với các insight và báo cáo mà họ đã tin cậy.
Khi nào nên dùng
- ✓Khi một canvas cần hiển thị một chỉ số từ PostHog.
- ✓Khi các con số trên canvas không khớp với insight PostHog mà chúng lấy từ đó.
- ✓Khi bạn muốn một chỉ số trên canvas liên kết ngược về insight đã lưu của nó.
Liên kết skill
https://github.com/PostHog/posthog/blob/master/products/canvas/skills/querying-canvas-data/SKILL.mdSao 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
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Hiển thị số người dùng hoạt động hằng tuần của chúng tôi trong canvas này, và đảm bảo con số khớp với insight đã lưu của chúng tôi.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI ưu tiên tải một insight đã lưu sẵn, vì cách này giữ các con số giống hệt PostHog. Tiếp theo, AI dùng các truy vấn có kiểu, và chỉ dùng SQL nội tuyến như lựa chọn cuối cùng. Sau đó, AI hiển thị kết quả.
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.
Querying canvas data
The ph bridge is the only way a canvas talks to PostHog. Import it with
import { ph } from "@posthog/canvas-sdk" — a platform-provided module, so it needs no
dependencies entry. The same object is also installed as the window.ph global, which existing
canvases use; prefer the import in new code. Its typed surface is
references/canvas-sdk.d.ts. Never initialize it: credentials stay in
the host, and fetch(), posthog-js, and hand-rolled clients cannot reach PostHog from the sandbox.
External requests and resources require a non-PostHog origin declared in
capabilities.network.origins, and work only in the published canvas — the edit-mode preview
blocks all direct network access. This includes external stylesheets; remote scripts remain
blocked.
Data hierarchy — back every metric with a saved insight
- Preferred — save an insight, load it by reference. Use the PostHog MCP insight tools to
create/save an insight that computes the metric with an insight query type (TrendsQuery,
FunnelsQuery, RetentionQuery, PathsQuery, or the web-analytics kinds WebOverviewQuery /
WebStatsTableQuery — not raw SQL). Confirm its numbers, note the
short_id, and render it withawait ph.loadInsight(shortId, { dateRange }). These are proven queries — numbers match the PostHog UI exactly (sessionization, unique users, breakdowns, bounce rate). Never fabricate a query or guess event/property names; discover and save them via MCP first. - Secondary — an ad-hoc typed node:
ph.query({ kind: "TrendsQuery", series: [...], dateRange: {...} })when saving an insight genuinely doesn't fit. - Last resort — inline HogQL:
ph.query("SELECT …"), only when no insight kind can express the metric; you then own the SQL and its date window.
For web-analytics boards specifically, use the web-analytics query kinds — raw HogQL subtly gets bounce rate, sessionization, channel attribution, and unique-visitor counts wrong.
Whatever tier you use, declare it in the project's capabilities before publishing: every
ph.loadInsight short id in capabilities.posthog.insights, every ph.capture event name in
captureEvents, and inlineQueries: true for any ph.query use. The host rejects undeclared
calls at runtime, and validation fails on undeclared literals.
Verifiability — every claim must be checkable in PostHog
A number a viewer cannot verify is a number they cannot trust. Every data-backed figure a canvas shows — a KPI, a chart, a table, a stated conclusion — must carry the verification affordance for its tier:
- Insight-backed metrics link the real insight in PostHog. For a metric loaded from a saved
insight (the preferred tier), render a "View in PostHog" affordance that calls
ph.openExternal(insightUrl)from a click. Mint the URL at authoring time with thegenerate-app-urlMCP tool (path template/insights/{id}with the insight's short id) and bake the returned URL into the source verbatim — never hand-build one.ph.openExternalonly openshttps://*.posthog.comURLs and only from a user gesture, so wire it to a button or link, never to load or render. Do not also bake the insight's saved query text into the source: canvas source is readable by every canvas viewer, while access to the insight itself is enforced by PostHog — the link is where a viewer inspects the query, with their own permissions applied. - Ad-hoc queries disclose the exact query that ran, viewable in place. For a figure computed
by
ph.query(a typed node or inline HogQL), show the query behind it — the HogQL text, or the typed query node pretty-printed as JSON — in a modal or a collapsed disclosure attached to the card (a QuillDialogorCollapsiblein a React canvas, a<details>element in an HTML one). Render it from the same constant or builder you pass toph.query, so the displayed query can never drift from the executed one. This discloses nothing beyond what the viewer already runs:ph.queryexecutes as the signed-in viewer.
These are not optional polish: a canvas that presents PostHog data without them is incomplete. Keep the affordances compact — a small link icon per insight-backed card, a "View query" disclosure per ad-hoc card, or one shared modal listing every ad-hoc query the canvas runs, each labeled with the figure it backs.
For a status board, set refresh to the cache lifet
Câu hỏi thường gặp
Vì sao phải lưu insight trước?
Insight đã lưu dùng logic truy vấn của PostHog, nên các con số của nó khớp chính xác với giao diện PostHog.
Canvas có thể chạy truy vấn thô không?
Có, nhưng đó là lựa chọn cuối cùng. Truy vấn nội tuyến cho thấy chính xác truy vấn đã chạy, giúp mọi người kiểm chứng con số.
Canvas có cần khóa API không?
Không. Thông tin xác thực nằm bên trong PostHog, và canvas dùng cầu nối dữ liệu thay thế.
Chi tiết
Mô tả gốc của tác giả
Get PostHog data into a canvas correctly: the host-injected `ph` SDK (loadInsight, query, capture, state, connectors, openExternal, navigate), the data hierarchy (saved insights first, typed query nodes second, inline HogQL last), verifiability (insight-backed metrics link their saved insight in PostHog; ad-hoc queries expose the exact query that ran), per-insight-type result shapes, progressive per-query loading, date-range wiring, live third-party data through the viewer's own connections (ph.connectors), and event capture from a canvas. Use whenever a canvas shows metrics, charts, tables, a
repo: PostHog/posthog
Độ phổ biến
- Sao GitHub
- 41k
- Vị trí xếp hạng
- #6842
- Giấy phép
- —
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
depot-ci
41kGiúp agent của bạn thiết lập và chạy quy trình Depot CI, một lựa chọn thay thế GitHub Actions, gồm chuyển các quy trình hiện có và đọc nhật ký chạy.
depot-container-builds
41kGiúp agent của bạn build image Docker trên các builder từ xa của Depot, gồm image đa nền tảng, đẩy lên registry và sửa lỗi build.
depot-github-runners
41kGiúp agent của bạn chuyển các job GitHub Actions sang runner do Depot quản lý, chọn kích thước và nhãn runner, và xử lý lỗi thiết lập runner.
editing-agents-md
41kGiúp agent của bạn quyết định một quy tắc nên nằm trong file AGENTS.md hay CLAUDE.md, và viết sao cho quy tắc được tuân thủ mà file không bị phình to.
Skill tương tự
discrawl
394kCho phép agent của bạn tìm kiếm và tóm tắt kho lưu trữ tin nhắn Discord trên máy, kiểm tra độ mới của dữ liệu và đọc tin nhắn trực tiếp.
clickhouse-io
280kGiúp agent của bạn tối ưu truy vấn và schema trong ClickHouse theo thực hành tốt nhất cho phân tích dữ liệu hiệu năng cao khi truy vấn chậm.
scientific-db-pubmed-database
280kGiúp agent của bạn tìm bài báo khoa học trong lĩnh vực y sinh. Hữu ích cho nghiên cứu cần thông tin y khoa cụ thể.