|
4 | 4 |
|
5 | 5 | namespace PhpList\Core\Domain\Analytics\Service; |
6 | 6 |
|
| 7 | +use DateInterval; |
7 | 8 | use DateTimeImmutable; |
8 | 9 | use PhpList\Core\Domain\Analytics\Repository\UserMessageViewRepository; |
9 | 10 | use PhpList\Core\Domain\Analytics\Service\Manager\LinkTrackManager; |
@@ -415,4 +416,61 @@ public function getTopLocalParts(int $limit = 25): array |
415 | 416 | 'total' => count($result), |
416 | 417 | ]; |
417 | 418 | } |
| 419 | + |
| 420 | + public function getCampaignPerformance(): array |
| 421 | + { |
| 422 | + $performance = []; |
| 423 | + $endDate = new DateTimeImmutable('today 23:59:59'); |
| 424 | + $startDate = $endDate->sub(new DateInterval('P29D'))->modify('00:00:00'); |
| 425 | + |
| 426 | + for ($index = 0; $index < 30; $index++) { |
| 427 | + $dayStart = $startDate->add(new DateInterval('P' . $index . 'D')); |
| 428 | + $dayEnd = $dayStart->modify('23:59:59'); |
| 429 | + |
| 430 | + $performance[] = [ |
| 431 | + 'date' => $dayStart->format('Y-m-d'), |
| 432 | + 'opens' => $this->userMessageViewManager->countViewsBetween($dayStart, $dayEnd), |
| 433 | + 'clicks' => $this->linkTrackManager->countClicksBetween($dayStart, $dayEnd), |
| 434 | + ]; |
| 435 | + } |
| 436 | + |
| 437 | + return $performance; |
| 438 | + } |
| 439 | + |
| 440 | + /** |
| 441 | + * Get recent campaigns with their performance rates |
| 442 | + * |
| 443 | + * @param int $limit |
| 444 | + * @return array |
| 445 | + */ |
| 446 | + public function getRecentCampaigns(int $limit = 5): array |
| 447 | + { |
| 448 | + $messages = $this->messageRepository->getFilteredAfterId(0, $limit)->getItems(); |
| 449 | + $recentCampaigns = []; |
| 450 | + foreach ($messages as $message) { |
| 451 | + $views = $this->userMessageViewManager->countViewsByMessageId($message->getId()); |
| 452 | + $linkTracks = $this->linkTrackManager->getLinkTracksByMessageId($message->getId()); |
| 453 | + |
| 454 | + $uniqueClickers = []; |
| 455 | + foreach ($linkTracks as $linkTrack) { |
| 456 | + $uniqueClickers[$linkTrack->getUserId()] = true; |
| 457 | + } |
| 458 | + $uniqueClicks = count($uniqueClickers); |
| 459 | + |
| 460 | + $sentCount = $message->getMetadata()->getViews() + $message->getMetadata()->getBounceCount(); |
| 461 | + |
| 462 | + $openRate = $sentCount > 0 ? ($views / $sentCount) * 100 : 0; |
| 463 | + $clickRate = $sentCount > 0 ? ($uniqueClicks / $sentCount) * 100 : 0; |
| 464 | + |
| 465 | + $recentCampaigns[] = [ |
| 466 | + 'name' => $message->getContent()->getSubject(), |
| 467 | + 'status' => $message->getMetadata()->getStatus()?->value, |
| 468 | + 'date' => $message->getMetadata()->getSent()?->format('Y-m-d'), |
| 469 | + 'openRate' => round($openRate, 2) . '%', |
| 470 | + 'clickRate' => round($clickRate, 2) . '%', |
| 471 | + ]; |
| 472 | + } |
| 473 | + |
| 474 | + return $recentCampaigns; |
| 475 | + } |
418 | 476 | } |
0 commit comments