Loading...
Loading...
Compare original and translation side by side
| File | Load When |
|---|---|
| Setting up PostHog from scratch, detailed code patterns |
| Designing event naming conventions, property patterns |
| Implementing feature flags, A/B tests, experiments |
| 文件 | 加载场景 |
|---|---|
| 从零开始搭建PostHog,详细代码示例 |
| 设计事件命名规范、属性格式 |
| 实现功能标志、A/B测试、实验配置 |
undefinedundefinedundefinedundefinednext.config.tsconst nextConfig: NextConfig = {
async rewrites() {
return [
{
source: "/ingest/static/:path*",
destination: "https://us-assets.i.posthog.com/static/:path*",
},
{
source: "/ingest/:path*",
destination: "https://us.i.posthog.com/:path*",
},
{
source: "/ingest/decide",
destination: "https://us.i.posthog.com/decide",
},
];
},
// ... rest of config
};"connect-src 'self' ... https://*.posthog.com https://us.i.posthog.com https://us-assets.i.posthog.com",next.config.tsconst nextConfig: NextConfig = {
async rewrites() {
return [
{
source: "/ingest/static/:path*",
destination: "https://us-assets.i.posthog.com/static/:path*",
},
{
source: "/ingest/:path*",
destination: "https://us.i.posthog.com/:path*",
},
{
source: "/ingest/decide",
destination: "https://us.i.posthog.com/decide",
},
];
},
// ... 其他配置
};"connect-src 'self' ... https://*.posthog.com https://us.i.posthog.com https://us-assets.i.posthog.com",app/providers.tsx'use client'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import { useEffect } from 'react'
export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
// Use reverse proxy to bypass ad blockers
api_host: '/ingest',
ui_host: 'https://us.i.posthog.com',
defaults: '2025-05-24',
capture_pageview: false, // We handle manually for accurate funnels
person_profiles: 'identified_only',
})
}, [])
return <PHProvider client={posthog}>{children}</PHProvider>
}app/providers.tsx'use client'
import posthog from 'posthog-js'
import { PostHogProvider as PHProvider } from 'posthog-js/react'
import { useEffect } from 'react'
export function PostHogProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
// 使用反向代理绕过广告拦截器
api_host: '/ingest',
ui_host: 'https://us.i.posthog.com',
defaults: '2025-05-24',
capture_pageview: false, // 手动处理以确保漏斗分析准确性
person_profiles: 'identified_only',
})
}, [])
return <PHProvider client={posthog}>{children}</PHProvider>
}lib/posthog-server.tsimport { PostHog } from 'posthog-node'
let posthogClient: PostHog | null = null
export function getPostHogServer(): PostHog {
if (!posthogClient) {
posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
flushAt: 1,
flushInterval: 0,
})
}
return posthogClient
}lib/posthog-server.tsimport { PostHog } from 'posthog-node'
let posthogClient: PostHog | null = null
export function getPostHogServer(): PostHog {
if (!posthogClient) {
posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
flushAt: 1,
flushInterval: 0,
})
}
return posthogClient
}| Pattern | Example | Use Case |
|---|---|---|
| | User actions |
| | Feature usage |
| | User journey |
| 格式 | 示例 | 适用场景 |
|---|---|---|
| | 用户操作 |
| | 功能使用 |
| | 用户旅程 |
| Pattern | Example | Type |
|---|---|---|
| | Any |
| | Boolean |
| | Boolean |
| | Number |
| | Timestamp |
| 格式 | 示例 | 类型 |
|---|---|---|
| | 任意类型 |
| | 布尔值 |
| | 布尔值 |
| | 数值 |
| | 时间戳 |
Where to track?
├── User action in browser → Client (posthog-js)
├── API route / webhook → Server (posthog-node)
├── Server Component render → Server (posthog-node)
├── Need 100% accuracy → Server (no ad blockers)
└── Real-time UI feedback → Client (posthog-js)选择追踪位置?
├── 浏览器中的用户操作 → 客户端(posthog-js)
├── API路由 / Webhook → 服务端(posthog-node)
├── 服务端组件渲染 → 服务端(posthog-node)
├── 需要100%准确性 → 服务端(无广告拦截)
└── 实时UI反馈 → 客户端(posthog-js)| Pitfall | Solution |
|---|---|
| Ad blockers blocking PostHog | Use reverse proxy ( |
| Events not appearing | Check ad blockers, verify API key, use reverse proxy |
| Duplicate pageviews | Use |
| Feature flag flicker | Bootstrap flags via middleware |
| Missing user data | Call |
| Inconsistent naming | Use |
| Browser extension blocking - use reverse proxy |
503 from | Ad blocker injecting fake response - use reverse proxy |
| 问题 | 解决方案 |
|---|---|
| 广告拦截器阻止PostHog请求 | 使用反向代理( |
| 事件未显示 | 检查广告拦截器、验证API密钥、使用反向代理 |
| 重复页面浏览事件 | 设置 |
| 功能标志闪烁 | 通过中间件预加载标志 |
| 用户数据缺失 | 在 |
| 命名不一致 | 使用 |
| 浏览器扩展拦截 - 使用反向代理 |
| 广告拦截器注入虚假响应 - 使用反向代理 |