Tips & Tricks

So implementieren Sie A/B-Tests mit Claude Code

Erfahren Sie, wie Sie A/B-Tests mit Claude Code implementieren. Mit praktischen Codebeispielen und Schritt-für-Schritt-Anleitung.

Überblick über die A/B-Test-Implementierung

A/B-Tests sind unverzichtbar für datengestützte Entscheidungen. Allerdings ist die eigenständige Implementierung von korrekter statistischer Verarbeitung, Nutzerzuordnung und Ergebnisverfolgung aufwendig. Mit Claude Code können Sie eine statistisch korrekte A/B-Test-Infrastruktur effizient aufbauen.

Test-Zuordnungs-Engine

> Erstelle eine Engine, die Nutzer konsistent in A/B-Gruppen einteilt.
> Der gleiche Nutzer soll immer die gleiche Variante erhalten.
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('Die Summe der Variantengewichte muss 100 ergeben');
    }
    this.experiments.set(experiment.id, experiment);
  }

  assign(experimentId: string, userId: string): string {
    const experiment = this.experiments.get(experimentId);
    if (!experiment) throw new Error(`Experiment nicht gefunden: ${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;
  }
}

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

Verwendung in React-Komponenten

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;
}

// Verwendung in Komponenten
function CheckoutPage() {
  const variant = useExperiment('checkout-flow');

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

Statistische Analyse der Ergebnisse

Um A/B-Testergebnisse korrekt zu bewerten, ist die Berechnung der statistischen Signifikanz erforderlich.

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);
}

Zusammenfassung

Mit Claude Code können Sie eine durchgängige A/B-Test-Infrastruktur aufbauen – von der Nutzerzuordnung bis zur Berechnung der statistischen Signifikanz. Für die Integration mit Feature-Flags siehe den Feature-Flag-Implementierungsleitfaden, für die Analytics-Integration den Analytics-Implementierungsleitfaden.

Für die Theorie hinter statistischen Tests ist Evan Miller - Sample Size Calculator eine hilfreiche Ressource.

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