After the answer @Shoaib Munir & @Rk Rathod, These tables would not update on runtime so you need to run Refresh Statistics from Reports -> Statistics -> Refresh Statistics
every times. but if you don't want to refresh Statistics manually every time so you may set cron for that so it will refresh Statistics automatically.
First, you need to pass reportTypes argument to your cron class by using di.xml like below (I have passed all the arguments you may pass as per your requirement).
<type name="Vendor\Module\Cron\CronFile">
<arguments>
<argument name="reportTypes" xsi:type="array">
<item name="sales" xsi:type="string">Magento\Sales\Model\ResourceModel\Report\Order</item>
<item name="tax" xsi:type="string">Magento\Tax\Model\ResourceModel\Report\Tax</item>
<item name="shipping" xsi:type="string">Magento\Sales\Model\ResourceModel\Report\Shipping</item>
<item name="invoiced" xsi:type="string">Magento\Sales\Model\ResourceModel\Report\Invoiced</item>
<item name="refunded" xsi:type="string">Magento\Sales\Model\ResourceModel\Report\Refunded</item>
<item name="coupons" xsi:type="string">Magento\SalesRule\Model\ResourceModel\Report\Rule</item>
<item name="bestsellers" xsi:type="string">Magento\Sales\Model\ResourceModel\Report\Bestsellers</item>
<item name="viewed" xsi:type="string">Magento\Reports\Model\ResourceModel\Report\Product\Viewed</item>
</argument>
</arguments>
</type>
Then in your cron file should be like this.
<?php
namespace Vendor\Module\Cron;
use Magento\Reports\Model\ResourceModel\Refresh\Collection;
class CronFile extends Collection
{
protected $logger;
protected $reportTypes;
public function __construct(
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Reports\Model\FlagFactory $reportsFlagFactory,
\Psr\Log\LoggerInterface $logger,
array $reportTypes
) {
$this->logger = $logger;
$this->reportTypes = $reportTypes;
parent::__construct($entityFactory,$localeDate,$reportsFlagFactory);
}
/**
* @return $this
*/
public function execute()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
try {
$codes = $this->loadData();
foreach ($codes->_items as $codek=>$codev) {
$objectManager->create($this->reportTypes[$codek])->aggregate();
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->logger->critical($e->getMessage());
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
return $this;
}
}
I hope it helps!