partial #1447 - Added the chart Churn Rate - per Active User
This commit is contained in:
parent
e6920e9ae2
commit
9646a68866
@ -262,6 +262,75 @@ class Controller_home_charts extends Crunchbutton_Controller_Account {
|
||||
|
||||
break;
|
||||
|
||||
case 'churn-rate-per-active-user':
|
||||
$queryActiveUsers = '';
|
||||
$queryNewUsers = '';
|
||||
|
||||
for( $i = $from -1 ; $i < $to; $i++ ){
|
||||
$week = $allWeeks[ $i ];
|
||||
|
||||
$queryActiveUsers .= $union . "SELECT '{$week}' AS Week,
|
||||
COUNT(*) AS Total
|
||||
FROM
|
||||
( SELECT u.phone,
|
||||
o.date,
|
||||
u.id_user,
|
||||
c.name
|
||||
FROM `order` o
|
||||
INNER JOIN user u ON u.id_user = o.id_user
|
||||
LEFT JOIN community c ON o.id_community = c.id_community
|
||||
WHERE o.date <= STR_TO_DATE('{$week} Saturday', '%X%V %W')
|
||||
AND o.date >= STR_TO_DATE('{$week} Saturday', '%X%V %W') - INTERVAL {$this->activeUsersInterval} DAY
|
||||
{$this->queryExcludeCommunties}
|
||||
{$this->queryExcludeUsers}
|
||||
{$this->queryOnlyCommunties}
|
||||
GROUP BY u.phone) ActiveUsers";
|
||||
|
||||
$queryNewUsers .= $union . "SELECT '{$week}' AS Week,
|
||||
COUNT(*) AS Total
|
||||
FROM
|
||||
(SELECT COUNT(*) orders,
|
||||
u.phone,
|
||||
o.date,
|
||||
u.id_user
|
||||
FROM `order` o
|
||||
INNER JOIN user u ON u.id_user = o.id_user
|
||||
LEFT JOIN community c ON o.id_community = c.id_community
|
||||
WHERE o.date <= STR_TO_DATE('{$week} Saturday', '%X%V %W')
|
||||
{$this->queryExcludeCommunties}
|
||||
{$this->queryExcludeUsers}
|
||||
GROUP BY u.phone HAVING orders = 1) Orders
|
||||
WHERE Orders.date BETWEEN STR_TO_DATE('{$week} Sunday', '%X%V %W') AND STR_TO_DATE('{$week} Saturday', '%X%V %W')";
|
||||
$union = ' UNION ';
|
||||
|
||||
}
|
||||
$uniqueUsers = $this->parseDataWeeksSimple( $queryActiveUsers );
|
||||
$newUsers = $this->parseDataWeeksSimple( $queryNewUsers );
|
||||
$data = [];
|
||||
for( $i = 0; $i < sizeof( $uniqueUsers ); $i++ ){
|
||||
$unique = $uniqueUsers[ $i ]->Total;
|
||||
$new = $newUsers[ $i ]->Total;
|
||||
if( $i - 1 >= 0 ){
|
||||
$uniquePrev = $uniqueUsers[ $i - 1 ]->Total;
|
||||
} else {
|
||||
$uniquePrev = 0;
|
||||
}
|
||||
|
||||
$lost = ( ( $uniquePrev + $new ) - $unique );
|
||||
$lost = ( $lost < 0 ) ? 0 : $lost;
|
||||
|
||||
// Formula: so, divide the number lost by the previous week's total
|
||||
if( $uniquePrev != 0 && $lost != 0 ){
|
||||
$result = $lost / $uniquePrev;
|
||||
} else {
|
||||
$result = 0;
|
||||
}
|
||||
$data[] = ( object ) array( 'Label' => $uniqueUsers[ $i ]->Label, 'Total' => number_format( $result, 4 ), 'Type' => 'Result' );
|
||||
}
|
||||
|
||||
$this->render( array( 'data' => $data, 'unit' => '' ) );
|
||||
break;
|
||||
|
||||
case 'churn-rate':
|
||||
|
||||
$queryActiveUsers = '';
|
||||
@ -316,10 +385,10 @@ class Controller_home_charts extends Crunchbutton_Controller_Account {
|
||||
} else {
|
||||
$uniquePrev = 0;
|
||||
}
|
||||
$churn = ( $unique - $uniquePrev - $new ) * -1;
|
||||
$churn = ( ( $uniquePrev + $new ) - $unique );
|
||||
// Do not show the negatives
|
||||
$churn = ( $churn < 0 ) ? 0 : $churn;
|
||||
$data[] = ( object ) array( 'Label' => $uniqueUsers[ $i ]->Label, 'Total' => $churn, 'Users' => 'Users' );
|
||||
$data[] = ( object ) array( 'Label' => $uniqueUsers[ $i ]->Label, 'Total' => $churn, 'Type' => 'Users' );
|
||||
}
|
||||
$this->render( array( 'data' => $data, 'unit' => 'users' ) );
|
||||
break;
|
||||
|
||||
@ -38,6 +38,7 @@ class Controller_home extends Crunchbutton_Controller_Account {
|
||||
'active-users-per-week' => 'Active Users per Week',
|
||||
'active-users-per-week-by-community' => 'Active Users per Week by Community',
|
||||
'churn-rate' => 'Churn Rate - Active Users Lost',
|
||||
'churn-rate-per-active-user' => 'Churn Rate - per Active User',
|
||||
'new-users-per-week' => 'New Users per Week',
|
||||
'new-users-per-week-by-community' => 'New Users per Week by Community',
|
||||
'new-users-per-active-users' => 'New Users per Active Users',
|
||||
|
||||
@ -279,10 +279,10 @@
|
||||
}
|
||||
chart.attr( 'opened', true );
|
||||
var weeksStr = '';
|
||||
if( weekFrom !== false ){
|
||||
if( weekFrom ){
|
||||
weeksStr += '&from=' + weekFrom;
|
||||
}
|
||||
if( weekTo !== false ){
|
||||
if( weekTo ){
|
||||
weeksStr += '&to=' + weekTo;
|
||||
}
|
||||
var activeUserDays = $( '#active-user-days' ).val();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user