Tips & Tricks

Cara Mengimplementasikan A/B Testing dengan Claude Code

Pelajari cara mengimplementasikan A/B testing menggunakan Claude Code. Dilengkapi contoh kode praktis dan panduan langkah demi langkah.

Gambaran Umum Implementasi A/B Testing

A/B testing sangat penting untuk pengambilan keputusan berbasis data. Namun, mengimplementasikan sendiri pemrosesan statistik yang benar, alokasi pengguna, dan pelacakan hasil cukup sulit. Dengan Claude Code, kamu bisa membangun infrastruktur A/B testing yang secara statistik akurat dengan efisien.

Engine Alokasi Test

> Buat engine yang mengalokasikan user ke grup A/B secara konsisten.
> User yang sama harus selalu mendapat variant yang sama.
import crypto from 'crypto';

interface Experiment {
  id: string;
  variants: { id: string; weight: number }[];
}

class ABTestEngine {
  private experiments: Map<string, Experiment> = new Map();

  register(experiment: Experiment) {
    const totalWeight = experiment.variants.reduce((sum, v) => sum + v.weight, 0);
    if (Math.abs(totalWeight - 100) > 0.01) {
      throw new Error('Total weight variant harus 100');
    }
    this.experiments.set(experiment.id, experiment);
  }

  assign(experimentId: string, userId: string): string {
    const experiment = this.experiments.get(experimentId);
    if (!experiment) throw new Error(`Experiment tidak ditemukan: ${experimentId}`);

    const hash = crypto
      .createHash('md5')
      .update(`${experimentId}:${userId}`)
      .digest('hex');
    const bucket = parseInt(hash.substring(0, 8), 16) % 100;

    let cumulative = 0;
    for (const variant of experiment.variants) {
      cumulative += variant.weight;
      if (bucket < cumulative) return variant.id;
    }
    return experiment.variants[0].id;
  }
}

// Contoh penggunaan
const engine = new ABTestEngine();
engine.register({
  id: 'checkout-flow',
  variants: [
    { id: 'control', weight: 50 },
    { id: 'new-design', weight: 50 },
  ],
});

Penggunaan di React Component

import { createContext, useContext, useEffect } from 'react';

function useExperiment(experimentId: string): string {
  const engine = useContext(ABTestContext);
  const userId = useCurrentUserId();
  const variant = engine.assign(experimentId, userId);

  useEffect(() => {
    trackEvent('experiment_exposure', {
      experimentId,
      variant,
      userId,
    });
  }, [experimentId, variant, userId]);

  return variant;
}

// Penggunaan di component
function CheckoutPage() {
  const variant = useExperiment('checkout-flow');

  return variant === 'new-design'
    ? <NewCheckoutFlow />
    : <CurrentCheckoutFlow />;
}

Analisis Statistik Hasil

Untuk mengevaluasi hasil A/B test dengan benar, diperlukan perhitungan signifikansi statistik.

interface TestResult {
  sampleSize: number;
  conversions: number;
}

function calculateSignificance(control: TestResult, treatment: TestResult) {
  const p1 = control.conversions / control.sampleSize;
  const p2 = treatment.conversions / treatment.sampleSize;

  const pooledP = (control.conversions + treatment.conversions) /
    (control.sampleSize + treatment.sampleSize);

  const se = Math.sqrt(
    pooledP * (1 - pooledP) * (1 / control.sampleSize + 1 / treatment.sampleSize)
  );

  const zScore = (p2 - p1) / se;
  const pValue = 2 * (1 - normalCDF(Math.abs(zScore)));

  return {
    controlRate: (p1 * 100).toFixed(2) + '%',
    treatmentRate: (p2 * 100).toFixed(2) + '%',
    improvement: (((p2 - p1) / p1) * 100).toFixed(2) + '%',
    pValue: pValue.toFixed(4),
    significant: pValue < 0.05,
  };
}

function normalCDF(x: number): number {
  const a1 = 0.254829592, a2 = -0.284496736;
  const a3 = 1.421413741, a4 = -1.453152027;
  const a5 = 1.061405429, p = 0.3275911;
  const sign = x < 0 ? -1 : 1;
  x = Math.abs(x) / Math.sqrt(2);
  const t = 1.0 / (1.0 + p * x);
  const y = 1.0 - ((((a5 * t + a4) * t + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
  return 0.5 * (1.0 + sign * y);
}

Summary

Dengan Claude Code, kamu bisa membangun infrastruktur A/B testing secara konsisten, mulai dari alokasi pengguna hingga perhitungan signifikansi statistik. Untuk integrasi dengan feature flag, lihat Implementasi Feature Flags, dan untuk integrasi analytics, lihat Panduan Implementasi Analytics.

Untuk teori pengujian statistik, Evan Miller - Sample Size Calculator bisa menjadi referensi yang berguna.

#Claude Code #A/B testing #analytics #React #statistics