tracking
它能做什么
这项技能说明应用如何通过一个追踪函数,将使用事件发送到一个或多个分析服务。适合添加分析功能或接入新的分析服务商的开发者。
适用场景
- ✓当你想在应用中记录一个事件,例如用户保存了一条新笔记的时候。
- ✓当你通过环境设置配置内置分析服务的时候。
- ✓当你为其他服务添加自定义分析服务商的时候。
技能链接
https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/tracking/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在有人在此应用中保存新笔记时进行追踪,以便我能在分析工具中看到它。”
每周 5 个新技能,1 分钟读完。
工作原理
应用通过一个追踪函数,将每个事件发送给所有已连接的服务商。服务商通过环境变量设置,并以批量方式发送;任何失败都不会阻塞用户。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Tracking
Rule
The tracking system provides a single server-side track() call that fans out
to all registered providers, plus the browser-side trackEvent() counterpart.
Built-in providers auto-register from env vars - set the var and tracking
starts. Custom providers can be registered for any analytics backend. Both
surfaces are best-effort and never block request handling.
How It Works
- At server startup,
registerBuiltinProviders()checks env vars and registers any configured providers. - Application code calls
track(eventName, properties, source)from actions, plugins, or server routes. - The registry fans out the event to every registered provider. Errors are caught and logged -- a failing provider never crashes the caller.
- Built-in providers batch HTTP calls (flush every 10 seconds or 50 events, whichever comes first). Each send times out after 10 seconds.
API
track(name, properties?, source?)
Fire an analytics event. source is either a { userId, anonymousId, sessionId }
meta object or an action's ctx passed straight through.
import { track } from "@agent-native/core/tracking";
// From an action — pass ctx; userId comes from ctx.userEmail.
run: async ({ name }, ctx) => {
track("meal.logged", { mealName: name, calories: 350 }, ctx);
};
// From a plugin or route with no ctx.
track(
"meal.logged",
{ mealName: "Salad", calories: 350 },
{ userId: "user@example.com" },
);
The caller's browser session comes from the ambient request context
(RequestContext.browserSessionId, set from the X-Agent-Native-Session-Id
header), so it resolves the same whether the UI called the action or the agent
did. Pass sessionId in the meta object to override it — routes that run
outside a request context, such as /_agent-native/track, do exactly that.
Providers map it to their own session field: $session_id for PostHog (which
joins the event to session replay), session_id as a property for Mixpanel and
Amplitude, a top-level sessionId for webhooks and Agent-Native Analytics. It
is absent for callers with no browser — cron, CLI, MCP, A2A.
identify(userId, traits?)
Identify a user with traits. Forwarded to providers that support it.
import { identify } from "@agent-native/core/tracking";
identify("user@example.com", { plan: "pro", company: "ExampleCo" });
registerTrackingProvider(provider)
Register a custom provider.
import { registerTrackingProvider } from "@agent-native/core/tracking";
registerTrackingProvider({
name: "my-analytics",
track(event) {
// Send event to your backend
},
identify(userId, traits) {
// Optional
},
flush() {
// Optional -- called on graceful shutdown
},
});
flushTracking()
Flush all providers (call before process exit).
Built-in Providers
Set the env var and the provider auto-registers at startup. No SDK dependencies -- all providers use raw HTTP.
| Provider | Env vars |
|---|---|
| PostHog | POSTHOG_API_KEY (required), POSTHOG_HOST (optional, defaults to https://us.i.posthog.com), POSTHOG_ERROR_TRACKING=false (optional opt-out) |
| Mixpanel | MIXPANEL_TOKEN |
| Amplitude | AMPLITUDE_API_KEY |
| Agent-Native Analytics | AGENT_NATIVE_ANALYTICS_PUBLIC_KEY (server), AGENT_NATIVE_ANALYTICS_ENDPOINT (optional, defaults to https://analytics.agent-native.com/track) |
| Webhook | TRACKING_WEBHOOK_URL (required), TRACKING_WEBHOOK_AUTH (optional, sent as Authorization header) |
Multiple providers can be active simultaneously. All receive every event.
Browser-side trackEvent() also forwards to Agent-Native Analytics when VITE_AGENT_NATIVE_ANALYTICS_PUBLIC_KEY is present. Use VITE_AGENT_NATIVE_ANALYTICS_ENDPOINT to override the default browser endpoint. The built-in Agent-Nat
常见问题
分析服务出错会导致应用崩溃吗?
不会。错误会被捕获并记录,请求会继续进行。
内置了哪些服务?
技能列出了 PostHog、Mixpanel、Amplitude 以及一个通用的 webhook。
事件是如何发送的?
内置服务商会批量发送事件,每 10 秒或每 50 个事件发送一次,以先到者为准。
详情
作者的原始描述
Server-side analytics tracking with pluggable providers. Use when adding analytics events, registering custom tracking providers, or configuring built-in providers (PostHog, Mixpanel, Amplitude, Webhook).
repo: BuilderIO/agent-native
热度
- GitHub 星标
- 7.6k
- 排名
- #13186
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …