# A/B Testing Guide

## Overview
A/B testing allows you to test different variations of features to determine which performs better.

## Creating A/B Tests

### Via Database
```sql
INSERT INTO ab_tests (name, key, is_active, variants)
VALUES ('Checkout Button Test', 'checkout_button', true, '{"A": 50, "B": 50}');
```

### Via Code
```php
AbTest::create([
    'name' => 'Checkout Button Test',
    'key' => 'checkout_button',
    'is_active' => true,
    'variants' => ['A' => 50, 'B' => 50], // 50/50 split
]);
```

## Usage

### Get Variant
```php
$abTestService = app(AbTestService::class);
$variant = $abTestService->getVariant('checkout_button');

if ($variant === 'A') {
    // Show variant A
} else {
    // Show variant B
}
```

### Track Events
```php
// Track view
$abTestService->trackEvent('checkout_button', 'view');

// Track click
$abTestService->trackEvent('checkout_button', 'click');

// Track conversion
$abTestService->trackEvent('checkout_button', 'conversion', [
    'order_id' => 123,
    'amount' => 1000.00,
]);
```

### Get Statistics
```php
$stats = $abTestService->getStatistics('checkout_button');

// Returns:
// [
//     'A' => [
//         'assignments' => 100,
//         'views' => 95,
//         'conversions' => 10,
//         'conversion_rate' => 10.53
//     ],
//     'B' => [
//         'assignments' => 100,
//         'views' => 98,
//         'conversions' => 15,
//         'conversion_rate' => 15.31
//     ]
// ]
```

## Example: Testing Checkout Button Colors

### 1. Create Test
```php
AbTest::create([
    'name' => 'Checkout Button Color',
    'key' => 'checkout_button_color',
    'is_active' => true,
    'variants' => ['red' => 50, 'blue' => 50],
]);
```

### 2. In Controller
```php
public function checkout(AbTestService $abTestService)
{
    $variant = $abTestService->getVariant('checkout_button_color');
    $abTestService->trackEvent('checkout_button_color', 'view');
    
    return view('checkout', ['button_color' => $variant]);
}
```

### 3. In Blade Template
```blade
<button 
    class="btn btn-{{ $button_color }}"
    onclick="trackClick()"
>
    Checkout
</button>

<script>
function trackClick() {
    fetch('/api/ab-test/track', {
        method: 'POST',
        body: JSON.stringify({
            test_key: 'checkout_button_color',
            event_type: 'click'
        })
    });
}
</script>
```

### 4. Track Conversion
```php
public function orderPlaced(AbTestService $abTestService)
{
    $abTestService->trackEvent('checkout_button_color', 'conversion', [
        'order_id' => $order->id,
    ]);
}
```

## Best Practices

1. **Test One Thing**: Only test one variable at a time
2. **Statistical Significance**: Wait for enough data before making decisions
3. **Clear Hypothesis**: Know what you're testing and why
4. **Track Right Events**: Track meaningful events (conversions, not just views)
5. **Monitor Results**: Regularly check statistics

