How to get views / clicks of a specific product
-
13-06-2021 - |
Pergunta
As the title suggests, how do I get the number of views / clicks / impressions of a specific product in Magento. Any help is welcome.
Solução
This simple example will give you a list of products that have been viewed between the dates you specify + their view counts:
$fromDate = '2010-01-01';
$toDate = now();
$viewedProducts = Mage::getResourceModel('reports/product_collection')
->addViewsCount($fromDate, $toDate);
foreach($viewedProducts as $product) {
echo "ID: " . $product->getData('entity_id') . " - View Count: " . $product->getData('views') . "<br/>";
}
Outras dicas
It helped me, get views just for one product.
$resource = Mage::getResourceModel('reports/event');
$select = $resource->getReadConnection()->select()
->from(array('ev' => $resource->getMainTable()), array(
'product_id' => 'object_id',
'view_count' => new Zend_Db_Expr('COUNT(*)')
))
->join(
array('et' => $resource->getTable('reports/event_type')),
"ev.event_type_id=et.event_type_id AND et.event_name='catalog_product_view'",'')
->group('ev.object_id')
->where('ev.object_id IN(?)', [$entity_id])
->where('ev.logged_at >= ?', $from)
->where('ev.logged_at <= ?', $to);
$views = $resource->getReadConnection()->fetchPairs($select);
$views = !empty($views[$entity_id]) ? $views[$entity_id] : 0;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow