PHP code example of lwc / kumite

1. Go to this page and download the library: Download lwc/kumite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lwc / kumite example snippets




// some controller action
function productPage()
{
  // place users into variants based on a query string value. Useful for integrating with external tools.
  Kumite::start('pricing-test', null, $_GET['v']);
}



// defined elsewhere
class MyRandomAllocator implements Kumite\Allocator
{
  // selects a variant at random. Everyone gets tagged with a variant.
  public funciton allocate($variantKeys)
  {
    return array_rand($variantKeys);
  }
}

// some controller action
function productPage()
{
  Kumite::start('pricing-test', null, new MyRandomAllocator());
}



// some controller action
function productPage()
{
  $user = getActiveUser();
  $metadata = array(
    'userId' => isset($user) ? $user->id : null
  );
  // place users into variants based on user id (why, who knows?!). Logged out users are disqualified from participating
  Kumite::start('pricing-test', $metadata, function($variantKeys) use($user) {
    if ($user) {
      return $variantKeys[$user->id() % count($variantKeys)]; 
    }
    return null; // user is logged out
  });
}


 if (Kumite::variant('pricing-test') == 'lowerprice'): 


function invoicePage()
{
  // ...
  $sale->save();
  // user made a purchase, track the event in kumite
  Kumite::event('pricing-test', 'sale', array('amount' => $sale->amount()));
}



$config = array(
  'pricing-test' => array(
  'allocator' => new Allocators\UCB1Allocator('purchase') // there are several / defines the default variant, served to request not participating in the test. Typically the control.
    'variants' => array(
      'control', // this variant defines no properties
      'lowerprice' => array('price' => '$300') // this variant defines properties
    ),
    'events' => array('purchase', 'refund')
  )
);



Kumite::setup(array(
  'storageAdapter' => new MyStorageAdapter(),
  'cookieAdapter' => new MyCookieAdapter(), // Optional, defaults to using PHP's $_COOKIE if not provided
  'tests' => function() {
    


class MyStorageAdapter implements Kumite\Adapters\StorageAdapter
{
  /**
	 * @return participantId
	 */
	public function createParticipant($testKey, $variantKey)
	{
		$participant = new KumiteParticipant(array(
			'test' => $testKey,
			'variant' => $variantKey
		));
		$participant->save();
		return $participant->id(); // Now we have a participant id, courtesy of the database 
	}

	public function createEvent($testKey, $variantKey, $eventKey, $participantId, $metadata=null)
	{
		$event = new KumiteEvent(array(
			'test' => $testKey,
			'variant' => $variantKey,
			'event' => $eventKey,
			'participantId' => $participantId,
			'metadata' => $metadata			
		));
		$event->save();
	}
	
	// used for results and for intelligent allocators, such as UCB1
	public function countParticipants($testKey, $variantKey)
	{
		return Participant::getTotalForVariant($testKey, $variantKey);
	}
	
	public function countEvents($testKey, $variantKey, $eventKey)
	{
		return Event::getTotalForEvent($testKey, $variantKey, $eventKey);
	}
}



class PhpCookieAdapter implements Kumite\Adapters\CookieAdapter
{
  public function getCookies()
	{
		return $_COOKIE;
	}

	public function getCookie($name)
	{
		return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
	}

	public function setCookie($name, $data)
	{
		setcookie($name, $data);
	}
}