forked from aliasaria/PHP-redis-A-B-Testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.php
377 lines (262 loc) · 9.17 KB
/
report.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
//include the redis connector
include('redis/redis.php');
include('config/configure.php');
//bring in the custom defined metrics
include('config/metrics.php');
include('config/tests.php');
include('lib/metrics.php');
include('lib/tests.php');
include('lib/report.php');
//do auth
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo '<p style=\"font-family: Zapfino, cursive;\">Text to send if user hits Cancel button</p>';
exit;
} else if (
$_SERVER['PHP_AUTH_USER'] != $ab_config['ADMIN_USERNAME'] &&
$_SERVER['PHP_AUTH_PW'] != $ab_config['ADMIN_PASSWORD']
)
{
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo "hi";
exit;
} else {
//echo "<p style=\"font-family: Zapfino, cursive;\">Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
//echo "<p style=\"font-family: Zapfino, cursive;\">You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
//connect to redis
$r = new Redis($ab_config['redis_host']);
$connected = $r->connect();
if ($connected)
{
$r->select_db($ab_config['redis_db_number']);
//$r->flushdb();
}
else
{
echo "<p>couldn't connect to redis</p>";
die();
}
//do forced alternative logic if necessary
if (isset($_GET['force']))
{
$test = $_GET['test'];
$alt = $_GET['alt'];
if ($alt != "!!null!!")
ab_tests_force_alternative(urldecode($test), urldecode($alt));
else
ab_tests_force_alternative(urldecode($test), null);
//redirect to remove get params from URL
header('Location: ' . $_SERVER['PHP_SELF']);
}
//clear a specific test's data
if (isset($_GET['clear-test']))
{
$test = $_GET['test'];
erase_data_for_test(urldecode($test));
//redirect to remove get params from URL
header('Location: ' . $_SERVER['PHP_SELF']);
}
//clear all data if requested -- but maintain forced tests
if (isset($_GET['clear-all-data']))
{
erase_all_keys_except_forced_keys();
//redirect to remove get params from URL
header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
<html>
<head><title>a/b testing report</title></head>
<!--[if IE]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
<script language="javascript" type="text/javascript" src="js/flot/jquery.js"></script>
<script language="javascript" type="text/javascript" src="js/flot/jquery.flot.js"></script>
<link href="css/report.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/buttons.css" media="all" rel="stylesheet" type="text/css" />
<body>
<h1>a/b testing report</h1>
<hr/>
<?php
$i = 0;
foreach ($ab_tests as $test)
{
$name = $test['name'];
$alternatives = $test['alternatives'];
$forced_alternative = ab_tests_get_forced_alternative($name);
$num_alternatives = count($alternatives);
echo "<a name=\"$name\"><h2>" . $name . "</h2></a>";
//echo "<P class=\"indent\" style='color: #556; font-size: 12px'>There are " . $num_alternatives . " alternatives for this test";
echo "<P class=\"indent\" style='color: #556; font-size: 12px'>" . $test['description'];
if ($forced_alternative != null)
{
//echo "<P class=\"indent\" style='color: #999; font-size: 15px; margin-top: 12px'>" . "Currently forced to show option <u style='color: #666'>$forced_alternative</u> for all participants";
/*echo ' <a href="?force=true&test=' . $test['name'] . '&alt=' . "!!null!!" . '" class="red-button pcb">
<span style="color: black;">Clear</span>
</a>'; */
}
$j = 0;
$a = array();
$total_converted = 0;
$total_conversions = 0;
$total_particants = 0;
$array_of_conversion_rates = array();
echo "<div class=\"alternatives\">";
foreach ($alternatives as $alt)
{
$p = $a[$alt]['participants'] = (int)ab_tests_total_participants_for_alternative($name,$alt);
$c1 = $a[$alt]['conversions'] = (int)ab_tests_total_conversions_for_alternative($name,$alt);
$c2 = $a[$alt]['converted'] = (int)ab_tests_total_converted_for_alternative($name,$alt);
if ($p > 0)
$conv_rate = $c2 / $p * 100;
else
$conv_rate = 0;
$conv_rate = round($conv_rate,3);
$array_of_conversion_rates[] = array( $j+1,$conv_rate);
echo "<h3>Alternative <span class=\"alternative\">" . $alt . "</span>:";
if ($forced_alternative != $alt)
{
echo ' <a href="?force=true&test=' . urlencode($test['name']) . '&alt=' . urlencode($alt) . '#'. urlencode($name) .'" class="grey-button pcb">
<span style="color: #666;">Show Always</span>
</a>';
}
else
{
echo ' <span class="forced-text">Always Showing</span> <a href="?force=true&test=' . urlencode($test['name']) . '&alt=' . "!!null!!" . '#'. urlencode($name) .'" class="red-disabled-button pcb">
<span style="color: #333;">Clear</a>
</span>';
}
echo "</h3>\n";
echo "<p class=\"results\">$p Participants, $c1 conversions, $c2 converted\n";
echo "<p class=\"results\">conversion rate: " . $conv_rate . "%";
$total_particants += $a[$alt]['participants'];
$total_converted += $a[$alt]['converted'];
$j++;
}
echo "</div>";
$total_conversion_rate = ($total_particants > 0)?($total_converted/$total_particants*100):0;
echo "<h3><strong>Total:</strong></h3>";
echo "<p class=\"results\">" . $total_particants . " participants, " . $total_converted . " converted";
echo " = " . round($total_conversion_rate,2) . "% total conversion rate";
//echo 'Test is active: <p><a class="button" href="#"><span>Stop this test</span></a></p><br/><br/>';
$metrics = $test['metrics'];
$metrics = implode(", ", $metrics);
echo "<br/><br/><p class=\"results\" style=''>(For this test,
a conversion is marked by any of these events: <span style='color: #666; font-weight: bold'>$metrics</span>
)</p>";
$chart_data = json_encode($array_of_conversion_rates);
//echo "<pre>";
//print_r ($chart_data);
//echo "</pre>";
echo '
<div id="placeholder'; echo $i; echo '" style="width:300px;height:200px; margin-top: 10px;"></div>
<script id="source" language="javascript" type="text/javascript">
$(function () {
var d1 = '; echo $chart_data; echo ';
var options = {
grid: {
backgroundColor: { colors: ["#ddd", "#ddd"] },
tickColor: "#ccc"
},
yaxis: {
ticks: 5,
min: 0,
xmax: 100
},
xaxis: {
tickSize: 1,
tickDecimals: 0
},
bars:
{show: true, barWidth: 0.6, fill: 0.8 },
};
var data = [
{
color: "#333",
fill: 1,
data: d1,
}
];
var plotarea = $("#placeholder'; echo $i; echo '");
plotarea.css("height", "100px");
plotarea.css("width", "200px");
$.plot(plotarea, data, options);
});
</script>
';
$conclusion = ab_tests_conclusion($test['name']);
echo "<p class=\"indent\" style='color: #556; font-size: 12px'>" . $conclusion . "</p>";
echo '<br /><a href="?clear-test=true&test=' . $test['name'] . '" class="red-disabled-button pcb"><span>Clear This Test</span></a>';
echo "<hr/>";
$i++;
}
?>
<h1>metrics report</h1>
<hr/>
<?php
$i = 0;
foreach ($ab_metrics as $metric)
{
echo '<h2>' . $metric['name'] . '</h2>';
echo "<P class=\"indent\" style='color: #556; font-size: 12px'>" . $metric['description'];
$today = date('Y-m-d');
$sixtydaysago = date('Y-m-d', strtotime("-60 days"));
$d = json_encode(ab_values($metric['name'],$sixtydaysago,$today));
//print_r($d);
echo '
<div id="placeholder2'; echo $i; echo '" style="width:300px;height:200px; margin-top: 10px;"></div>
<script id="source" language="javascript" type="text/javascript">
$(function () {
var d1 = '; echo $d; echo ';
for (var i in d1)
{
//alert(d1[i]);
d1[i] = [ d1[i][0]*1000, d1[i][1] ];
}
//alert(d1);
var options = {
grid: {
backgroundColor: { colors: ["#ddd", "#ddd"] },
tickColor: "#ccc"
},
xaxis: {
mode: "time",
minTickSize: [7, "day"],
xtimeformat: "%y/%m/%d"
},
lines:
{show: true},
series: { lines: { show: true, lineWidth: 2, fill: 0.8, fillColor: "#444"},
points: { show: false, radius: 3 }, shadowSize: 0 },
};
var data = [
{
color: "#333",
data: d1,
xlabel: "y = 5",
fill: true, fillColor: { colors: ["#ddd", "#ddd"] },
}
];
var plotarea = $("#placeholder2'; echo $i; echo '");
plotarea.css("height", "120px");
plotarea.css("width", "450px");
$.plot(plotarea, data, options);
});
</script>
';
$i ++;
}
echo "</div>";
?>
<hr/>
<a href="?clear-all-data=true" class="red-button pcb"><span>Clear All Data</span></a>
<?php
$info = $r->info();
$mem = $info['used_memory'];
$mem = round($mem / 1024, 1);
$num_keys = $r->dbsize();
echo "<br><br><p style='color: #556; font-size: 12px'>Memory used: $mem KBytes, Number of keys: $num_keys";
?>
</body>
</html>