Skip to content

Commit

Permalink
Merge pull request Smile-SA#52 from Elastic-Suite/feat-ESP-210-add-co…
Browse files Browse the repository at this point in the history
…nversation-rate

[Behavioral Data] - Add conversion rate #ESP-210
  • Loading branch information
rbayet authored Aug 12, 2021
2 parents eda5bd7 + 37990a0 commit e78a3ea
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ public function loadBehavioralData($storeId, $productIds)
try {
$views = $this->getViewsData($storeId, $productIds);
$sales = $this->getSalesData($storeId, $productIds);
$conversionRates = $this->getConversionRateData($views, $sales);

// Not using array_merge_recursive here because it discards numeric keys (which are product ids).
$data = array_replace_recursive($views, $sales);
$data = array_replace_recursive($views, $sales, $conversionRates);
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage());
}
Expand Down Expand Up @@ -356,6 +357,63 @@ private function getSalesData($storeId, $productIds)
return $this->parseResponse($result, 'sales');
}

/**
* Compute conversion rates data for a given sales and views data.
*
* @param array $viewsData The views data
* @param array $salesData The sales data
*
* @return array
*/
private function getConversionRateData(array $viewsData, array $salesData)
{
$conversionRateData = [];
// Initialize value with "1" if there is no views data for this product in order to avoid
// division by 0.
$defaultViewsData = [
'total' => 1,
'daily' => ['ma' => 1, 'count' => 1],
'weekly' => ['ma' => 1, 'count' => 1],
];
array_walk(
$salesData,
function ($sales, $productId) use (&$conversionRateData, $viewsData, $defaultViewsData) {
$views = $defaultViewsData;
if (array_key_exists($productId, $viewsData)) {
$views = $viewsData[$productId]['_stats']['views'];
}
$sales = $sales['_stats']['sales'];
$data = [
'total' => $this->calculateConvRate($views['total'], $sales['total']),
'daily' => [
'ma' => $this->calculateConvRate($views['daily']['ma'], $sales['daily']['ma']),
'count' => $this->calculateConvRate($views['daily']['count'], $sales['daily']['count']),
],
'weekly' => [
'ma' => $this->calculateConvRate($views['weekly']['ma'], $sales['weekly']['ma']),
'count' => $this->calculateConvRate($views['weekly']['count'], $sales['weekly']['count']),
],
];
$conversionRateData[$productId] = ['_stats' => ['conversion_rate' => $data]];
}
);

return $conversionRateData;
}

/**
* Calculate conversion rates value.
*
* @param int $viewsNumber The views number of the product
* @param int $salesNumber The sales number of the product
*
* @return float
*/
private function calculateConvRate($viewsNumber, $salesNumber)
{
return $salesNumber / ($viewsNumber ?: 1);
}

/**
* Compute behavioral data across query results.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,27 @@
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
<field name="_stats.conversion_rate.total" type="double">
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
<field name="_stats.conversion_rate.daily.ma" type="double">
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
<field name="_stats.conversion_rate.daily.count" type="double">
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
<field name="_stats.conversion_rate.weekly.ma" type="double">
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
<field name="_stats.conversion_rate.weekly.count" type="double">
<sortOrderAscMissing>_first</sortOrderAscMissing>
<sortOrderDescMissing>_last</sortOrderDescMissing>
</field>
</mapping>
</type>
</index>
</indices>
</indices>

0 comments on commit e78a3ea

Please sign in to comment.