Helping ordinary people create extraordinary websites!

Conduct Web experiments using PHP, Part 1

By Paul Meagher
2005-03-16

Assigning stimulus codes
The end result of invoking the generate_2d.php script is that you have all your stimulus codes set up in a proper factorial design format. You also have a random number between 0 and 1 associated with each stimulus code. This number is used to randomly select a stimulus code for a new visitor to your site. The getRandomStimulusCode() method is responsible for the random assignment of stimulus codes to new visitors, as shown in Listing 6.

Listing 6. Source of getRandomStimulusCode.php method



<?php

require_once "StimulusResponse.php";

class StimulusResponse2D extends StimulusResponse {

function getRandomStimulusCode($vis_id) {
global $db;
list($factor1, $factor2) = array_keys($this->factors);
$sql = " SELECT id, $factor1, $factor2 FROM $this->table ";
$sql .= " WHERE used='n' ORDER BY rand LIMIT 1 ";
$result = $db->query($sql);
if (DB::isError($result)) {
die($result->getMessage());
}
if ($result->numRows()) {
$row = $result->fetchRow();
$this->code[$factor1] = $row[$factor1];
$this->code[$factor2] = $row[$factor2];
$id = $row["id"];
$sql = " UPDATE $this->table ";
$sql .= " SET used='y', vis_id='$vis_id', views=1, timer_start=now() ";
$sql .= " WHERE id='$id' ";
$result = $db->query($sql);
if (DB::isError($result)) {
die($result->getMessage());
} else {
return true;
}
} else {
return false;
}
}

}

?>

Listing 7 displays an except of the demo code that shows the context in 

which the getRandomStimulusCode() method is called.

Listing 7. Context in which getRandomStimulusCode.php method is called


<?php

// Log the response if any.
$response_field = "response";
$sr->logResponse($_COOKIE['vis_id'], $response_field, $_GET['response']);

// If stimulus code not already generated for visitor then get one.
if (!$sr->generated) {
$sr->getRandomStimulusCode($_COOKIE['vis_id']);
}

?>

The logResponse() method does a lookup to see if a stimulus code has already been assigned to the visitor. If so, the generated flag is set to true and the need to generate a random stimulus code is bypassed. The logResponse() method also takes care of retrieving the previously assigned stimulus codes that are used to determine which Web offer version the site visitor is shown.



Tutorial pages:

First published by IBM developerWorks


 1 Votes

You might also want to check these out:


Leave a Comment on "Conduct Web experiments using PHP, Part 1"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS