Skip to content

Commit

Permalink
API Fixing the items avg_price value
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Nov 26, 2013
1 parent b69f9a5 commit d947868
Showing 1 changed file with 69 additions and 48 deletions.
117 changes: 69 additions & 48 deletions plugins/Goals/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
*/
class API extends \Piwik\Plugin\API
{
const AVG_PRICE_VIEWED = 'avg_price_viewed';

/**
* Returns all Goals for a given website, or list of websites
*
Expand Down Expand Up @@ -212,6 +214,10 @@ protected function getItems($recordName, $idSite, $period, $date, $abandonedCart
$dataTable = $archive->getDataTable($recordNameFinal);

$dataTable->filter('Sort', array(Metrics::INDEX_ECOMMERCE_ITEM_REVENUE));

$renameColumn = array(self::AVG_PRICE_VIEWED => 'avg_price');
$dataTable->queueFilter('ReplaceColumnNames', array($renameColumn, $skipIfColumnAlreadyNonEmpty = true));

$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');

Expand All @@ -225,55 +231,11 @@ protected function getItems($recordName, $idSite, $period, $date, $abandonedCart
$dataTable->queueFilter('ColumnCallbackAddColumnQuotient', array('avg_price', 'price', $ordersColumn, GoalManager::REVENUE_PRECISION));

// Average quantity = sum product quantity / abandoned carts
$dataTable->queueFilter('ColumnCallbackAddColumnQuotient', array('avg_quantity', 'quantity', $ordersColumn, $precision = 1));
$dataTable->queueFilter('ColumnCallbackAddColumnQuotient',
array('avg_quantity', 'quantity', $ordersColumn, $precision = 1));
$dataTable->queueFilter('ColumnDelete', array('price'));

// Enrich the datatable with Product/Categories views, and conversion rates
$customVariables = \Piwik\Plugins\CustomVariables\API::getInstance()->getCustomVariables($idSite, $period, $date, $segment = false, $expanded = false,
$_leavePiwikCoreVariables = true);
$mapping = array(
'Goals_ItemsSku' => '_pks',
'Goals_ItemsName' => '_pkn',
'Goals_ItemsCategory' => '_pkc',
);
$reportToNotDefinedString = array(
'Goals_ItemsSku' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductSKU')), // Note: this should never happen
'Goals_ItemsName' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductName')),
'Goals_ItemsCategory' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductCategory'))
);
$notDefinedStringPretty = $reportToNotDefinedString[$recordName];
$customVarNameToLookFor = $mapping[$recordName];

// Handle case where date=last30&period=day
if ($customVariables instanceof DataTable\Map) {
$customVariableDatatables = $customVariables->getDataTables();
$dataTables = $dataTable->getDataTables();
foreach ($customVariableDatatables as $key => $customVariableTableForDate) {
$dataTableForDate = isset($dataTables[$key]) ? $dataTables[$key] : new DataTable();

// we do not enter the IF
// if case idSite=1,3 AND period=day&date=datefrom,dateto,
if ($customVariableTableForDate instanceof DataTable
&& $customVariableTableForDate->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)
) {
$dateRewrite = $customVariableTableForDate->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->toString();
$row = $customVariableTableForDate->getRowFromLabel($customVarNameToLookFor);
if ($row) {
$idSubtable = $row->getIdSubDataTable();
$this->enrichItemsDataTableWithItemsViewMetrics($dataTableForDate, $idSite, $period, $dateRewrite, $idSubtable);
}
$dataTable->addTable($dataTableForDate, $key);
}
$this->renameNotDefinedRow($dataTableForDate, $notDefinedStringPretty);
}
} elseif ($customVariables instanceof DataTable) {
$row = $customVariables->getRowFromLabel($customVarNameToLookFor);
if ($row) {
$idSubtable = $row->getIdSubDataTable();
$this->enrichItemsDataTableWithItemsViewMetrics($dataTable, $idSite, $period, $date, $idSubtable);
}
$this->renameNotDefinedRow($dataTable, $notDefinedStringPretty);
}
$this->enrichItemsTableWithViewMetrics($dataTable, $recordName, $idSite, $period, $date);

// Product conversion rate = orders / visits
$dataTable->queueFilter('ColumnCallbackAddColumnPercentage', array('conversion_rate', $ordersColumn, 'nb_visits', GoalManager::REVENUE_PRECISION));
Expand Down Expand Up @@ -311,7 +273,7 @@ protected function enrichItemsDataTableWithItemsViewMetrics($dataTable, $idSite,
if (empty($price)) {
// If a price was tracked on the product page
if ($rowView->getColumn(Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED)) {
$rowView->renameColumn(Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED, 'avg_price');
$rowView->renameColumn(Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED, self::AVG_PRICE_VIEWED);
}
}
$rowView->deleteColumn(Metrics::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED);
Expand Down Expand Up @@ -544,4 +506,63 @@ public function getVisitsUntilConversion($idSite, $period, $date, $segment = fal

return $dataTable;
}

/**
* Enhances the dataTable with Items attributes found in the Custom Variables report.
*
* @param $dataTable
* @param $recordName
* @param $idSite
* @param $period
* @param $date
*/
protected function enrichItemsTableWithViewMetrics($dataTable, $recordName, $idSite, $period, $date)
{
// Enrich the datatable with Product/Categories views, and conversion rates
$customVariables = \Piwik\Plugins\CustomVariables\API::getInstance()->getCustomVariables($idSite, $period, $date, $segment = false, $expanded = false,
$_leavePiwikCoreVariables = true);
$mapping = array(
'Goals_ItemsSku' => '_pks',
'Goals_ItemsName' => '_pkn',
'Goals_ItemsCategory' => '_pkc',
);
$reportToNotDefinedString = array(
'Goals_ItemsSku' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductSKU')), // Note: this should never happen
'Goals_ItemsName' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductName')),
'Goals_ItemsCategory' => Piwik::translate('General_NotDefined', Piwik::translate('Goals_ProductCategory'))
);
$notDefinedStringPretty = $reportToNotDefinedString[$recordName];
$customVarNameToLookFor = $mapping[$recordName];

// Handle case where date=last30&period=day
if ($customVariables instanceof DataTable\Map) {
$customVariableDatatables = $customVariables->getDataTables();
$dataTables = $dataTable->getDataTables();
foreach ($customVariableDatatables as $key => $customVariableTableForDate) {
$dataTableForDate = isset($dataTables[$key]) ? $dataTables[$key] : new DataTable();

// we do not enter the IF
// if case idSite=1,3 AND period=day&date=datefrom,dateto,
if ($customVariableTableForDate instanceof DataTable
&& $customVariableTableForDate->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)
) {
$dateRewrite = $customVariableTableForDate->getMetadata(Archive\DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getDateStart()->toString();
$row = $customVariableTableForDate->getRowFromLabel($customVarNameToLookFor);
if ($row) {
$idSubtable = $row->getIdSubDataTable();
$this->enrichItemsDataTableWithItemsViewMetrics($dataTableForDate, $idSite, $period, $dateRewrite, $idSubtable);
}
$dataTable->addTable($dataTableForDate, $key);
}
$this->renameNotDefinedRow($dataTableForDate, $notDefinedStringPretty);
}
} elseif ($customVariables instanceof DataTable) {
$row = $customVariables->getRowFromLabel($customVarNameToLookFor);
if ($row) {
$idSubtable = $row->getIdSubDataTable();
$this->enrichItemsDataTableWithItemsViewMetrics($dataTable, $idSite, $period, $date, $idSubtable);
}
$this->renameNotDefinedRow($dataTable, $notDefinedStringPretty);
}
}
}

0 comments on commit d947868

Please sign in to comment.