<?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 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' => array('$eq' => (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;β |
$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']])) {β |
$uids[$log['u'].'##'.$log['s']] = true;β |
$step = $log['s'];β |
$steps[$step]['name'] = $log['sn'];β |
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β |
for ($i=2;$i<=20;$i++) {β |
if ($steps[$i] and $steps[$i-1]) {β |
//$steps[$i]['conv'] = sprintf('%d', (float)$steps[$i-1]['total']/$steps[$i]['total']*100.0);β |
$steps[$i]['conv'] = sprintf('%01.2f%%', (float)$steps[$i-1]['total']/$steps[$i]['total']*100.0);β |
$steps[$i]['conv1'] = sprintf('%01.2f%%', (float)$steps[$i-1]['total']/$steps[1]['total']*100.0);β |
}β |
}β |
β |
return $steps;β |
}β |
}β |