Newer
Older
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Goals;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution;
use Piwik\Plugins\CoreVisualizations\Visualizations\Sparklines;
Thomas Steur
a validé
use Piwik\Plugin\ReportsProvider;
use Piwik\Widget\WidgetContainerConfig;
use Piwik\Widget\WidgetConfig;
use Piwik\Report\ReportWidgetFactory;
class Pages
{
private $orderId = 0;
private $allReports = array();
private $factory = array();
private $conversions;
public function __construct(ReportWidgetFactory $reportFactory, $reportsWithGoalMetrics)
{
$this->factory = $reportFactory;
$this->allReports = $reportsWithGoalMetrics;
$this->conversions = new Conversions();
}
/**
* @param array $goals
* @return WidgetConfig[]
*/
public function createGoalsOverviewPage($goals)
{
$subcategory = 'General_Overview';
$widgets = array();
$config = $this->factory->createWidget();
$config->forceViewDataTable(Evolution::ID);
$config->setSubcategoryId($subcategory);
$config->setAction('getEvolutionGraph');
$config->setIsNotWidgetizable();
$widgets[] = $config;
$config = $this->factory->createWidget();
$config->forceViewDataTable(Sparklines::ID);
$config->setSubcategoryId($subcategory);
$config->setName('');
$config->setIsNotWidgetizable();
$widgets[] = $config;
foreach ($goals as $goal) {
$name = Common::sanitizeInputValue($goal['name']);
$goalTranslated = Piwik::translate('Goals_GoalX', array($name));
$config = $this->factory->createWidget();
$config->setName($goalTranslated);
$config->setSubcategoryId($subcategory);
$config->forceViewDataTable(Sparklines::ID);
$config->setParameters(array('idGoal' => $goal['idgoal']));
$config->setIsNotWidgetizable();
$config->addParameters(array('allow_multiple' => (int) $goal['allow_multiple'], 'only_summary' => '1'));
$widgets[] = $config;
}
$container = $this->createWidgetizableWidgetContainer('GoalsOverview', $subcategory, $widgets);
$config = $this->factory->createContainerWidget('Goals');
$config->setSubcategoryId($subcategory);
$config->setName('Goals_ConversionsOverviewBy');
$config->setIsNotWidgetizable();
$this->buildGoalByDimensionView('', $config);
$config->setMiddlewareParameters(array(
'module' => 'Goals',
'action' => 'hasConversions'
));
return array($container, $config);
}
/**
* @return WidgetConfig[]
*/
public function createEcommerceOverviewPage()
{
$category = 'Goals_Ecommerce';
$subcategory = 'General_Overview';
$idGoal = Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER;
$widgets = array();
$config = $this->factory->createWidget();
$config->forceViewDataTable(Evolution::ID);
$config->setCategoryId($category);
$config->setSubcategoryId($subcategory);
$config->setAction('getEvolutionGraph');
$config->setIsNotWidgetizable();
$config->setParameters(array('idGoal' => $idGoal));
$widgets[] = $config;
$config = $this->factory->createWidget();
$config->setCategoryId($category);
$config->forceViewDataTable(Sparklines::ID);
$config->setSubcategoryId($subcategory);
$config->setName('');
$config->setModule('Ecommerce');
$config->setAction('getSparklines');
$config->setParameters(array('idGoal' => $idGoal));
$config->setIsNotWidgetizable();
$widgets[] = $config;
$config = $this->factory->createWidget();
$config->setModule('Ecommerce');
$config->setAction('getConversionsOverview');
$config->setSubcategoryId($idGoal);
$config->setName('Goals_ConversionsOverview');
$config->setParameters(array('idGoal' => $idGoal));
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
$config->setIsNotWidgetizable();
$config->setMiddlewareParameters(array(
'module' => 'Goals',
'action' => 'hasConversions',
'idGoal' => $idGoal
));
$widgets[] = $config;
$container = $this->createWidgetizableWidgetContainer('EcommerceOverview', $subcategory, $widgets);
return array($container);
}
/**
* @return WidgetConfig[]
*/
public function createEcommerceSalesPage()
{
$category = 'Goals_Ecommerce';
$subcategory = 'Ecommerce_Sales';
$config = $this->factory->createContainerWidget('GoalsOrder');
$config->setCategoryId($category);
$config->setSubcategoryId($subcategory);
$config->setName('');
$config->setParameters(array('idGoal' => Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER));
$config->setIsNotWidgetizable();
$this->buildGoalByDimensionView(Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER, $config);
return array($config);
}
/**
* @param array $goal
* @return WidgetConfig[]
*/
public function createGoalDetailPage($goal)
{
$widgets = array();
$idGoal = (int) $goal['idgoal'];
$name = Common::sanitizeInputValue($goal['name']);
$params = array('idGoal' => $idGoal);
$config = $this->factory->createWidget();
$config->setSubcategoryId($idGoal);
$config->forceViewDataTable(Evolution::ID);
$config->setAction('getEvolutionGraph');
$config->setParameters($params);
$config->setIsNotWidgetizable();
$widgets[] = $config;
$config = $this->factory->createWidget();
$config->setSubcategoryId($idGoal);
$config->setName('');
$config->forceViewDataTable(Sparklines::ID);
$config->setParameters($params);
$config->addParameters(array('allow_multiple' => (int) $goal['allow_multiple']));
$config->setIsNotWidgetizable();
$widgets[] = $config;
$config = $this->factory->createWidget();
$config->setAction('goalConversionsOverview');
$config->setSubcategoryId($idGoal);
$config->setName('Goals_ConversionsOverview');
$config->setParameters($params);
$config->setIsNotWidgetizable();
$config->setMiddlewareParameters(array(
'module' => 'Goals',
'action' => 'hasConversions',
'idGoal' => $idGoal
));
$widgets[] = $config;
$container = $this->createWidgetizableWidgetContainer('Goal_' . $idGoal, $name, $widgets);
$configs = array($container);
$config = $this->factory->createContainerWidget('Goals' . $idGoal);
$config->setName(Piwik::translate('Goals_GoalConversionsBy', array($name)));
$config->setSubcategoryId($idGoal);
$config->setParameters(array());
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
$config->setIsNotWidgetizable();
$config->setMiddlewareParameters(array(
'module' => 'Goals',
'action' => 'hasConversions',
'idGoal' => $idGoal
));
$this->buildGoalByDimensionView($idGoal, $config);
$configs[] = $config;
return $configs;
}
private function createWidgetizableWidgetContainer($containerId, $pageName, $widgets)
{
/** @var \Piwik\Widget\WidgetConfig[] $widgets */
$firstWidget = reset($widgets);
/** @var \Piwik\Report\ReportWidgetConfig $firstWidget */
if (!empty($pageName)) {
// make sure to not show two titles (one for this container and one for the first widget)
$firstWidget->setName('');
}
$config = $this->factory->createContainerWidget($containerId);
$config->setName($pageName);
$config->setCategoryId($firstWidget->getCategoryId());
$config->setSubcategoryId($firstWidget->getSubcategoryId());
$config->setIsWidgetizable();
$config->setOrder($this->orderId++);
foreach ($widgets as $widget) {
$config->addWidgetConfig($widget);
}
return $config;
}
private function buildGoalByDimensionView($idGoal, WidgetContainerConfig $container)
{
$container->setLayout('ByDimension');
$ecommerce = ($idGoal == Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER);
// for non-Goals reports, we show the goals table
$customParams = array('documentationForGoalsPage' => '1');
if ($idGoal === '') {
// if no idGoal, use 0 for overview. Must be string! Otherwise Piwik_View_HtmlTable_Goals fails.
$customParams['idGoal'] = '0';
} else {
$customParams['idGoal'] = $idGoal;
}
$translationHelper = new TranslationHelper();
foreach ($this->allReports as $category => $reports) {
$order = ($this->getSortOrderOfCategory($category) * 100);
if ($ecommerce) {
$categoryText = $translationHelper->translateEcommerceMetricCategory($category);
} else {
$categoryText = $translationHelper->translateGoalMetricCategory($category);
}
foreach ($reports as $report) {
$order++;
if (empty($report['viewDataTable'])
&& empty($report['abandonedCarts'])
) {
$report['viewDataTable'] = 'tableGoals';
}
if (!empty($report['parameters'])) {
$params = array_merge($customParams, $report['parameters']);
} else {
$params = $customParams;
}
$widget = $this->createWidgetForReport($report['module'], $report['action']);
if (!empty($report['name'])) {
$widget->setName($report['name']);
}
$widget->setCategoryId($categoryText);
$widget->setSubcategoryId($categoryText);
$widget->setOrder($order);
Benaka
a validé
if ($ecommerce) {
$widget->setIsWidgetizable();
} else {
$widget->setIsNotWidgetizable();
}
if (!empty($report['viewDataTable'])) {
$widget->forceViewDataTable($report['viewDataTable']);
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}
$container->addWidgetConfig($widget);
}
}
}
private function getSortOrderOfCategory($category)
{
static $order = null;
if (is_null($order)) {
$order = array(
'Referrers_Referrers',
'General_Visit',
'General_Visitors',
'VisitsSummary_VisitsSummary',
);
}
$value = array_search($category, $order);
if (false === $value) {
$value = count($order) + 1;
}
return $value;
}
private function createWidgetForReport($module, $action)
{
Thomas Steur
a validé
$report = ReportsProvider::factory($module, $action);
return $factory->createWidget();
}
}