integration-astro-view-transitions

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PostHog integration for Astro (View Transitions)

支持视图过渡(View Transitions)的Astro PostHog集成

This skill helps you add PostHog analytics to Astro (View Transitions) applications.
本技能可帮助你为Astro(支持视图过渡)应用添加PostHog分析功能。

Workflow

操作流程

Follow these steps in order to complete the integration:
  1. basic-integration-1.0-begin.md
    - PostHog Setup - Begin ← Start here
  2. basic-integration-1.1-edit.md
    - PostHog Setup - Edit
  3. basic-integration-1.2-revise.md
    - PostHog Setup - Revise
  4. basic-integration-1.3-conclude.md
    - PostHog Setup - Conclusion
请按以下步骤完成集成:
  1. basic-integration-1.0-begin.md
    - PostHog 设置 - 开始 ← 从此处启动
  2. basic-integration-1.1-edit.md
    - PostHog 设置 - 编辑
  3. basic-integration-1.2-revise.md
    - PostHog 设置 - 修改
  4. basic-integration-1.3-conclude.md
    - PostHog 设置 - 总结

Reference files

参考文件

  • references/EXAMPLE.md
    - Astro (View Transitions) example project code
  • references/astro.md
    - Astro - docs
  • references/identify-users.md
    - Identify users - docs
  • references/basic-integration-1.0-begin.md
    - PostHog setup - begin
  • references/basic-integration-1.1-edit.md
    - PostHog setup - edit
  • references/basic-integration-1.2-revise.md
    - PostHog setup - revise
  • references/basic-integration-1.3-conclude.md
    - PostHog setup - conclusion
The example project shows the target implementation pattern. Consult the documentation for API details.
  • references/EXAMPLE.md
    - Astro(支持视图过渡)示例项目代码
  • references/astro.md
    - Astro - 文档
  • references/identify-users.md
    - 用户识别 - 文档
  • references/basic-integration-1.0-begin.md
    - PostHog 设置 - 开始
  • references/basic-integration-1.1-edit.md
    - PostHog 设置 - 编辑
  • references/basic-integration-1.2-revise.md
    - PostHog 设置 - 修改
  • references/basic-integration-1.3-conclude.md
    - PostHog 设置 - 总结
示例项目展示了目标实现模式。如需了解API详情,请查阅相关文档。

Key principles

核心原则

  • Environment variables: Always use environment variables for PostHog keys. Never hardcode them.
  • Minimal changes: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
  • Match the example: Your implementation should follow the example project's patterns as closely as possible.
  • 环境变量(Environment variables):始终使用环境变量存储PostHog密钥,切勿硬编码。
  • 最小改动:在现有集成代码旁添加PostHog代码,不要替换或重构现有代码。
  • 匹配示例:你的实现应尽可能贴近示例项目的模式。

Framework guidelines

框架指南

  • Always use the is:inline directive on PostHog script tags to prevent Astro from processing them and causing TypeScript errors
  • Use PUBLIC_ prefix for client-side environment variables in Astro (e.g., PUBLIC_POSTHOG_PROJECT_TOKEN)
  • Create a posthog.astro component in src/components/ for reusable initialization across pages
  • Import the PostHog component in a Layout and wrap all pages with that layout
  • Wrap PostHog initialization with a window.__posthog_initialized guard to prevent stack overflow during soft navigation
  • Set capture_pageview option to 'history_change' for automatic pageview tracking during soft navigation
  • Use the astro page-load event instead of just DOMContentLoaded to re-run scripts after soft navigation
  • 务必在PostHog脚本标签上使用is:inline指令,以避免Astro处理这些脚本时引发TypeScript错误
  • 在Astro中,客户端环境变量需使用PUBLIC_前缀(例如:PUBLIC_POSTHOG_PROJECT_TOKEN)
  • 在src/components/目录下创建posthog.astro组件,用于在多个页面中复用初始化逻辑
  • 在布局组件中导入PostHog组件,并使用该布局包裹所有页面
  • 使用window.__posthog_initialized守卫包裹PostHog初始化代码,防止软导航期间出现堆栈溢出
  • 将capture_pageview选项设置为'history_change',以在软导航期间自动追踪页面浏览
  • 使用astro的page-load事件替代单纯的DOMContentLoaded,确保软导航后脚本重新运行

Identifying users

用户识别

Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using
X-POSTHOG-DISTINCT-ID
and
X-POSTHOG-SESSION-ID
headers to maintain correlation.
在登录和注册事件中识别用户。请参考示例代码和文档,获取适用于该框架的正确识别模式。如果同时存在前端和后端代码,请使用
X-POSTHOG-DISTINCT-ID
X-POSTHOG-SESSION-ID
传递客户端会话和唯一ID,以保持关联。

Error tracking

错误追踪

Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.
在相关文件中添加PostHog错误追踪,尤其是关键用户流程和API边界处。