_storeManager = $storeManager; $this->_eventFactory = $event; $this->_customerSession = $customerSession; $this->_customerVisitor = $customerVisitor; } /** * Save event * * @param int $eventTypeId * @param int $objectId * @param int|null $subjectId * @param int $subtype * @return void */ public function save($eventTypeId, $objectId, $subjectId = null, $subtype = 0) { if ($subjectId === null) { if ($this->_customerSession->isLoggedIn()) { $subjectId = $this->_customerSession->getCustomerId(); } else { $subjectId = $this->_customerVisitor->getId(); $subtype = 1; } } /** @var \Magento\Reports\Model\Event $eventModel */ $eventModel = $this->_eventFactory->create(); $storeId = $this->_storeManager->getStore()->getId(); $eventModel->setData([ 'event_type_id' => $eventTypeId, 'object_id' => $objectId, 'subject_id' => $subjectId, 'subtype' => $subtype, 'store_id' => $storeId, ]); $eventModel->save(); } }