Initial commit
This commit is contained in:
86
pluf/src/Pluf/AB/Form/MarkWinner.php
Normal file
86
pluf/src/Pluf/AB/Form/MarkWinner.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Mark the winner of a test.
|
||||
*
|
||||
* This form is not used to display the form, only to validate and
|
||||
* process it.
|
||||
*
|
||||
*/
|
||||
class Pluf_AB_Form_MarkWinner extends Pluf_Form
|
||||
{
|
||||
protected $test = null; /**< Store the test retrieved during validation. */
|
||||
|
||||
public function initFields($extra=array())
|
||||
{
|
||||
$this->fields['test'] = new Pluf_Form_Field_Varchar(
|
||||
array('required' => true)
|
||||
);
|
||||
$this->fields['alt'] = new Pluf_Form_Field_Integer(
|
||||
array('required' => true,
|
||||
'min' => 0,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the test exists, is active and the corresponding
|
||||
* alternative exists too.
|
||||
*
|
||||
* The validation is at the global level to prevent the need of a
|
||||
* form per test and simplify the dashboard design.
|
||||
*/
|
||||
public function clean()
|
||||
{
|
||||
$db = Pluf_AB::getDb();
|
||||
$test = $db->tests->findOne(array('_id' => $this->cleaned_data['test']));
|
||||
if ($test == null) {
|
||||
throw new Pluf_Form_Invalid(__('The test has not been found.'));
|
||||
}
|
||||
if (!$test['active']) {
|
||||
throw new Pluf_Form_Invalid(__('The test is already inactive.'));
|
||||
}
|
||||
if (!isset($test['alts'][$this->cleaned_data['alt']])) {
|
||||
throw new Pluf_Form_Invalid(__('This alternative is not available.'));
|
||||
}
|
||||
// Good we have the test and the right alternative
|
||||
$this->test = $test;
|
||||
return $this->cleaned_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the test.
|
||||
*
|
||||
* @return array Test.
|
||||
*/
|
||||
function save($commit=true)
|
||||
{
|
||||
$this->test['winner'] = $this->cleaned_data['alt'];
|
||||
$this->test['active'] = false;
|
||||
$this->test['stop_dtime'] = gmdate('Y-m-d H:i:s');
|
||||
$db = Pluf_AB::getDb();
|
||||
$db->tests->update(array('_id'=> $this->cleaned_data['test']),
|
||||
$this->test);
|
||||
return $this->test;
|
||||
}
|
||||
}
|
219
pluf/src/Pluf/AB/Funnel.php
Normal file
219
pluf/src/Pluf/AB/Funnel.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Funnel statistics.
|
||||
*
|
||||
* Funnels are easy to track but not that easy to generate statistics
|
||||
* out of them.
|
||||
*
|
||||
* Stats are compiled "by GMT day", so you can track your funnel per
|
||||
* day, week or more. Stats are put in cache in the "funnels" collection.
|
||||
*
|
||||
*/
|
||||
class Pluf_AB_Funnel
|
||||
{
|
||||
/**
|
||||
* Returns the list of funnels.
|
||||
*
|
||||
* @return array Funnels
|
||||
*/
|
||||
public static function getFunnels()
|
||||
{
|
||||
$db = Pluf_AB::getDb();
|
||||
foreach (array('f', 't') as $k) {
|
||||
// Once created, it will return immediately in the future
|
||||
// calls so the overhead is negligeable.
|
||||
$db->funnellogs->ensureIndex(array($k => 1),
|
||||
array('background' => true));
|
||||
}
|
||||
$nf = $db->command(array('distinct' => 'funnellogs', 'key' => 'f'));
|
||||
if ((int) $nf['ok'] == 1) {
|
||||
sort($nf['values']);
|
||||
return $nf['values'];
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the properties for a given period.
|
||||
*/
|
||||
public static function getFunnelProps($funnel, $period='today')
|
||||
{
|
||||
$db = Pluf_AB::getDb();
|
||||
switch ($period) {
|
||||
case 'yesterday':
|
||||
$q = array('t' => (int) gmdate('Ymd', time()-86400));
|
||||
break;
|
||||
case 'today':
|
||||
$q = array('t' => (int) gmdate('Ymd'));
|
||||
break;
|
||||
case '7days':
|
||||
$q = array('t' => array('$gte' => (int) gmdate('Ymd', time()-604800)));
|
||||
break;
|
||||
case 'all':
|
||||
default:
|
||||
$q = array();
|
||||
break;
|
||||
}
|
||||
$q['f'] = $funnel;
|
||||
$props = array();
|
||||
foreach ($db->funnellogs->find($q) as $log) {
|
||||
foreach ($log['p'] as $prop => $v) {
|
||||
if (isset($props[$prop]) and !in_array($v, $props[$prop])) {
|
||||
$props[$prop][] = $v;
|
||||
} else {
|
||||
$props[$prop] = array($v);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $props;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stats for a given funnel.
|
||||
*
|
||||
* @param $funnel string Funnel
|
||||
* @param $period string Time period 'yesterday', ('today'), '7days', 'all'
|
||||
* @param $prop string Property to filter (null)
|
||||
*/
|
||||
public static function getStats($funnel, $period='today', $prop=null)
|
||||
{
|
||||
$db = Pluf_AB::getDb();
|
||||
$steps = array();
|
||||
for ($i=1;$i<=20;$i++) {
|
||||
$steps[$i] = array();
|
||||
}
|
||||
switch ($period) {
|
||||
case 'yesterday':
|
||||
$q = array('t' => (int) gmdate('Ymd', time()-86400));
|
||||
break;
|
||||
case 'today':
|
||||
$q = array('t' => (int) gmdate('Ymd'));
|
||||
break;
|
||||
case '7days':
|
||||
$q = array('t' => array('$gte' => (int) gmdate('Ymd', time()-604800)));
|
||||
break;
|
||||
case 'all':
|
||||
default:
|
||||
$q = array();
|
||||
break;
|
||||
}
|
||||
$q['f'] = $funnel;
|
||||
if ($prop) {
|
||||
$q['p.'.$prop] = array('$exists' => true);
|
||||
}
|
||||
$uids = array();
|
||||
// With very big logs, we will need to find by schunks, this
|
||||
// will be very easy to adapt.
|
||||
foreach ($db->funnellogs->find($q) as $log) {
|
||||
if (!isset($uids[$log['u'].'##'.$log['s']])) {
|
||||
if ($prop and !isset($log['p'][$prop])) {
|
||||
continue;
|
||||
}
|
||||
$uids[$log['u'].'##'.$log['s']] = true;
|
||||
$step = $log['s'];
|
||||
$steps[$step]['name'] = $log['sn'];
|
||||
$steps[$step]['id'] = $log['s'];
|
||||
if ($prop and !isset($steps[$step]['props'])) {
|
||||
$steps[$step]['props'] = array();
|
||||
}
|
||||
$steps[$step]['total'] = (isset($steps[$step]['total'])) ?
|
||||
$steps[$step]['total'] + 1 : 1;
|
||||
if ($prop) {
|
||||
$steps[$step]['props'][$log['p'][$prop]] = (isset($steps[$step]['props'][$log['p'][$prop]])) ?
|
||||
$steps[$step]['props'][$log['p'][$prop]] + 1 : 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Now, compile the stats for steps 2 to n
|
||||
// First, we find the "max" for the reference number of
|
||||
// visitors along this funnel. This is $t1 and $tprops[prop]
|
||||
$t1 = 0;
|
||||
foreach ($steps as $step) {
|
||||
if (isset($step['total']) and $step['total'] > $t1) {
|
||||
$t1 = $step['total'];
|
||||
}
|
||||
if ($prop and isset($step['props'])) {
|
||||
foreach ($step['props'] as $v => $t) {
|
||||
if (!isset($tprops[$v])) {
|
||||
$tprops[$v] = $t;
|
||||
continue;
|
||||
}
|
||||
if ($tprops[$v] < $t) {
|
||||
$tprops[$v] = $t;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($t1 == 0) {
|
||||
return array();
|
||||
}
|
||||
$prev_step = null;
|
||||
for ($i=1;$i<=20;$i++) {
|
||||
if ($prev_step == null) {
|
||||
|
||||
}
|
||||
if ($steps[$i]) {
|
||||
$tp = $prev_step['total'];
|
||||
$tn = $steps[$i]['total'];
|
||||
if ($tp) {
|
||||
$steps[$i]['conv'] = sprintf('%01.2f%%', 100.0 - (float)($tp-$tn)/$tp*100.0);
|
||||
} else {
|
||||
$steps[$i]['conv'] = 'N/A';
|
||||
}
|
||||
if ($t1) {
|
||||
$steps[$i]['conv1'] = sprintf('%01.2f%%', 100.0 - (float)($t1-$tn)/$t1*100.0);
|
||||
} else {
|
||||
$steps[$i]['conv1'] = 'N/A';
|
||||
}
|
||||
if ($prop) {
|
||||
$steps[$i]['sprops'] = array();
|
||||
$steps[$i]['sprops1'] = array();
|
||||
foreach ($steps[$i]['props'] as $v => $t) {
|
||||
if (!isset($tprops[$v])) {
|
||||
$tprops[$v] = $t;
|
||||
}
|
||||
$pv = isset($prev_step['props'][$v]) ? $prev_step['props'][$v] : 0;
|
||||
$steps[$i]['sprops'][$v] = array($t, $pv);
|
||||
$steps[$i]['sprops1'][$v] = array($t, $tprops[$v]);
|
||||
if ($pv) {
|
||||
$steps[$i]['sprops'][$v][] = round(100*(float)$t/(float)$pv,2).'%';
|
||||
} else {
|
||||
$steps[$i]['sprops'][$v][] = 0;
|
||||
}
|
||||
$steps[$i]['sprops1'][$v][] = round(100*(float)$t/(float)$tprops[$v],2).'%';
|
||||
}
|
||||
}
|
||||
$steps[$i]['bigtotal'] = $t1;
|
||||
$prev_step = $steps[$i];
|
||||
}
|
||||
}
|
||||
for ($i=20;$i>=1;$i--) {
|
||||
if (!$steps[$i]) {
|
||||
unset($steps[$i]); // We remove the inexisting steps
|
||||
}
|
||||
}
|
||||
return $steps;
|
||||
}
|
||||
}
|
176
pluf/src/Pluf/AB/Views.php
Normal file
176
pluf/src/Pluf/AB/Views.php
Normal file
@@ -0,0 +1,176 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
Pluf::loadFunction('Pluf_Shortcuts_RenderToResponse');
|
||||
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
|
||||
|
||||
/**
|
||||
* Manage and visualize the tests.
|
||||
*
|
||||
* It is possible to mark a test as inactive by picking a
|
||||
* winner.
|
||||
*
|
||||
* Check the urls.php file for the URL definition to integrate the
|
||||
* dashboard in your application/project.
|
||||
*
|
||||
* The permission used are:
|
||||
*
|
||||
* Pluf_AB.view-dashboard: The user can view the dasboard.
|
||||
* Pluf_AB.edit-test: The user can edit a test.
|
||||
*
|
||||
*/
|
||||
class Pluf_AB_Views
|
||||
{
|
||||
/**
|
||||
* Display the currently running tests.
|
||||
*
|
||||
* The name of the view in the urls must be 'pluf_ab_dashboard'.
|
||||
*/
|
||||
public $dasboard_precond = array(array('Pluf_Precondition::hasPerm',
|
||||
'Pluf_AB.view-dashboard'));
|
||||
public function dashboard($request, $match)
|
||||
{
|
||||
$url = Pluf_HTTP_URL_urlForView('pluf_ab_dashboard');
|
||||
$can_edit = $request->user->hasPerm('Pluf_AB.edit-test');
|
||||
if ($can_edit && $request->method == 'POST') {
|
||||
// We mark the winner.
|
||||
$form = new Pluf_AB_Form_MarkWinner($request->POST);
|
||||
if ($form->isValid()) {
|
||||
$form->save();
|
||||
$request->user->setMessage(__('The test has been updated.'));
|
||||
return new Pluf_HTTP_Response_Redirect($url);
|
||||
}
|
||||
} else {
|
||||
// To have it available for the control of the errors in
|
||||
// the template.
|
||||
$form = new Pluf_AB_Form_MarkWinner();
|
||||
}
|
||||
// Get the list of tests
|
||||
$db = Pluf_AB::getDb();
|
||||
$active = array();
|
||||
$stopped = array();
|
||||
foreach ($db->tests->find() as $test) {
|
||||
$test['stats'] = Pluf_AB::getTestStats($test);
|
||||
if ($test['active']) {
|
||||
$active[] = $test;
|
||||
} else {
|
||||
$stopped[] = $test;
|
||||
}
|
||||
}
|
||||
return Pluf_Shortcuts_RenderToResponse('pluf/ab/dashboard.html',
|
||||
array('active' => $active,
|
||||
'stopped' => $stopped,
|
||||
'form' => $form,
|
||||
'can_edit' => $can_edit,
|
||||
),
|
||||
$request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the list of funnels.
|
||||
*
|
||||
*/
|
||||
public $funnels_precond = array(array('Pluf_Precondition::hasPerm',
|
||||
'Pluf_AB.view-funnels'));
|
||||
public function funnels($request, $match)
|
||||
{
|
||||
$url = Pluf_HTTP_URL_urlForView('pluf_ab_funnels');
|
||||
$funnels = Pluf_AB_Funnel::getFunnels();
|
||||
return Pluf_Shortcuts_RenderToResponse('pluf/ab/funnels.html',
|
||||
array('funnels' => $funnels,
|
||||
),
|
||||
$request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a given funnel stats.
|
||||
*
|
||||
*/
|
||||
public $funnel_precond = array(array('Pluf_Precondition::hasPerm',
|
||||
'Pluf_AB.view-funnels'));
|
||||
public function funnel($request, $match)
|
||||
{
|
||||
$periods = array('yesterday' => __('Yesterday'),
|
||||
'today' => __('Today'),
|
||||
'7days' => __('Last 7 days'),
|
||||
'all' => __('All time'));
|
||||
$period = 'today';
|
||||
$nperiod = $periods[$period];
|
||||
if (isset($request->REQUEST['p'])
|
||||
and isset($periods[$request->REQUEST['p']])) {
|
||||
$period = $request->REQUEST['p'];
|
||||
$nperiod = $periods[$request->REQUEST['p']];
|
||||
}
|
||||
$props = Pluf_AB_Funnel::getFunnelProps($match[1], $period);
|
||||
$prop = null;
|
||||
if (isset($request->REQUEST['prop']) and
|
||||
in_array($request->REQUEST['prop'], array_keys($props))) {
|
||||
$prop = $request->REQUEST['prop'];
|
||||
}
|
||||
$stats = Pluf_AB_Funnel::getStats($match[1], $period, $prop);
|
||||
return Pluf_Shortcuts_RenderToResponse('pluf/ab/funnel.html',
|
||||
array('stats' => $stats,
|
||||
'funnel' => $match[1],
|
||||
'nperiod' => $nperiod,
|
||||
'period' => $period,
|
||||
'props' => $props,
|
||||
'prop' => $prop,
|
||||
),
|
||||
$request);
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple view to redirect a user and convert it.
|
||||
*
|
||||
* To convert the user for the test 'my_test' and redirect it to
|
||||
* the URL 'http://www.example.com' add the following view in your
|
||||
* urls.php:
|
||||
*
|
||||
* <pre>
|
||||
* array('regex' => '#^/goto/example/$#',
|
||||
* 'base' => $base,
|
||||
* 'model' => 'Pluf_AB_Views',
|
||||
* 'method' => 'convRedirect',
|
||||
* 'name' => 'go_to_example',
|
||||
* 'params' => array('url' => 'http://www.example.com',
|
||||
* 'test' => 'my_test')
|
||||
* );
|
||||
* </pre>
|
||||
*
|
||||
* Try to put a url which reflects the final url after redirection
|
||||
* to minimize the confusion for the user. In this example, in
|
||||
* your code or template you use the named url 'go_to_example'.
|
||||
*
|
||||
*/
|
||||
public function convRedirect($request, $match, $p)
|
||||
{
|
||||
Pluf_AB::convert($p['test'], $request);
|
||||
return new Pluf_HTTP_Response_Redirect($p['url']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user