Differentiate machine and humans opens on subscriber stats API
[MAILPOET-3741]
This commit is contained in:
committed by
Veljko V
parent
bbf19d07c8
commit
14ff48c7fa
@ -44,6 +44,7 @@ class SubscriberStats extends APIEndpoint {
|
||||
'email' => $subscriber->getEmail(),
|
||||
'total_sent' => $statistics->getTotalSentCount(),
|
||||
'open' => $statistics->getOpenCount(),
|
||||
'machine_open' => $statistics->getMachineOpenCount(),
|
||||
'click' => $statistics->getClickCount(),
|
||||
'engagement_score' => $subscriber->getEngagementScore(),
|
||||
];
|
||||
|
@ -12,36 +12,35 @@ class SubscriberStatistics {
|
||||
/** @var int */
|
||||
private $openCount;
|
||||
|
||||
/** @var int */
|
||||
private $machineOpenCount;
|
||||
|
||||
/** @var int */
|
||||
private $totalSentCount;
|
||||
|
||||
/** @var WooCommerceRevenue|null */
|
||||
private $wooCommerceRevenue;
|
||||
|
||||
public function __construct($clickCount, $openCount, $totalSentCount, $wooCommerceRevenue = null) {
|
||||
public function __construct($clickCount, $openCount, $machineOpenCount, $totalSentCount, $wooCommerceRevenue = null) {
|
||||
$this->clickCount = $clickCount;
|
||||
$this->openCount = $openCount;
|
||||
$this->machineOpenCount = $machineOpenCount;
|
||||
$this->totalSentCount = $totalSentCount;
|
||||
$this->wooCommerceRevenue = $wooCommerceRevenue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getClickCount(): int {
|
||||
return $this->clickCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getOpenCount(): int {
|
||||
return $this->openCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getMachineOpenCount(): int {
|
||||
return $this->machineOpenCount;
|
||||
}
|
||||
|
||||
public function getTotalSentCount(): int {
|
||||
return $this->totalSentCount;
|
||||
}
|
||||
|
@ -8,9 +8,11 @@ use MailPoet\Entities\StatisticsNewsletterEntity;
|
||||
use MailPoet\Entities\StatisticsOpenEntity;
|
||||
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Entities\UserAgentEntity;
|
||||
use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
|
||||
use MailPoet\WooCommerce\Helper as WCHelper;
|
||||
use MailPoetVendor\Doctrine\ORM\EntityManager;
|
||||
use MailPoetVendor\Doctrine\ORM\QueryBuilder;
|
||||
|
||||
/**
|
||||
* @extends Repository<SubscriberEntity>
|
||||
@ -33,31 +35,46 @@ class SubscriberStatisticsRepository extends Repository {
|
||||
return new SubscriberStatistics(
|
||||
$this->getStatisticsClickCount($subscriber),
|
||||
$this->getStatisticsOpenCount($subscriber),
|
||||
$this->getStatisticsMachineOpenCount($subscriber),
|
||||
$this->getTotalSentCount($subscriber),
|
||||
$this->getWooCommerceRevenue($subscriber)
|
||||
);
|
||||
}
|
||||
|
||||
private function getStatisticsClickCount(SubscriberEntity $subscriber): int {
|
||||
return $this->getStatisticsCount(StatisticsClickEntity::class, $subscriber);
|
||||
return (int)$this->getStatisticsCountQuery(StatisticsClickEntity::class, $subscriber)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function getStatisticsOpenCount(SubscriberEntity $subscriber): int {
|
||||
return $this->getStatisticsCount(StatisticsOpenEntity::class, $subscriber);
|
||||
return (int)$this->getStatisticsCountQuery(StatisticsOpenEntity::class, $subscriber)
|
||||
->andWhere('(stats.userAgentType = :userAgentType) OR (stats.userAgentType IS NULL)')
|
||||
->setParameter('userAgentType', UserAgentEntity::USER_AGENT_TYPE_HUMAN)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function getStatisticsMachineOpenCount(SubscriberEntity $subscriber): int {
|
||||
return (int)$this->getStatisticsCountQuery(StatisticsOpenEntity::class, $subscriber)
|
||||
->andWhere('(stats.userAgentType = :userAgentType)')
|
||||
->setParameter('userAgentType', UserAgentEntity::USER_AGENT_TYPE_MACHINE)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function getTotalSentCount(SubscriberEntity $subscriber): int {
|
||||
return $this->getStatisticsCount(StatisticsNewsletterEntity::class, $subscriber);
|
||||
return $this->getStatisticsCountQuery(StatisticsNewsletterEntity::class, $subscriber)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function getStatisticsCount($entityName, SubscriberEntity $subscriber): int {
|
||||
return (int)$this->entityManager->createQueryBuilder()
|
||||
private function getStatisticsCountQuery(string $entityName, SubscriberEntity $subscriber): QueryBuilder {
|
||||
return $this->entityManager->createQueryBuilder()
|
||||
->select('COUNT(DISTINCT stats.newsletter) as cnt')
|
||||
->from($entityName, 'stats')
|
||||
->where('stats.subscriber = :subscriber')
|
||||
->setParameter('subscriber', $subscriber)
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
->setParameter('subscriber', $subscriber);
|
||||
}
|
||||
|
||||
private function getWooCommerceRevenue(SubscriberEntity $subscriber) {
|
||||
|
Reference in New Issue
Block a user