posthog-instrumentation

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PostHog Instrumentation Skill

PostHog 埋点技能

Help users add PostHog analytics, event tracking, and feature flags to their code.
帮助用户在代码中添加PostHog分析、事件追踪和功能标志。

When to Use

使用场景

  • User asks to "add PostHog" or "add analytics"
  • User wants to track events or user actions
  • User needs to implement feature flags
  • User asks about instrumenting their code
  • 用户要求“添加PostHog”或“添加分析功能”
  • 用户想要追踪事件或用户行为
  • 用户需要实现功能标志
  • 用户询问代码埋点相关问题

Workflow

工作流程

  1. Identify the framework (React, Next.js, Python, Node.js, etc.)
  2. Check for existing PostHog setup
  3. Add appropriate instrumentation
  1. 识别框架(React、Next.js、Python、Node.js等)
  2. 检查是否已存在PostHog配置
  3. 添加合适的埋点代码

Code Patterns

代码示例

JavaScript/TypeScript

JavaScript/TypeScript

javascript
// Event tracking
posthog.capture('button_clicked', { button_name: 'signup' })

// Feature flags
if (posthog.isFeatureEnabled('new-feature')) {
  // Show new feature
}

// User identification
posthog.identify(userId, { email: user.email })
javascript
// Event tracking
posthog.capture('button_clicked', { button_name: 'signup' })

// Feature flags
if (posthog.isFeatureEnabled('new-feature')) {
  // Show new feature
}

// User identification
posthog.identify(userId, { email: user.email })

Python

Python

python
from posthog import Posthog
posthog = Posthog(api_key='<ph_project_api_key>')
python
from posthog import Posthog
posthog = Posthog(api_key='<ph_project_api_key>')

Event tracking

Event tracking

posthog.capture(distinct_id='user_123', event='purchase_completed')
posthog.capture(distinct_id='user_123', event='purchase_completed')

Feature flags

Feature flags

if posthog.feature_enabled('new-feature', 'user_123'): # Show new feature
undefined
if posthog.feature_enabled('new-feature', 'user_123'): # Show new feature
undefined

React

React

jsx
import { usePostHog } from 'posthog-js/react'

function MyComponent() {
  const posthog = usePostHog()

  const handleClick = () => {
    posthog.capture('button_clicked')
  }
}
jsx
import { usePostHog } from 'posthog-js/react'

function MyComponent() {
  const posthog = usePostHog()

  const handleClick = () => {
    posthog.capture('button_clicked')
  }
}

Best Practices

最佳实践

  • Use consistent event naming (snake_case recommended)
  • Include relevant properties with events
  • Identify users early in their session
  • Use feature flags for gradual rollouts
  • 使用一致的事件命名(推荐使用蛇形命名法snake_case)
  • 为事件添加相关属性
  • 在用户会话早期完成用户标识
  • 使用功能标志进行逐步发布