Skip to content

PostHog GitHub 持续 AI

本指南介绍如何集成 PostHog 和 GitHub 来构建持续的产品分析和 AI 驱动的决策系统。

概述

通过结合 PostHog 的产品分析能力和 GitHub 的代码管理,ByteBuddy 可以:

  • 自动分析用户行为数据
  • 基于数据生成产品改进建议
  • 创建数据驱动的 Issue 和 PR
  • 监控功能发布后的表现

配置

1. 安装必要的 MCP 服务器

bash
npm install -g posthog-mcp-server github-mcp-server

2. 配置 ByteBuddy

json
{
  "mcpServers": {
    "posthog": {
      "command": "node",
      "args": ["posthog-mcp-server"],
      "env": {
        "POSTHOG_API_KEY": "your-posthog-api-key",
        "POSTHOG_HOST": "https://app.posthog.com"
      }
    },
    "github": {
      "command": "node",
      "args": ["github-mcp-server"],
      "env": {
        "GITHUB_TOKEN": "your-github-token"
      }
    }
  }
}

使用场景

场景 1: 自动化产品洞察

typescript
// 产品洞察自动化
class ProductInsights {
  async generateWeeklyInsights() {
    // 获取过去一周的数据
    const weeklyData = await mcp.call("posthog.getEvents", {
      events: ["$pageview", "$autocapture"],
      date_from: "-7d",
      date_to: "now",
      properties: [],
    });

    // 分析关键指标
    const insights = await this.analyzeKeyMetrics(weeklyData);

    // 生成改进建议
    const recommendations = await this.generateRecommendations(insights);

    // 创建 GitHub Issue
    for (const recommendation of recommendations) {
      await this.createInsightIssue(recommendation);
    }

    return insights;
  }

  private async analyzeKeyMetrics(data: any) {
    return {
      userEngagement: this.calculateEngagement(data),
      featureUsage: this.analyzeFeatureUsage(data),
      conversionRates: this.calculateConversionRates(data),
      dropOffPoints: this.identifyDropOffPoints(data),
      performance: this.analyzePerformance(data),
    };
  }

  private async generateRecommendations(insights: any) {
    const recommendations = [];

    // 用户参与度分析
    if (insights.userEngagement.score < 0.7) {
      recommendations.push({
        title: "提升用户参与度",
        description: `当前用户参与度评分为 ${insights.userEngagement.score},低于目标值`,
        type: "enhancement",
        priority: "high",
        suggestedActions: [
          "优化首页加载速度",
          "改进用户引导流程",
          "增加个性化推荐",
        ],
      });
    }

    // 功能使用率分析
    const lowUsageFeatures = insights.featureUsage.filter((f) => f.usage < 0.1);
    for (const feature of lowUsageFeatures) {
      recommendations.push({
        title: `功能 ${feature.name} 使用率低`,
        description: `功能 ${feature.name} 的使用率仅为 ${(feature.usage * 100).toFixed(2)}%`,
        type: "enhancement",
        priority: "medium",
        suggestedActions: [
          "改进功能入口的可见性",
          "添加功能使用引导",
          "收集用户反馈",
        ],
      });
    }

    return recommendations;
  }

  private async createInsightIssue(recommendation: any) {
    const issueBody = this.generateIssueBody(recommendation);

    return await mcp.call("github.createIssue", {
      owner: "your-org",
      repo: "your-repo",
      title: recommendation.title,
      body: issueBody,
      labels: ["data-driven", recommendation.type, recommendation.priority],
      assignees: this.findAssignees(recommendation.type),
    });
  }
}

场景 2: A/B 测试分析

typescript
// A/B 测试自动化分析
class ABTestAnalysis {
  async analyzeABTest(testId: string) {
    // 获取测试数据
    const testData = await mcp.call("posthog.getExperimentResults", {
      experiment_id: testId,
      date_from: "-30d",
      date_to: "now",
    });

    // 统计分析
    const analysis = this.performStatisticalAnalysis(testData);

    // 生成报告
    const report = await this.generateTestReport(testId, analysis);

    // 如果测试有明确结果,自动创建 PR
    if (analysis.winner) {
      await this.createWinnerPR(testId, analysis.winner);
    }

    return report;
  }

  private performStatisticalAnalysis(testData: any) {
    // 计算统计显著性
    const variants = testData.variants.map((variant) => ({
      name: variant.key,
      conversionRate: variant.conversion_rate,
      sampleSize: variant.participants,
      confidence: this.calculateConfidence(variant),
      uplift: this.calculateUplift(variant, testData.control),
    }));

    // 确定胜者
    const winner = this.determineWinner(variants);

    return {
      variants,
      winner,
      statisticalSignificance: this.isStatisticallySignificant(testData),
      testDuration: this.calculateTestDuration(testData),
      recommendations: this.generateTestRecommendations(variants),
    };
  }

  private async createWinnerPR(testId: string, winner: any) {
    const prTitle = `Implement winning variant: ${winner.name} for test ${testId}`;
    const prBody = this.generatePRBody(testId, winner);

    return await mcp.call("github.createPullRequest", {
      owner: "your-org",
      repo: "your-repo",
      title: prTitle,
      body: prBody,
      head: `feature/test-${testId}-${winner.name}`,
      base: "main",
    });
  }
}

场景 3: 用户反馈集成

typescript
// 用户反馈自动化处理
class UserFeedbackProcessor {
  async processFeedback() {
    // 获取最近反馈
    const feedback = await mcp.call("posthog.getSurveyResponses", {
      survey_id: "user-satisfaction",
      date_from: "-7d",
    });

    // 分析反馈趋势
    const trends = await this.analyzeFeedbackTrends(feedback);

    // 创建问题或改进任务
    for (const trend of trends) {
      if (trend.severity === "high") {
        await this.createFeedbackIssue(trend);
      }
    }

    return trends;
  }

  private async analyzeFeedbackTrends(feedback: any[]) {
    // 使用情感分析
    const sentimentAnalysis = await this.performSentimentAnalysis(feedback);

    // 识别常见问题
    const commonIssues = this.identifyCommonIssues(feedback);

    // 趋势分析
    const trends = [];

    for (const issue of commonIssues) {
      if (issue.count > 5) {
        trends.push({
          issue: issue.description,
          count: issue.count,
          sentiment: sentimentAnalysis[issue.description],
          severity: this.calculateSeverity(
            issue.count,
            sentimentAnalysis[issue.description],
          ),
          affectedUsers: issue.users.length,
        });
      }
    }

    return trends;
  }

  private async createFeedbackIssue(trend: any) {
    const issueBody = `
## 用户反馈趋势分析

**问题描述**: ${trend.issue}

**影响用户数**: ${trend.affectedUsers}

**反馈次数**: ${trend.count}

**情感分析**: ${trend.sentiment.label} (置信度: ${trend.sentiment.confidence})

**严重程度**: ${trend.severity}

## 建议行动

1. 调查具体用户场景
2. 制定解决方案
3. 跟踪改进效果

## 相关数据

- PostHog 仪表板: [查看详细数据](https://app.posthog.com/insights)
- 用户反馈记录: 已导出至附件
    `;

    return await mcp.call("github.createIssue", {
      owner: "your-org",
      repo: "your-repo",
      title: `用户反馈: ${trend.issue}`,
      body: issueBody,
      labels: ["user-feedback", trend.severity, "data-driven"],
    });
  }
}

持续监控

1: 性能监控

typescript
// 产品性能监控
class PerformanceMonitor {
  async monitorProductHealth() {
    const healthChecks = [
      this.checkPageLoadTimes(),
      this.checkErrorRates(),
      this.checkUserSatisfaction(),
      this.checkFeaturePerformance(),
    ];

    const results = await Promise.allSettled(healthChecks);

    const healthReport = {
      overall: "healthy",
      metrics: {},
      alerts: [],
    };

    results.forEach((result, index) => {
      const metricName = [
        "pageLoad",
        "errorRate",
        "userSatisfaction",
        "featurePerformance",
      ][index];

      if (result.status === "fulfilled") {
        healthReport.metrics[metricName] = result.value;

        if (result.value.status !== "healthy") {
          healthReport.alerts.push({
            metric: metricName,
            severity: result.value.severity,
            message: result.value.message,
          });
        }
      }
    });

    // 如果有告警,创建 Issue
    if (healthReport.alerts.length > 0) {
      await this.createHealthAlert(healthReport);
    }

    return healthReport;
  }

  private async checkPageLoadTimes() {
    const performanceData = await mcp.call("posthog.getInsight", {
      insight_id: "page-load-times",
      date_from: "-24h",
    });

    const avgLoadTime = performanceData.result[0].value;

    return {
      status: avgLoadTime < 3000 ? "healthy" : "warning",
      value: avgLoadTime,
      threshold: 3000,
      message: `平均页面加载时间: ${avgLoadTime}ms`,
    };
  }

  private async createHealthAlert(healthReport: any) {
    const alertBody = `
## 产品健康状态告警

**总体状态**: ${healthReport.overall}

### 关键指标

${Object.entries(healthReport.metrics)
  .map(
    ([key, value]: [string, any]) =>
      `- **${key}**: ${value.message} (${value.status})`,
  )
  .join("\n")}

### 告警详情

${healthReport.alerts
  .map(
    (alert) =>
      `- **${alert.metric}**: ${alert.message} (严重程度: ${alert.severity})`,
  )
  .join("\n")}

### 建议行动

1. 立即调查高严重程度问题
2. 制定改进计划
3. 加强监控频率

---

*此告警由 ByteBuddy 自动生成*
    `;

    return await mcp.call("github.createIssue", {
      owner: "your-org",
      repo: "your-repo",
      title: "产品健康状态告警",
      body: alertBody,
      labels: ["health-alert", "automated", "monitoring"],
    });
  }
}

自动化工作流

1: 每日报告

typescript
// 自动化每日报告
class DailyReporter {
  async generateDailyReport() {
    const reportDate = new Date().toISOString().split("T")[0];

    // 收集数据
    const data = {
      analytics: await this.getAnalyticsData(),
      github: await this.getGitHubActivity(),
      errors: await this.getErrorData(),
      performance: await this.getPerformanceData(),
    };

    // 生成报告
    const report = await this.compileReport(data, reportDate);

    // 发布到 GitHub
    await this.publishReport(report, reportDate);

    // 发送通知
    await this.sendSummary(report);

    return report;
  }

  private async getAnalyticsData() {
    return await mcp.call("posthog.getInsight", {
      insight_id: "daily-overview",
      date_from: "-1d",
      date_to: "now",
    });
  }

  private async publishReport(report: any, date: string) {
    const reportPath = `reports/daily-${date}.md`;

    // 创建或更新报告文件
    await mcp.call("github.createOrUpdateFile", {
      owner: "your-org",
      repo: "your-repo",
      path: reportPath,
      message: `Daily report for ${date}`,
      content: report.content,
      branch: "main",
    });

    // 创建 Issue 讨论报告
    await mcp.call("github.createIssue", {
      owner: "your-org",
      repo: "your-repo",
      title: `每日报告 - ${date}`,
      body: `## 每日报告摘要

${report.summary}

[查看详细报告](https://github.com/your-org/your-repo/blob/main/${reportPath})

### 关键指标

${report.keyMetrics
  .map((metric) => `- **${metric.name}**: ${metric.value} (${metric.trend})`)
  .join("\n")}
      `,
      labels: ["daily-report", "automated"],
    });
  }
}

最佳实践

1: 数据质量保证

typescript
// 数据质量检查
class DataQualityChecker {
  async checkDataQuality() {
    const checks = [
      this.checkDataCompleteness(),
      this.checkDataAccuracy(),
      this.checkDataConsistency(),
      this.checkDataTimeliness(),
    ];

    const results = await Promise.allSettled(checks);

    const qualityReport = {
      overall: "good",
      issues: [],
      recommendations: [],
    };

    results.forEach((result, index) => {
      if (
        result.status === "rejected" ||
        (result.status === "fulfilled" && !result.value.passed)
      ) {
        qualityReport.issues.push({
          check: ["completeness", "accuracy", "consistency", "timeliness"][
            index
          ],
          issue: result.reason || result.value.issue,
        });
      }
    });

    return qualityReport;
  }
}

通过 PostHob 和 GitHub 的深度集成,ByteBuddy 可以构建强大的数据驱动决策系统,让产品开发更加智能和高效。