forked from jesstelford/funnels
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAPI.php
247 lines (225 loc) · 7.52 KB
/
API.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
<?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_API
{
static private $instance = null;
static public function getInstance()
{
if (self::$instance == null)
{
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
public function getFunnels( $idSite )
{
Piwik::checkUserHasViewAccess($idSite);
$funnel_table = Piwik_Common::prefixTable('funnel');
$goal_table = Piwik_Common::prefixTable('goal');
$funnel_step_table = Piwik_Common::prefixTable('funnel_step');
$funnels = Piwik_FetchAll("SELECT ".$funnel_table.".*, ".$goal_table.".name as goal_name, ".$goal_table.".idgoal
FROM ".$funnel_table.", ".$goal_table."
WHERE ".$funnel_table.".idsite = ?
AND ".$goal_table.".idsite = ?
AND ".$funnel_table.".idgoal = ".$goal_table.".idgoal
AND ".$funnel_table.".deleted = 0", array($idSite, $idSite));
$funnelsById = array();
foreach($funnels as &$funnel)
{
$funnel_steps = Piwik_FetchAll("SELECT *
FROM ".$funnel_step_table."
WHERE idsite = ?
AND idfunnel = ?
AND deleted = 0", array($idSite, $funnel['idfunnel']));
$funnel['steps'] = $funnel_steps;
$funnelsById[$funnel['idfunnel']] = $funnel;
}
return $funnelsById;
}
public function getFunnelsByGoal( $idSite )
{
Piwik::checkUserHasViewAccess($idSite);
$funnelsByGoal = array();
$funnels = $this->getFunnels( $idSite );
foreach($funnels as &$funnel)
{
$funnelsByGoal[$funnel['idgoal']] = $funnel;
}
return $funnelsByGoal;
}
public function getGoalsWithoutFunnels( $idSite )
{
Piwik::checkUserHasViewAccess($idSite);
$goals = Piwik_Goals_API::getInstance()->getGoals( $idSite );
$funnelsByGoal = $this->getFunnelsByGoal( $idSite );
$goalsWithoutFunnels = array();
foreach($goals as &$goal)
{
if(!array_key_exists($goal['idgoal'], $funnelsByGoal)) {
$goalsWithoutFunnels[$goal['idgoal']] = $goal;
}
}
return $goalsWithoutFunnels;
}
public function addFunnel( $idSite, $idGoal, $steps )
{
Piwik::checkUserHasAdminAccess($idSite);
// save in db
$idFunnel = Piwik_FetchOne("SELECT max(idfunnel) + 1
FROM ".Piwik_Common::prefixTable('funnel')."
WHERE idsite = ?", $idSite);
if($idFunnel == false)
{
$idFunnel = 1;
}
Piwik_Query("INSERT INTO " . Piwik_Common::prefixTable('funnel')."
(idsite, idgoal, idfunnel)
VALUES (?, ?, ?)", array($idSite, $idGoal, $idFunnel));
Piwik_Tracker_Cache::regenerateCacheWebsiteAttributes($idSite);
$this->updateFunnel($idSite, $idGoal, $idFunnel, $steps);
return $idFunnel;
}
public function updateFunnel( $idSite, $idGoal, $idFunnel, $steps=array())
{
Piwik::checkUserHasAdminAccess($idSite);
$currentStepIds = array();
foreach($steps as &$step){
$idStep = $step['id'];
if (! is_numeric($idStep))
{
continue;
}
$currentStepIds[] = $idStep;
$name = $this->checkName($step['name']);
$url = $this->checkUrl($step['pattern']);
$pattern_type = $this->checkPatternType($step['pattern_type']);
$match_attribute = $this->checkMatchAttribute($step['match_attribute']);
$case_sensitive = $step['case_sensitive'];
$exists = Piwik_FetchOne("SELECT idstep
FROM ".Piwik_Common::prefixTable('funnel_step')."
WHERE idsite = ?
AND idfunnel = ?
AND idstep = ?", array($idSite, $idFunnel, $idStep));
if ($exists){
Piwik_Query("UPDATE ".Piwik_Common::prefixTable('funnel_step')."
SET name = ?, url = ?, match_attribute = ?, pattern_type = ?, case_sensitive = ?,
deleted = 0
WHERE idsite = ? AND idstep = ? AND idfunnel = ?",
array($name, $url, $match_attribute, $pattern_type, $case_sensitive, $idSite, $idStep,
$idFunnel));
} else {
Piwik_Query("INSERT INTO ". Piwik_Common::prefixTable('funnel_step')."
(idsite, idfunnel, idstep, name, url, match_attribute, pattern_type, case_sensitive)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
array($idSite, $idFunnel, $idStep, $name, $url, $match_attribute, $pattern_type,
$case_sensitive));
}
}
// Any steps not currently defined should be set to deleted
$whereClause = " WHERE idsite = ? AND idfunnel = ? ";
$params = array($idSite, $idFunnel);
if (count($currentStepIds) > 0)
{
$currentStepIds = join(', ', $currentStepIds);
$whereClause .= "AND idstep not in ($currentStepIds)";
}
Piwik_Query("UPDATE ". Piwik_Common::prefixTable('funnel_step')."
SET deleted = 1
$whereClause", $params);
Piwik_Common::regenerateCacheWebsiteAttributes($idSite);
}
public function deleteFunnel( $idSite, $idGoal, $idFunnel )
{
Piwik::checkUserHasAdminAccess($idSite);
Piwik_Query("UPDATE ".Piwik_Common::prefixTable('funnel')."
SET deleted = 1
WHERE idsite = ?
AND idgoal = ?
AND idfunnel = ?",
array($idSite, $idGoal, $idFunnel));
Piwik_Common::regenerateCacheWebsiteAttributes($idSite);
}
// Get the referring URLs for a funnel step
public function getNextUrls( $idSite, $period, $date, $idFunnel, $idStep ) {
Piwik::checkUserHasViewAccess( $idSite );
$archive = Piwik_Archive::build( $idSite, $period, $date );
$recordName = Piwik_Funnels::getRecordName('idaction_url_next', $idFunnel, $idStep);
$dataTable = $archive->getDataTable($recordName);
return $dataTable;
}
// Get the referring URLs for a funnel step
public function getRefUrls( $idSite, $period, $date, $idFunnel, $idStep ) {
Piwik::checkUserHasViewAccess( $idSite );
$archive = Piwik_Archive::build( $idSite, $period, $date );
$recordName = Piwik_Funnels::getRecordName('idaction_url_ref', $idFunnel, $idStep);
$dataTable = $archive->getDataTable($recordName);
return $dataTable;
}
public function get( $idSite, $period, $date, $idFunnel = false, $columns = array() )
{
Piwik::checkUserHasViewAccess( $idSite );
$archive = Piwik_Archive::build( $idSite, $period, $date );
if(!empty($columns))
{
$toFetch = $columns;
}
else
{
$toFetch = array();
$funnels = $this->getFunnels($idSite);
$stepNumericColumnNames = array(
'nb_entry',
'nb_exit',
'nb_actions',
'nb_next_step_actions',
'percent_next_step_actions'
);
foreach($stepNumericColumnNames as $columnName)
{
if (!empty($idFunnel)) {
$funnel = $funnels[$idFunnel];
foreach($funnel['steps'] as $step) {
$toFetch[] = Piwik_Funnels::getRecordName($columnName, $idFunnel, $step['idstep']);
}
}
}
$funnelColumnNames = array(
'nb_actions',
'conversion_rate');
foreach($funnelColumnNames as $columnName)
{
if (!empty($idFunnel)) {
$funnel = $funnels[$idFunnel];
$toFetch[] = Piwik_Funnels::getRecordName($columnName, $idFunnel, false);
}
}
}
$dataTable = $archive->getDataTableFromNumeric($toFetch);
return $dataTable;
}
private function checkName($name)
{
return urldecode($name);
}
private function checkUrl($url)
{
return urldecode($url);
}
private function checkPatternType($str) {
return urldecode($str);
}
private function checkMatchAttribute($str) {
return urldecode($str);
}
}