You should specify the product attribute set id before you do the selection, like this:
$yourAttributeSet = 15; // or other
$prodData = $this->_prodCollection->setProductAttributeSetId($yourAttributeSet)->addViewsCount()->getData();
becouse in the addViewsCount
method only one attribute set id is available:
$this->getSelect()->reset()->from(
['report_table_views' => $this->getTable('report_event')],
['views' => 'COUNT(report_table_views.event_id)']
)->join(
['e' => $this->getProductEntityTableName()],
$this->getConnection()->quoteInto(
'e.entity_id = report_table_views.object_id AND e.attribute_set_id = ?',
$this->getProductAttributeSetId()
)
)->where(
'report_table_views.event_type_id = ?',
$productViewEvent
)->group(
'e.entity_id'
)->order(
'views ' . self::SORT_ORDER_DESC
)->having(
'COUNT(report_table_views.event_id) > ?',
0
);
I think there is no way to select the data using this collection for many attribute sets.
Result sql looks like this:
SELECT COUNT(report_table_views.event_id) AS `views`,
`e`.*
FROM `report_event` AS `report_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id AND e.attribute_set_id = '4'
WHERE (report_table_views.event_type_id = 1)
GROUP BY `e`.`entity_id`
HAVING (COUNT(report_table_views.event_id) > 0)
ORDER BY `views` DESC
As you can see the default attribute set is set to 4 (default). If we remove this condition from the select we can get statistics for the all of products:
SELECT COUNT(report_table_views.event_id) AS `views`,
`e`.*
FROM `report_event` AS `report_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = report_table_views.object_id
WHERE (report_table_views.event_type_id = 1)
GROUP BY `e`.`entity_id`
HAVING (COUNT(report_table_views.event_id) > 0)
ORDER BY `views` DESC
Result:
+-------+-----------+------------------+--------------+---------+-------------+------------------+---------------------+---------------------+
| views | entity_id | attribute_set_id | type_id | sku | has_options | required_options | created_at | updated_at |
+-------+-----------+------------------+--------------+---------+-------------+------------------+---------------------+---------------------+
| 33 | 1 | 15 | simple | 24-MB01 | 0 | 0 | 2016-11-22 08:43:50 | 2016-11-22 08:43:50 |
| 3 | 3 | 15 | simple | 24-MB03 | 0 | 0 | 2016-11-22 08:43:50 | 2016-11-22 08:43:50 |
| 1 | 611 | 9 | configurable | MS07 | 1 | 0 | 2016-11-22 08:44:24 | 2016-11-22 08:44:24 |
| 1 | 846 | 10 | configurable | MP09 | 1 | 0 | 2016-11-22 08:44:31 | 2016-11-22 08:44:31 |
| 1 | 20 | 11 | simple | 24-UG01 | 0 | 0 | 2016-11-22 08:43:53 | 2016-11-22 08:43:53 |
+-------+-----------+------------------+--------------+---------+-------------+------------------+---------------------+---------------------+
5 rows in set (0.00 sec)
If you wish to get the same result you should write your own filter-method for the collection, like this (just remove the attribute set condition):
public function addViewsCount($from = '', $to = '')
{
/**
* Getting event type id for catalog_product_view event
*/
$eventTypes = $this->_eventTypeFactory->create()->getCollection();
foreach ($eventTypes as $eventType) {
if ($eventType->getEventName() == 'catalog_product_view') {
$productViewEvent = (int)$eventType->getId();
break;
}
}
$this->getSelect()->reset()->from(
['report_table_views' => $this->getTable('report_event')],
['views' => 'COUNT(report_table_views.event_id)']
)->join(
['e' => $this->getProductEntityTableName()],
'e.entity_id = report_table_views.object_id'
)->where(
'report_table_views.event_type_id = ?',
$productViewEvent
)->group(
'e.entity_id'
)->order(
'views ' . self::SORT_ORDER_DESC
)->having(
'COUNT(report_table_views.event_id) > ?',
0
);
if ($from != '' && $to != '') {
$this->getSelect()->where('logged_at >= ?', $from)->where('logged_at <= ?', $to);
}
return $this;
}
Result in the debug window:

Example of the code:
/**
* @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
*/
protected $prodCollectionFactory;
/**
* @var \Magento\Reports\Model\ResourceModel\Event\Type\CollectionFactory
*/
protected $eventTypeCollectionFactory;
/**
* Channels constructor.
* @param \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $prodCollectionFactory
* @param \Magento\Reports\Model\ResourceModel\Event\Type\CollectionFactory $eventTypeCollectionFactory
* @throws Exception
*/
public function __construct(
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $prodCollectionFactory,
\Magento\Reports\Model\ResourceModel\Event\Type\CollectionFactory $eventTypeCollectionFactory
) {
$this->eventTypeCollectionFactory = $eventTypeCollectionFactory;
$this->prodCollectionFactory = $prodCollectionFactory;
}
/**
* @param string $from
* @param string $to
* @return array
* @throws Exception
*/
public function getReportData($from = '', $to = '')
{
/** @var \Magento\Reports\Model\ResourceModel\Product\Collection $prodCollection */
$prodCollection = $this->prodCollectionFactory->create();
/** @var \Magento\Reports\Model\ResourceModel\Event\Type\Collection $eventTypeCollection */
$eventTypeCollection = $this->eventTypeCollectionFactory->create();
$catalogProductViewFilter = $eventTypeCollection->addFieldToFilter('event_name', 'catalog_product_view')->getFirstItem();
if ($catalogProductViewFilter->getData('event_type_id')) {
$productViewEvent = $catalogProductViewFilter->getData('event_type_id');
} else {
throw new Exception(__('Unknown event: catalog_product_view'));
}
$prodCollection->getSelect()->reset()->from(
['report_table_views' => $prodCollection->getTable('report_event')],
['views' => 'COUNT(report_table_views.event_id)']
)->join(
['e' => $prodCollection->getProductEntityTableName()],
'e.entity_id = report_table_views.object_id'
)->where(
'report_table_views.event_type_id = ?',
$productViewEvent
)->group(
'e.entity_id'
)->order(
'views ' . \Magento\Framework\Data\Collection::SORT_ORDER_DESC
)->having(
'COUNT(report_table_views.event_id) > ?',
0
);
if ($from != '' && $to != '') {
$prodCollection->getSelect()->where('logged_at >= ?', $from)->where('logged_at <= ?', $to);
}
$prodData = $prodCollection->getData();
return $prodData;
}