-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabtest.class.php
More file actions
171 lines (145 loc) · 4.96 KB
/
abtest.class.php
File metadata and controls
171 lines (145 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/* Make sure you run config.php in your browser first */
class ABTest
{
public $db_server;
public $db_username;
public $db_password;
public $db_database;
public $db_table;
// Sets the test_name
function __construct($test_name = NULL){
if($test_name != NULL) $this->test_name = $test_name;
}
/*
* addOption($value, $weight): Adds an option into the ABTest
*
* ARGS
* @value str The name of the option
* @weight (opt) float How often the option should appear compared to the other options, defaults to 1
*
*/
function addOption($value, $weight = 1){
$this->options->$value = $weight;
}
/*
* selectOption(): Takes the ABTest parameters, outputs an option at random, and records an impression in the db
*
* REQUIRES
* @options array The set of options to test against each other defined by addOption()
* @test_name str Used to organize impressions and conversions for each option
*
* GENERATES
* option_key str A key generated from the strings of the option and test name
* success bool Whether or not the query was successful
*
* RETUNS
* selected_option str Randomly selected option based on respective weights
*
*/
function selectOption(){
if($this->test_name == NULL){
$this->errors[] = 'test_name not defined';
}
// This section selects an option at random given the weights provided
// 1. Get the sum of the options' weights
foreach($this->options as $val){
$sum += $val;
}
// 2. Get a float between 0 and the sum of the options' weights
$random = mt_rand(0, $sum * 100) / 100;
// 3. Iterate through the weights concatenating them until the current value is greater than the random number
foreach($this->options as $key => $val){
$currentValue += $val;
if($currentValue >= $random){
$selected_option = $this->selected_option = $key;
break;
}
}
$this->option_key = md5($this->test_name.'|'.$this->selected_option);
// We picked an option so add an impression if the option exists in the db or create a new row for it
$mysqli = $this->db_connect();
$sql = 'INSERT INTO '.$this->db_table.' ( test_key, test_name, `option`, weight, impressions ) VALUES ( "'
.$this->option_key.'", "'.$this->test_name.'", "'.$this->selected_option.'", "'.$this->options->$selected_option.'", 1 )'
.' ON DUPLICATE KEY UPDATE impressions = impressions + 1, weight = "'.$this->options->$selected_option.'"';
$this->success = $mysqli->query($sql);
$mysqli->close();
return $this->selected_option;
}
/*
* selectAll(): Marks an impression in the DB for all of the options set with addOption(). Good if you're testing all options simultaneously
*
* REQUIRES
* @options array The set of options to test against each other defined by addOption()
* @test_name str Used to organize impressions and conversions for each option
*
* RETUNS
* success str Returns TRUE if all of the SQL queries were executed successfully
*
*/
function selectAll(){
$mysqli = $this->db_connect();
foreach($this->options as $key => $val){
$option_key = md5($this->test_name.'|'.$key);
$sql = 'INSERT INTO '.$this->db_table.' ( test_key, test_name, `option`, weight, impressions ) VALUES ';
$sql .= '( "'.$option_key.'", "'.$this->test_name.'", "'.$key.'", "'.$val.'", "1" ) ';
$sql .= 'ON DUPLICATE KEY UPDATE impressions = impressions + 1, weight = "'.$val.'"';
$success = ($mysqli->query($sql)) ? 1 : 0;
$total_successful += $success;
}
$mysqli->close();
return $this->success = ($total_successful < count($this->options) - 1) ? 1 : 0;
}
/*
* markConversion(): increments the number of conversions for a given option by 1
*
* PARAMS
* @option_key str the test key
*
* RETURNS
* success bool Whether or not the query was successful
*
*/
function markConversion($option_key){
// Update the SQL db
$mysqli = $this->db_connect();
$sql = 'UPDATE '.$this->db_table.' SET conversions = conversions + 1 WHERE test_key = "'.$option_key.'"';
$this->success = $mysqli->query($sql);
$mysqli->close();
return $this->success;
}
/*
* getTestResults(): input a test name and return the results
*
* REQUIRES
* @test_name str The name of the test you want to know about
*
* RETURNS
* results arr Object containing all database fields for all options in the given test
*
*/
function getTestResults(){
$mysqli = $this->db_connect();
$sql = "SELECT * FROM $this->db_table WHERE test_name = '$this->test_name'";
$res = $mysqli->query($sql);
if($res){
$i = 0;
while($row = $res->fetch_assoc()){
foreach($row as $key => $val){
$results[$i][$key] = $val;
}
$i++;
}
}
return $results;
}
/*
* Connects to the MySQL database and returns a MySQLi object
*/
function db_connect(){
$mysqli = new mysqli($this->db_server, $this->db_username, $this->db_password, $this->db_database);
if($mysqli->connect_errno) $this->errors[] = "Connect failed: ".$mysqli->connect_error;
return $mysqli;
}
}
?>