Differentiate machine and humans opens on subscriber stats API

[MAILPOET-3741]
This commit is contained in:
Rostislav Wolny
2021-08-26 15:15:34 +02:00
committed by Veljko V
parent bbf19d07c8
commit 14ff48c7fa
3 changed files with 35 additions and 18 deletions

View File

@ -44,6 +44,7 @@ class SubscriberStats extends APIEndpoint {
'email' => $subscriber->getEmail(), 'email' => $subscriber->getEmail(),
'total_sent' => $statistics->getTotalSentCount(), 'total_sent' => $statistics->getTotalSentCount(),
'open' => $statistics->getOpenCount(), 'open' => $statistics->getOpenCount(),
'machine_open' => $statistics->getMachineOpenCount(),
'click' => $statistics->getClickCount(), 'click' => $statistics->getClickCount(),
'engagement_score' => $subscriber->getEngagementScore(), 'engagement_score' => $subscriber->getEngagementScore(),
]; ];

View File

@ -12,36 +12,35 @@ class SubscriberStatistics {
/** @var int */ /** @var int */
private $openCount; private $openCount;
/** @var int */
private $machineOpenCount;
/** @var int */ /** @var int */
private $totalSentCount; private $totalSentCount;
/** @var WooCommerceRevenue|null */ /** @var WooCommerceRevenue|null */
private $wooCommerceRevenue; private $wooCommerceRevenue;
public function __construct($clickCount, $openCount, $totalSentCount, $wooCommerceRevenue = null) { public function __construct($clickCount, $openCount, $machineOpenCount, $totalSentCount, $wooCommerceRevenue = null) {
$this->clickCount = $clickCount; $this->clickCount = $clickCount;
$this->openCount = $openCount; $this->openCount = $openCount;
$this->machineOpenCount = $machineOpenCount;
$this->totalSentCount = $totalSentCount; $this->totalSentCount = $totalSentCount;
$this->wooCommerceRevenue = $wooCommerceRevenue; $this->wooCommerceRevenue = $wooCommerceRevenue;
} }
/**
* @return int
*/
public function getClickCount(): int { public function getClickCount(): int {
return $this->clickCount; return $this->clickCount;
} }
/**
* @return int
*/
public function getOpenCount(): int { public function getOpenCount(): int {
return $this->openCount; return $this->openCount;
} }
/** public function getMachineOpenCount(): int {
* @return int return $this->machineOpenCount;
*/ }
public function getTotalSentCount(): int { public function getTotalSentCount(): int {
return $this->totalSentCount; return $this->totalSentCount;
} }

View File

@ -8,9 +8,11 @@ use MailPoet\Entities\StatisticsNewsletterEntity;
use MailPoet\Entities\StatisticsOpenEntity; use MailPoet\Entities\StatisticsOpenEntity;
use MailPoet\Entities\StatisticsWooCommercePurchaseEntity; use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
use MailPoet\Entities\SubscriberEntity; use MailPoet\Entities\SubscriberEntity;
use MailPoet\Entities\UserAgentEntity;
use MailPoet\Newsletter\Statistics\WooCommerceRevenue; use MailPoet\Newsletter\Statistics\WooCommerceRevenue;
use MailPoet\WooCommerce\Helper as WCHelper; use MailPoet\WooCommerce\Helper as WCHelper;
use MailPoetVendor\Doctrine\ORM\EntityManager; use MailPoetVendor\Doctrine\ORM\EntityManager;
use MailPoetVendor\Doctrine\ORM\QueryBuilder;
/** /**
* @extends Repository<SubscriberEntity> * @extends Repository<SubscriberEntity>
@ -33,31 +35,46 @@ class SubscriberStatisticsRepository extends Repository {
return new SubscriberStatistics( return new SubscriberStatistics(
$this->getStatisticsClickCount($subscriber), $this->getStatisticsClickCount($subscriber),
$this->getStatisticsOpenCount($subscriber), $this->getStatisticsOpenCount($subscriber),
$this->getStatisticsMachineOpenCount($subscriber),
$this->getTotalSentCount($subscriber), $this->getTotalSentCount($subscriber),
$this->getWooCommerceRevenue($subscriber) $this->getWooCommerceRevenue($subscriber)
); );
} }
private function getStatisticsClickCount(SubscriberEntity $subscriber): int { 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 { 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 { 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 { private function getStatisticsCountQuery(string $entityName, SubscriberEntity $subscriber): QueryBuilder {
return (int)$this->entityManager->createQueryBuilder() return $this->entityManager->createQueryBuilder()
->select('COUNT(DISTINCT stats.newsletter) as cnt') ->select('COUNT(DISTINCT stats.newsletter) as cnt')
->from($entityName, 'stats') ->from($entityName, 'stats')
->where('stats.subscriber = :subscriber') ->where('stats.subscriber = :subscriber')
->setParameter('subscriber', $subscriber) ->setParameter('subscriber', $subscriber);
->getQuery()
->getSingleScalarResult();
} }
private function getWooCommerceRevenue(SubscriberEntity $subscriber) { private function getWooCommerceRevenue(SubscriberEntity $subscriber) {