Helping ordinary people create extraordinary websites!

Conduct Web experiments using PHP, Part 1

By Paul Meagher
2005-03-16

Logging responses
The logResponse() method logs the number of offer views and whether an offer response occurred. It also logs when the visitor first viewed the offer and when the visitor responded to the offer.

In addition to these logging actions, this method also updates the StimulusResponse2D object with information about:

• Whether a stimulus code already exists for the visitor (through the generated flag)
• Whether the visitor responded to the offer (through the success flag)
• What stimulus codes to use (through the codes array)

Many of these updating actions are performed in the response-logging method because a table lookup always needs to occur in order to properly log a response. And you can satisfy two needs by using the retrieved visitor information to set variables needed later in your script.

Listing 8. Source code for logResponse method


<?php

require_once "StimulusResponse.php";

class StimulusResponse2D extends StimulusResponse {

function logResponse($vis_id, $resp_col, $response=false) {
global $db;
list($factor1, $factor2) = array_keys($this->factors);
$sql = " SELECT id, $factor1, $factor2, $resp_col ";
$sql .= " FROM $this->table ";
$sql .= " WHERE vis_id='$vis_id' 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];
// See if a response exists
if (strlen($row[$resp_col]) >= 1) {
$this->success = true;
} else {
// If response does not already exist, then see if the response
// string has been set to something other than false. If so, then
// update the stimulus table with the response value.
if ($response != false) {
$sql = " UPDATE $this->table ";
$sql .= " SET $resp_col='$response', timer_stop=now() ";
$sql .= " WHERE vis_id='$vis_id' ";
$result = $db->query($sql);
if (DB::isError($result)) {
die($result->getMessage());
} else {
$this->success = true;
}
} else {
$sql = " UPDATE $this->table SET views=views+1 WHERE vis_id='$vis_id' ";
$result = $db->query($sql);
if (DB::isError($result)) {
die($result->getMessage());
}
$this->success = false;
}
}
$this->generated = true;
} else {
$this->generated = false;
}
}

}

?>






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