forked from jesstelford/funnels
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController.php
More file actions
151 lines (125 loc) · 5.22 KB
/
Controller.php
File metadata and controls
151 lines (125 loc) · 5.22 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
<?php
/**
* Piwik - Open source web analytics
* Funnel Plugin - Analyse and visualise goal funnels
*
* @link http://mysociety.org
* @license http://www.gnu.org/licenses/agpl-3.0.html
* @version 0.2
*
* @category Piwik_Plugins
* @package Piwik_Funnels
*/
class Piwik_Funnels_Controller extends Piwik_Controller
{
const ROUNDING_PRECISION = 2;
const NUM_URLS_TO_DISPLAY = 5;
function __construct()
{
parent::__construct();
$this->idSite = Piwik_Common::getRequestVar('idSite');
$this->funnels = Piwik_Funnels_API::getInstance()->getFunnels($this->idSite);
}
function addNewFunnel()
{
$view = Piwik_View::factory('add_new_funnel');
$this->setGeneralVariablesView($view);
$view->goalsPluginDeactived = ! Piwik_PluginsManager::getInstance()->isPluginActivated('Goals');
$view->userCanEditFunnels = Piwik::isUserHasAdminAccess($this->idSite);
$view->onlyShowAddNewFunnel = true;
$view->goalsWithoutFunnels = Piwik_Funnels_API::getInstance()->getGoalsWithoutFunnels($this->idSite);
echo $view->render();
}
function index()
{
$view = Piwik_View::factory('overview');
$this->setGeneralVariablesView($view);
$view->funnels = $this->funnels;
$view->funnelsJSON = json_encode($this->funnels);
$view->userCanEditFunnels = Piwik::isUserHasAdminAccess($this->idSite);
$view->goalsWithoutFunnels = Piwik_Funnels_API::getInstance()->getGoalsWithoutFunnels($this->idSite);
echo $view->render();
}
function funnelReport()
{
$idFunnel = Piwik_Common::getRequestVar('idFunnel', null, 'int');
if(!isset($this->funnels[$idFunnel]))
{
Piwik::redirectToModule('Funnels', 'index', array('idFunnel' => null));
}
// Set up the view
$view = Piwik_View::factory('single_funnel');
$this->setGeneralVariablesView($view);
// Get the funnel and related goal data
$funnelDefinition = $this->funnels[$idFunnel];
$idGoal = $funnelDefinition['idgoal'];
$goal_request = new Piwik_API_Request("method=Goals.get&format=original&idGoal=$idGoal");
$datatable = $goal_request->process();
$dataRow = $datatable->getFirstRow();
$view->goal_conversions = $dataRow->getColumn('nb_conversions');
$view->name = $funnelDefinition['goal_name'];
// Get the data on each funnel step
$funnel_data = $this->getMetricsForFunnel($idFunnel);
foreach ($funnelDefinition['steps'] as &$step) {
$recordName = Piwik_Funnels::getRecordName('nb_actions', $idFunnel, $step['idstep']);
$step['nb_actions'] = $funnel_data->getColumn($recordName);
$recordName = Piwik_Funnels::getRecordName('nb_next_step_actions', $idFunnel, $step['idstep']);
$step['nb_next_step_actions'] = $funnel_data->getColumn($recordName);
$recordName = Piwik_Funnels::getRecordName('percent_next_step_actions', $idFunnel, $step['idstep']);
$step['percent_next_step_actions'] = round($funnel_data->getColumn($recordName), self::ROUNDING_PRECISION);
$recordName = Piwik_Funnels::getRecordName('nb_entry', $idFunnel, $step['idstep']);
$step['nb_entry'] = $funnel_data->getColumn($recordName);
$step['referring_actions'] = array();
$refUrls = $this->getRefUrls($idFunnel, $step['idstep']);
$refUrls->filter('Sort', array('value', 'desc'));
$refUrls->filter('Limit', array(0, self::NUM_URLS_TO_DISPLAY));
foreach($refUrls->getRows() as $row) {
$label = $this->labelOrDefault($row->getColumn('label'), '(entrance)');
$step['referring_actions'][] = array('label' => $label, 'value' => $row->getColumn('value'));
}
$recordName = Piwik_Funnels::getRecordName('nb_exit', $idFunnel, $step['idstep']);
$step['nb_exit'] = $funnel_data->getColumn($recordName);
$step['next_actions'] = array();
$nextUrls = $this->getNextUrls($idFunnel, $step['idstep']);
$nextUrls->filter('Sort', array('value', 'desc'));
$nextUrls->filter('Limit', array(0, self::NUM_URLS_TO_DISPLAY));
foreach($nextUrls->getRows() as $row) {
$label = $this->labelOrDefault($row->getColumn('label'), '(exit)');
$step['next_actions'][] = array('label' => $label, 'value' => $row->getColumn('value'));
}
}
// What percent of people who visited the first funnel step converted at the end of the funnel?
$recordName = Piwik_Funnels::getRecordName('conversion_rate', $idFunnel, false);
$view->conversion_rate = round($funnel_data->getColumn($recordName), self::ROUNDING_PRECISION);
// Let the view access the funnel steps
$view->steps = $funnelDefinition['steps'];
echo $view->render();
}
protected function labelOrDefault($label, $default)
{
if ($label == 'null')
{
return $default;
}
return $label;
}
protected function getNextUrls($idFunnel, $idStep)
{
$request = new Piwik_API_Request("method=Funnels.getNextUrls&format=original&idFunnel=$idFunnel&idStep=$idStep");
$dataTable = $request->process();
return $dataTable;
}
protected function getRefUrls($idFunnel, $idStep)
{
$request = new Piwik_API_Request("method=Funnels.getRefUrls&format=original&idFunnel=$idFunnel&idStep=$idStep");
$dataTable = $request->process();
return $dataTable;
}
protected function getMetricsForFunnel($idFunnel)
{
$request = new Piwik_API_Request("method=Funnels.get&format=original&idFunnel=$idFunnel");
$dataTable = $request->process();
$dataRow = $dataTable->getFirstRow();
return $dataRow;
}
}