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

@@ -10,6 +10,9 @@ class NewsletterStatistics {
/** @var int */
private $openCount;
/** @var int */
private $machineOpens;
/** @var int */
private $unsubscribeCount;
@@ -27,49 +30,40 @@ class NewsletterStatistics {
$this->wooCommerceRevenue = $wooCommerceRevenue;
}
/**
* @return int
*/
public function getClickCount() {
public function getClickCount(): int {
return $this->clickCount;
}
/**
* @return int
*/
public function getOpenCount() {
public function getOpenCount(): int {
return $this->openCount;
}
/**
* @return int
*/
public function getUnsubscribeCount() {
public function getUnsubscribeCount(): int {
return $this->unsubscribeCount;
}
/**
* @return int
*/
public function getTotalSentCount() {
public function getTotalSentCount(): int {
return $this->totalSentCount;
}
/**
* @return WooCommerceRevenue|null
*/
public function getWooCommerceRevenue() {
public function getWooCommerceRevenue(): ?WooCommerceRevenue {
return $this->wooCommerceRevenue;
}
/**
* @return array
*/
public function asArray() {
public function setMachineOpens(int $machineOpens): void {
$this->machineOpens = $machineOpens;
}
public function getMachineOpens(): int {
return $this->machineOpens;
}
public function asArray(): array {
return [
'clicked' => (int)$this->clickCount,
'opened' => (int)$this->openCount,
'unsubscribed' => (int)$this->unsubscribeCount,
'clicked' => $this->clickCount,
'opened' => $this->openCount,
'machineOpens' => $this->machineOpens,
'unsubscribed' => $this->unsubscribeCount,
'revenue' => empty($this->wooCommerceRevenue) ? null : $this->wooCommerceRevenue->asArray(),
];
}

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;