2

As the title suggests, how do I get the number of views / clicks / impressions of a specific product in Magento. Any help is welcome.

Filburt
  • 17,626
  • 12
  • 64
  • 115
ThreeCheeseHigh
  • 1,429
  • 5
  • 22
  • 40
  • 2
    possible duplicate of [Get view count for magento product based on product\_id](http://stackoverflow.com/questions/9172755/get-view-count-for-magento-product-based-on-product-id) – Kostanos Jul 23 '13 at 18:52

2 Answers2

6

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/>";
}
Drew Hunter
  • 10,136
  • 2
  • 40
  • 49
0

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;