Display machine opens

[MAILPOET-3740]
This commit is contained in:
Pavel Dohnal
2021-08-25 13:42:25 +02:00
committed by Veljko V
parent 58a9e9580d
commit 464f23c82b
7 changed files with 67 additions and 35 deletions

View File

@ -12,6 +12,7 @@ use MailPoet\Entities\StatisticsWooCommercePurchaseEntity;
use MailPoet\Entities\UserAgentEntity;
use MailPoet\WooCommerce\Helper as WCHelper;
use MailPoetVendor\Doctrine\ORM\EntityManager;
use MailPoetVendor\Doctrine\ORM\QueryBuilder;
use MailPoetVendor\Doctrine\ORM\UnexpectedResultException;
/**
@ -32,13 +33,15 @@ class NewsletterStatisticsRepository extends Repository {
}
public function getStatistics(NewsletterEntity $newsletter): NewsletterStatistics {
return new NewsletterStatistics(
$stats = new NewsletterStatistics(
$this->getStatisticsClickCount($newsletter),
$this->getStatisticsOpenCount($newsletter),
$this->getStatisticsUnsubscribeCount($newsletter),
$this->getTotalSentCount($newsletter),
$this->getWooCommerceRevenue($newsletter)
);
$stats->setMachineOpens($this->getStatisticsMachineOpenCount($newsletter));
return $stats;
}
/**
@ -81,6 +84,17 @@ class NewsletterStatisticsRepository extends Repository {
return $counts[$newsletter->getId()] ?? 0;
}
public function getStatisticsMachineOpenCount(NewsletterEntity $newsletter): int {
$qb = $this->getStatisticsQuery(StatisticsOpenEntity::class, [$newsletter]);
$result = $qb->andWhere('(stats.userAgentType = :userAgentType)')
->setParameter('userAgentType', UserAgentEntity::USER_AGENT_TYPE_MACHINE)
->getQuery()
->getResult();
if (empty($result)) return 0;
return $result['cnt'] ?? 0;
}
public function getStatisticsUnsubscribeCount(NewsletterEntity $newsletter): int {
$counts = $this->getStatisticCounts(StatisticsUnsubscribeEntity::class, [$newsletter]);
return $counts[$newsletter->getId()] ?? 0;
@ -132,12 +146,7 @@ class NewsletterStatisticsRepository extends Repository {
}
private function getStatisticCounts(string $statisticsEntityName, array $newsletters): array {
$qb = $this->entityManager->createQueryBuilder()
->select('IDENTITY(stats.newsletter) AS id, COUNT(DISTINCT stats.subscriber) as cnt')
->from($statisticsEntityName, 'stats')
->where('stats.newsletter IN (:newsletters)')
->groupBy('stats.newsletter')
->setParameter('newsletters', $newsletters);
$qb = $this->getStatisticsQuery($statisticsEntityName, $newsletters);
if (in_array($statisticsEntityName, [StatisticsOpenEntity::class, StatisticsClickEntity::class], true)) {
$qb->andWhere('(stats.userAgentType = :userAgentType) OR (stats.userAgentType IS NULL)')
->setParameter('userAgentType', UserAgentEntity::USER_AGENT_TYPE_HUMAN);
@ -154,6 +163,15 @@ class NewsletterStatisticsRepository extends Repository {
return $counts;
}
private function getStatisticsQuery(string $statisticsEntityName, array $newsletters): QueryBuilder {
return $this->entityManager->createQueryBuilder()
->select('IDENTITY(stats.newsletter) AS id, COUNT(DISTINCT stats.subscriber) as cnt')
->from($statisticsEntityName, 'stats')
->where('stats.newsletter IN (:newsletters)')
->groupBy('stats.newsletter')
->setParameter('newsletters', $newsletters);
}
private function getWooCommerceRevenues(array $newsletters) {
if (!$this->wcHelper->isWooCommerceActive()) {
return null;