Skip to content

Experiments API

A/B 测试分析 API,用于分析实验数据和转化率。

端点列表

方法端点说明
POST/api/experiments创建实验
GET/api/experiments获取实验列表
GET/api/experiments/{id}获取实验详情
PUT/api/experiments/{id}更新实验
DELETE/api/experiments/{id}删除实验
POST/api/experiments/{id}/publish发布实验
POST/api/experiments/{id}/pause暂停实验
GET/api/config/{gameId}/{environment}获取运行中的实验配置

实验状态

状态说明
draft草稿
running运行中
paused已暂停

创建实验

http
POST /api/experiments
Content-Type: application/json
Authorization: Bearer {token}

{
  "gameId": "game_abc123",
  "environment": "prod",
  "name": "Homepage Banner Test",
  "salt": "unique_salt",
  "config": {
    "variants": [
      {"name": "A", "weight": 50},
      {"name": "B", "weight": 50}
    ],
    "targeting": {
      "platform": ["android", "ios"],
      "countries": ["US", "CN"]
    },
    "metrics": {
      "primary": "conversion",
      "secondary": ["revenue", "retention"]
    }
  }
}

响应:

json
{
  "id": "exp_abc123",
  "gameId": "game_abc123",
  "environment": "prod",
  "name": "Homepage Banner Test",
  "status": "draft",
  "salt": "unique_salt",
  "config": {...},
  "createdAt": "2024-01-01T00:00:00Z"
}

发布实验

http
POST /api/experiments/{id}/publish
Authorization: Bearer {token}

获取运行中的实验配置

http
GET /api/config/{gameId}/{environment}

响应(公开端点,无需认证):

json
[
  {
    "id": "exp_abc123",
    "salt": "unique_salt",
    "config": {
      "variants": [
        {"name": "A", "weight": 50},
        {"name": "B", "weight": 50}
      ]
    }
  }
]

SDK 使用

javascript
// 获取实验配置
const experiments = await fetch('/api/config/game_abc123/prod');

// 分流
function assignVariant(experiment, userId) {
  const hash = murmur3(experiment.id + ':' + experiment.salt + ':' + userId);
  const bucket = hash % 100;
  let acc = 0;
  for (const variant of experiment.config.variants) {
    acc += variant.weight;
    if (bucket < acc) return variant.name;
  }
  return experiment.config.variants[0].name;
}

// 上报曝光
oddsmaker.track('experiment_exposure', {
  exp: 'exp_abc123',
  variant: 'B'
});

分析指标

指标说明
转化率各变体的转化率对比
统计显著性p-value < 0.05
SRM样本比例不匹配检测
UpliftB vs A 的提升度
置信区间95% 置信区间

Released under the MIT License.