๐Ÿงช Complete A/B Testing Demo

Demonstrating Phases 1-4: Core + Server + Client + Analytics

๐Ÿ“ก Phase 2: Server-Side Detection (SSR)

Middleware assigned variant: This happens on the server BEFORE React renders

โœ“ Middleware ran at: 2026-03-25T16:49:54.628Z
โœ“ Server detected variant: none
โœ“ Cookie was set in middleware
โœ“ Available in getServerSideProps via isVariantServer()
View Server Code
export const getServerSideProps = withAbTesting(async (context) => {
  // Access variant server-side
  const variant = getVariantServer(context.req, 'example')
  
  // Can use for:
  // - Conditional data fetching
  // - Server-side analytics
  // - Redirects
  // - SEO optimization
  
  return {
    props: {
      serverDetectedVariant: variant,
      timestamp: new Date().toISOString()
    }
  }
})

โš›๏ธ Phase 3: Client-Side Hooks (React)

useAbTest hook: Components can access A/B tests directly, no prop drilling!

โณ Loading (hydration in progress)...
View Component Code
function MyComponent() {
  const { variant, isVariant, loading } = useAbTest('example')
  
  if (loading) return <Skeleton />
  
  return isVariant('variant') ? (
    <NewExperience />
  ) : (
    <OriginalExperience />
  )
}

๐Ÿ“Š Phase 4: Analytics Tracking (GTM)

Automatic tracking to Google Tag Manager for data analysis:

โœ… Auto-tracked Events:
  • ab_test_assignment - Fires when user gets assigned to a variant (automatic)
  • ab_test_conversion - Fires when user completes a goal (manual)
Try Conversion Tracking:
View Analytics Code
// 1. Automatic assignment tracking (happens in AbProvider)
// When user gets assigned to a variant, event fires automatically:
// {
//   event: 'ab_test_assignment',
//   ab_test_name: 'example',
//   ab_test_variant: 'control'
// }

// 2. Manual conversion tracking
import { useAbConversion } from '@/common/hooks/useAbConversion'

function MyComponent() {
  const trackConversion = useAbConversion('example')
  
  const handleSignup = async () => {
    await submitSignupForm()
    trackConversion('signup') // Track the conversion
  }
  
  const handlePurchase = async () => {
    const amount = await processPurchase()
    trackConversion('purchase', amount) // Track with value
  }
  
  return (
    <>
      <button onClick={handleSignup}>Sign Up</button>
      <button onClick={handlePurchase}>Purchase</button>
    </>
  )
}
๐Ÿ’ก In Production:
  • Events automatically flow to Google Analytics
  • Analyze conversion rates by variant
  • Calculate statistical significance
  • Make data-driven decisions

๐ŸŽจ Conditional Rendering Example

๐Ÿ“Š Control Experience

You're seeing the ORIGINAL version of the page.

  • Classic layout
  • Standard colors
  • Familiar experience

๐Ÿช† No Prop Drilling Demo

Deeply nested components can access A/B tests without passing props:

Parent Component
(doesn't know about A/B tests)
Middle Component
(also doesn't know about A/B tests)
Deep Nested Component
โœ… Can access A/B test directly with useAbTest()
Current variant:

๐Ÿ› ๏ธ Developer Tools

Manual overrides for testing and debugging:

View Code
const { overrideTest } = useAbContext()

// Override a test variant
overrideTest('example', 'variant')

// Cookie is automatically updated
// Components re-render with new variant

๐Ÿ”— Query Parameter Overrides

URL parameters automatically override variants (set by middleware):

โœ“ Middleware parses ab_* query params
โœ“ Validates variant against config
โœ“ Updates cookie if valid
โœ“ Works on every page automatically

๐Ÿ“Š Current State

Server Assignments

From getServerSideProps (Phase 2)

{
  "edge_ai_exit_intent": "variant"
}

Client Assignments

From AbContext (Phase 3)

{
  "edge_ai_exit_intent": "variant"
}
๐Ÿ’ก Note: These should be identical! Server sets the cookie, client reads it. This proves the full-stack integration is working.