diff --git a/lib/API/JSON/v1/SubscriberStats.php b/lib/API/JSON/v1/SubscriberStats.php index f3cf122239..50c5174b11 100644 --- a/lib/API/JSON/v1/SubscriberStats.php +++ b/lib/API/JSON/v1/SubscriberStats.php @@ -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(), ]; diff --git a/lib/Subscribers/Statistics/SubscriberStatistics.php b/lib/Subscribers/Statistics/SubscriberStatistics.php index 116b650700..83ad927860 100644 --- a/lib/Subscribers/Statistics/SubscriberStatistics.php +++ b/lib/Subscribers/Statistics/SubscriberStatistics.php @@ -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; } diff --git a/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php b/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php index 265f5f08ce..42d16c230d 100644 --- a/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php +++ b/lib/Subscribers/Statistics/SubscriberStatisticsRepository.php @@ -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 @@ -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) {